1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <bits/stdc++.h> #define rint register int using namespace std;
int N, M, sx, sy, ex, ey; int a[60][60], ans[60][60]; char d;
struct Direction { int x, y, nxt; Direction(int _x = 0, int _y = 0, int _nxt = 'Z') { x = _x; y = _y; nxt = _nxt; } }; Direction dire[100];
struct Robot { int x, y, t; char d; Robot(int _x, int _y, int _t, char _d) { x = _x; y = _y; t = _t; d = _d; } }; queue<Robot> que;
int main() { cin >> N >> M; for (rint i = 0; i<N; ++i) for (rint j = 0; j<M; ++j) cin >> a[i][j]; cin >> sx >> sy >> ex >> ey >> d;
dire['N'] = Direction(-1, 0, 'E'); dire['E'] = Direction(0, 1, 'S'); dire['S'] = Direction(1, 0, 'W'); dire['W'] = Direction(0, -1, 'N');
que.push(Robot(sx, sy, 0, d));
memset(ans, 0x3f, sizeof(ans));
while (!que.empty()) { Robot r = que.front(); que.pop();
if (r.t>ans[r.x][r.y]) continue; else ans[r.x][r.y] = r.t;
for (rint i = 0, j = r.d; i<4; ++i, j = dire[j].nxt) { rint t = 1; if (i == 1 i == 3) t = 2; else if (i == 2) t = 3;
for (rint k = 1; k <= 3; ++k) { rint _x = r.x + dire[j].x*k, _y = r.y + dire[j].y*k; if (_x <= 0 _x >= N _y <= 0 _y >= M) break; if (a[_x][_y] a[_x - 1][_y] a[_x][_y - 1] a[_x - 1][_y - 1]) break; que.push(Robot(_x, _y, r.t + t, (char)j)); } } }
if (ans[ex][ey] != 0x3f3f3f3f) cout << ans[ex][ey]; else cout << -1; return 0; }
|