#include #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int dx[4] = { -1,0,1,0 }; int dy[4] = { 0,-1,0,1 }; int ddice[6][4] = { {3,2,1,4}, {0,1,5,1}, {2,5,2,0}, {5,3,0,3}, {4,0,4,5}, {1,4,3,2} }; int H,W; int sy,sx, gy,gx; int blocks[1000][1000]; vector> E; int Vid(int y,int x,int dice){ return (y*W + x) * 6 + dice; } int main(){ cin >> H >> W; cin >> sy >> sx >> gy >> gx; sy--; sx--; gy--; gx--; rep(y,H) rep(x,W){ char c; cin >> c; if(c == '.') blocks[y][x] = 0; else blocks[y][x] = 1; } E.resize(H*W*6); rep(y,H) rep(x,W) rep(d,6) rep(to,4){ int ny = y + dy[to]; int nx = x + dx[to]; int nd = ddice[d][to]; //cout << "d = " << d << ", to = " << to << ", nd = " << nd << endl; if(ny < 0 || H <= ny) continue; if(nx < 0 || W <= nx) continue; if(blocks[y][x]) continue; if(blocks[ny][nx]) continue; E[Vid(y,x,d)].push_back(Vid(ny,nx,nd)); } vector D(H*W*6,-1); vector I; D[Vid(sy,sx,0)] = 0; I.push_back(Vid(sy,sx,0)); for(int i=0; i