/* -*- coding: utf-8 -*- * * 1572.cc: No.1572 XI - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_H = 1000; const int MAX_W = 1000; const int D = 6; const int dxs[] = { 1, 0, -1, 0 }, dys[] = { 0, -1, 0, 1 }; const int rots[D][4] = { { 1, 2, 3, 4 }, { 5, 1, 0, 1 }, { 2, 5, 2, 0 }, { 0, 3, 5, 3 }, { 4, 0, 4, 5 }, { 3, 4, 1, 2 }, }; /* typedef */ struct Stat { int y, x, d; Stat() {} Stat(int _y, int _x, int _d): y(_y), x(_x), d(_d) {} }; /* global variables */ char fs[MAX_H][MAX_W + 4]; int ds[MAX_H][MAX_W][D]; /* subroutines */ /* main */ int main() { int h, w, sy, sx, gy, gx; scanf("%d%d%d%d%d%d", &h, &w, &sy, &sx, &gy, &gx); sy--, sx--, gy--, gx--; for (int y = 0; y < h; y++) scanf("%s", fs[y]); memset(ds, -1, sizeof(ds)); ds[sy][sx][0] = 0; queue q; q.push(Stat(sy, sx, 0)); while (! q.empty()) { Stat u = q.front(); q.pop(); if (u.y == gy && u.x == gx && u.d == 0) break; for (int di = 0; di < 4; di++) { int vy = u.y + dys[di], vx = u.x + dxs[di], vd = rots[u.d][di]; if (vy >= 0 && vy < h && vx >= 0 && vx < w && fs[vy][vx] == '.' && ds[vy][vx][vd] < 0) { ds[vy][vx][vd] = ds[u.y][u.x][u.d] + 1; q.push(Stat(vy, vx, vd)); } } } printf("%d\n", (ds[gy][gx][0] >= 0) ? ds[gy][gx][0] : -1); return 0; }