Go: package main

import "fmt"

var h, w int
var maze [][]byte

var visited [][]bool

type dice struct {
	top, bottom, right, left, up, down int
}

func move(d *dice, dir byte) {
	now := d.top
	switch dir {
	case 'N':
		d.top = d.down
		d.down = d.bottom
		d.bottom = d.up
		d.up = now
	case 'S':
		d.top = d.up
		d.up = d.bottom
		d.bottom = d.down
		d.down = now
	case 'W':
		d.top = d.right
		d.right = d.bottom
		d.bottom = d.left
		d.left = now
	case 'E':
		d.top = d.left
		d.left = d.bottom
		d.bottom = d.right
		d.right = now
	}
}

var dx = [4]int{1, -1, 0, 0}
var dy = [4]int{0, 0, 1, -1}

func dfs(x, y int, d *dice) bool {
	// fmt.Printf("dfs(%d,%d) en: dice.bottom=%d\n", x, y, d.bottom)
	visited[y][x] = true // (x,y)に到達した
	if (y == h-1) && (x == w-1) {
		return true
	}
	// 4方のマス目に進めるかどうかチェック
	for i := 0; i < 4; i++ {
		nx := x + dx[i]
		ny := y + dy[i]
		dir := []byte{'E', 'W', 'S', 'N'}
		dc := *d
		move(&dc, dir[i]) // 動かしてみる
		if 0 <= nx && nx < w && 0 <= ny && ny < h && maze[ny][nx] != '#' && !visited[ny][nx] && dc.bottom == int(maze[ny][nx]-'0') {
			// fmt.Printf("move dir %c, new bottom=%d, move to (nx, ny)=(%d,%d)\n", dir[i], dc.bottom, nx, ny)
			if dfs(nx, ny, &dc) {
				return true
			}
		} else {
			continue
		}
	}
	return false
}

func main() {
	fmt.Scan(&h, &w)
	maze = make([][]byte, h)
	visited = make([][]bool, h)
	for i := 0; i < h; i++ {
		visited[i] = make([]bool, w)
		maze[i] = make([]byte, w)
		fmt.Scan(&maze[i])
	}
	// fmt.Println("h", h, "w", w, "maze", maze)
	// visited[0][0] = true
	var d dice
	d.bottom = 6
	d.top = 1
	d.right = 3
	d.left = 4
	d.down = 2
	d.up = 5
	if dfs(0, 0, &d) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

Python: H,W = map(int,input().split())
S = [list(input()) for i in range(H)]

A = [[False]*W for i in range(H)]

Q = [(0,0,(5,4,1,3,6,2))]

while Q:
    q = Q.pop()
    h,w,s = q[0],q[1],q[2]
    if min(h,w)<0 or h>=H or w>=W:continue
    if S[h][w]=="#":continue
    if A[h][w]:continue
    if int(S[h][w])!=s[4]:continue
    A[h][w] = True
    Q.append((h-1,w,(s[2],s[1],s[5],s[3],s[0],s[4])))
    Q.append((h+1,w,(s[4],s[1],s[0],s[3],s[5],s[2])))
    Q.append((h,w-1,(s[0],s[2],s[3],s[4],s[1],s[5])))
    Q.append((h,w+1,(s[0],s[4],s[1],s[2],s[3],s[5])))
print("YES" if A[H-1][W-1] else "NO")

Python: H,W=map(int,input().split())
MAP=[input() for i in range(H)]

CANGO=[[0]*W for i in range(H)]
CANGO[0][0]=1

Q=[(0,0)]

while Q:
    x,y=Q.pop()

    for tox,toy in [[x+1,y],[x-1,y],[x,y+1],[x,y-1]]:
        if 0<=tox<H and 0<=toy<W and CANGO[tox][toy]==0:
            if tox%4==0:
                if toy%4==0 and MAP[tox][toy]=="6":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
                if toy%4==1 and MAP[tox][toy]=="3":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
                if toy%4==2 and MAP[tox][toy]=="1":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
                if toy%4==3 and MAP[tox][toy]=="4":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
            if tox%4==2:
                if toy%4==0 and MAP[tox][toy]=="1":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
                if toy%4==1 and MAP[tox][toy]=="3":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
                if toy%4==2 and MAP[tox][toy]=="6":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
                if toy%4==3 and MAP[tox][toy]=="4":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
            if tox%4==1:
                if toy%4==0 and MAP[tox][toy]=="2":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))

                if toy%4==2 and MAP[tox][toy]=="2":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))
            if tox%4==3:
                if toy%4==0 and MAP[tox][toy]=="5":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))

                if toy%4==2 and MAP[tox][toy]=="5":
                    CANGO[tox][toy]=1
                    Q.append((tox,toy))

