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")
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

class Dice{
private:
    int l[7][7] = {}; 
public:
    int x,y,z;
    int top,front,bottom,back,right,left;
    Dice(int t,int f){
        l[1][2] = 4; l[1][3] = 2; l[1][4] = 5; l[1][5] = 3;
        l[2][3] = 6; l[2][4] = 1; l[2][6] = 4; l[3][5] = 6; l[3][6] = 2;
        l[4][5] = 1; l[4][6] = 5; l[5][6] = 3;
        top = t; front = f;
        if(t<f) left = l[t][f];
        else left = 7-l[f][t];
        bottom = 7-top; back = 7-front; right = 7-left;
    }
    int num(int d){
        if(d==0) return front;
        if(d==1) return right;
        if(d==2) return back;
        if(d==3) return left;
    }
    void roll(int d){
        int buf;
        if(d==0){//手前
            buf=top;top=back;back=bottom;bottom=front;front=buf;x--;
        }else if(d==1){//右
            buf=top;top=left;left=bottom;bottom=right;right=buf;z++;
        }else if(d==2){//後
            buf=top;top=front;front=bottom;bottom=back;back=buf;x++;
        }else if(d==3){//左
            buf=top;top=right;right=bottom;bottom=left;left=buf;z--;
        }
        y--;
    }
};

int dp[110][110][7][7] = {};

struct state{
    int x,y,top,front;
};

vec<int> dx = {1,0,-1,0},dy = {0,1,0,-1};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H,W;
    cin >> H >> W;
    auto in = [&](int x,int y){
        return 0<=x && x<H && 0<=y && y<W;
    };
    vvec<char> F(H,vec<char>(W));
    for(int i=0;i<H;i++) for(int j=0;j<W;j++) cin >> F[i][j];
    queue<state> Q;
    Q.push({0,0,1,2});
    dp[0][0][1][2] = 1;
    while(!Q.empty()){
        state now = Q.front(); Q.pop();
        Dice D(now.top,now.front);
        for(int k=0;k<4;k++){
            int nx = now.x+dx[k],ny = now.y+dy[k];
            if(!in(nx,ny)) continue;
            Dice D(now.top,now.front);
            int n = F[nx][ny]-'0';
            D.roll(k);
            if(n!=D.bottom) continue;
            if(!dp[nx][ny][D.top][D.front]){
                dp[nx][ny][D.top][D.front] = 1;
                Q.push({nx,ny,D.top,D.front});
            }
        }
    }
    bool ok = false;
    for(int i=1;i<7;i++) for(int j=1;j<7;j++) ok |= dp[H-1][W-1][i][j];
    cout << (ok? "YES\n":"NO\n");
}

C++: #include <bits/stdc++.h>
using namespace std;

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

const vector<string> A {
    "6314",
    "2.2.",
    "1364",
    "5.5.",
};

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    for(auto &e : S) cin >> e;
    queue<pair<int, int>> que;
    vector<vector<int>> dist(H, vector<int>(W, -1));
    que.emplace(0, 0);
    dist[0][0] = 0;
    while(!que.empty()) {
        auto cur = que.front(); que.pop();
        int y = cur.first, x = cur.second;
        for(int i = 0; i < 4; ++i) {
            int ny = y + dy[i], nx = x + dx[i];
            if(ny < 0 || nx < 0 || ny >= H || nx >= W) continue;
            if(dist[ny][nx] != -1) continue;
            if(S[ny][nx] == A[ny % 4][nx % 4]) {
                que.emplace(ny, nx);
                dist[ny][nx] = dist[y][x] + 1;
            }
        }
    }
    cout << (dist[H - 1][W - 1] != -1 ? "YES" : "NO") << '\n';
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

int H,W;
vector<string> S;

int dp[101][101][7][7][7];//dw,fr,ri
struct State{
    int dw,fr,ri;
    State(int dw,int fr,int ri):dw(dw),fr(fr),ri(ri){}
    State(){}
};

State toR(State now){
    State nex=now;
    nex.ri=7-now.dw;
    nex.fr=now.fr;
    nex.dw=now.ri;
    return nex;
}
State toL(State now){
    State nex=now;
    nex.ri=now.dw;
    nex.fr=now.fr;
    nex.dw=7-now.ri;
    return nex;
}
State toD(State now){
    State nex=now;
    nex.ri=now.ri;
    nex.fr=7-now.dw;
    nex.dw=now.fr;
    return nex;
}
State toU(State now){
    State nex=now;
    nex.ri=now.ri;
    nex.fr=now.dw;
    nex.dw=7-now.fr;
    return nex;
}

const int INF=1e9;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

bool bfs(){
    rep(i,H) rep(j,W) rep(k,7) rep(l,7) rep(m,7) dp[i][j][k][l][m]=INF;
    queue<pair<pair<int,int>,State>> Q;
    Q.push(mkp(mkp(0,0),State{6,2,3}));
    dp[0][0][6][2][3]=0;

    while(!Q.empty()){
        auto f=Q.front();
        Q.pop();
        int x=f.first.first;
        int y=f.first.second;
        State now=f.second;

        rep(k,4){
            int nx=x+dx[k];
            int ny=y+dy[k];
            if(nx<0||ny<0||nx>=H||ny>=W) continue;
            State nex;
            if(k==0) nex=toR(now);
            else if(k==1) nex=toD(now);
            else if(k==2) nex=toL(now);
            else nex=toU(now);

            if(S[nx][ny]=='#') continue;
            if(S[nx][ny]-'0'!=nex.dw) continue;
            
            if(dp[nx][ny][nex.dw][nex.fr][nex.ri]==INF){
                dp[nx][ny][nex.dw][nex.fr][nex.ri]=0;
                Q.push(mkp(mkp(nx,ny),nex));
            }
        }
    }

    rep(k,7) rep(l,7) rep(m,7) if(dp[H-1][W-1][k][l][m]!=INF) return true;
    return false;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin>>H>>W;
  S.resize(H);
  rep(i,H) cin>>S[i];

  if(bfs()) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;

  return 0;
}