if CANGO[H-1][W-1]==1:
    print("YES")
else:
    print("NO")

Python: import sys
sys.setrecursionlimit(10**6)
H, W = map(int, input().split())
S = [[c for c in input()] for _ in range(H)]
B = [[False] * W for _ in range(H)]

R = [['6', '3', '1', '4'], ['1', '3', '6', '4']]
C = [['6', '2', '1', '5'], ['1', '2', '6', '5']]
G = [['6', '3', '1', '4'], ['2', '-1', '2', '-1'],
     ['1', '3', '6', '4'], ['5', '-1', '5', '-1']]

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]


def dfs(y, x):
    if y < 0 or H <= y or x < 0 or W <= x:
        return
    if S[y][x] == '#':
        return
    if B[y][x] == True:
        return

    m4y = y % 4
    m4x = x % 4

    if S[y][x] != G[m4y][m4x]:
        return

    # R
    # if y % 2 == 0:
    #    if not(y % 4 != 0 or y % 4 == 0 and S[y][x] == R[0][x % 4]):
    #        return
    #    if not(y % 4 != 2 or y % 4 == 2 and S[y][x] == R[1][x % 4]):
    #        return
    # C
    # if y % 2 == 1:
    #    if not(y % 4 != 1 or y % 4 == 1 and S[y][x] == '2'):
    #        return
    #    if not(y % 4 != 3 or y % 4 == 3 and S[y][x] == '5'):
    #        return

    B[y][x] = True

    for i in range(4):
        dfs(y+dy[i], x+dx[i])


dfs(0, 0)
#print(*B, sep='\n')
print('YES' if B[H-1][W-1] else 'NO')

Python: h, w = map(int, input().split())

p = list(range(h*w))

def find(x):
    global p
    if p[x] == x:return x
    p[x] = find(p[x])
    return p[x]

def unite(x, y):
    global p
    x = find(x)
    y = find(y)
    if x != y:p[x] =y

def real_number(i, j):
    i += 1
    j += 1
    i %= 4
    j %= 4
    if i == 1 and j == 1:return 6
    if i == 1 and j == 2:return 3
    if i == 1 and j == 3:return 1
    if i == 1 and j == 0:return 4
    if i == 2 and j == 1:return 2
    if i == 2 and j == 3:return 2
    if i == 3 and j == 1:return 1
    if i == 3 and j == 2:return 3
    if i == 3 and j == 3:return 6
    if i == 3 and j == 0:return 4
    if i == 0 and j == 1:return 5
    if i == 0 and j == 3:return 5
    return -1
    
field = [input() for i in range(h)]
for i in range(h):
    for j in range(w):
        if str(real_number(i,j)) != field[i][j]:continue
        if i + 1 < h and str(real_number(i+1,j)) == field[i+1][j]:
            unite(i*w+j, (i+1)*w+j)
        if j + 1 < w and str(real_number(i,j+1)) == field[i][j+1]:
            unite(i*w+j, i*w+j+1)

if find(0) == find(h*w-1):
    print("YES")
else:
    print("NO")

Python: from collections import deque

h,w = map(int,input().split())
g = [list(map(int, list(input().replace('#', '0')))) for _ in range(h)]
f = [[False]*w for _ in range(h)]
q = deque()
d = ((0,1), (1,0), (-1,0), (0,-1))

def chst(c,st,i):
  if i == 1:
    nc = st[1]
    nst = (st[0], 7-c)
  if i == 0:
    nc = st[0]
    nst = (7-c, st[1])
  if i == 2:
    nc = 7-st[1]
    nst = (st[0], c)
  if i == 3:
    nc = 7 - st[0]
    nst = (c, st[1])
  return nc, nst

q.append((0,0,(2,3)))
while q:
  y,x,st = q.popleft()
  if f[y][x]: continue
  f[y][x] = st
  for i in range(4):
    dx, dy = d[i]
    nx = x + dx; ny = y + dy
    if 0 <= nx < w and 0 <= ny < h:
      nc, nst = chst(g[y][x], st, i)
      if nc == g[ny][nx]:
        q.append((ny,nx,nst))
print('YES' if f[h-1][w-1] else 'NO')
# print(*f, sep = '\n')