A diameter robot walks on an m * n lattice board and follows the go or turn command,
Go indicates 1-3 meters per second,
Turn refers to turning 90 degrees, taking one second,
How many seconds does it take to start from the start point to the end point?
Know the original direction
# Include <iostream> # include <queue> using namespace std; int n, m, a [55] [55], B [55] [55], sx, sy, ex, ey, d [4] [2] = {-}, {0,-1}; char c [10]; struct point {int x, y, I, time;} p; void bfs () {int I, j, r, k; point t; p. x = sx; p. y = sy; p. time = 0; B [sx] [sy] = 0; queue <point> q; q. push (p); while (! Q. empty () {p = q. front (); q. pop (); for (I = 0; I <4; I ++) {switch (I) {case 0: break; // The original direction is case 1: p. I = (p. i-1 + 4) % 4; p. time ++; break; // to the left case 2: p. I = (p. I + 2) % 4; break; // right case 3: p. I = (p. I + 1) % 4; p. time ++; // opposite} for (j = 1; j <4; j ++) {r = p. x + j * d [p. i] [0]; k = p. y + j * d [p. i] [1]; t. time = p. time + 1; if (r> = 1 & r <n & k> = 1 & k <m &&! A [r] [k] &! A [r] [k + 1] &! A [r + 1] [k] &! A [r + 1] [k + 1]) {if (B [r] [k]> t. time) B [r] [k] = t. time, t. x = r, t. y = k, t. I = p. i, q. push (t); // press} else break if it is earlier than the current time;} int main (int argc, char * argv []) {int I, j, k; while (cin> n> m & (n | m) {for (I = 1; I <= n; I ++) for (j = 1; j <= m; j ++) {cin> a [I] [j]; B [I] [j] = 1000000 ;} cin> sx> sy> ex> ey> c; if (c [0] = 'n') p. I = 0; if (c [0] = 'E') p. I = 1; if (c [0] = 's') p. I = 2; if (c [0] = 'W') p. I = 3; bfs (); if (B [ex] [ey] = 1000000) cout <-1 <endl; else cout <B [ex] [ey] <endl;} return 0 ;}
# Include <iostream> # include <queue> using namespace std; int n, m, a [55] [55], B [55] [55], sx, sy, ex, ey, d [4] [2] = {-}, {0,-1}; char c [10]; struct point {int x, y, I, time;} p; void bfs () {int I, j, r, k; point t; p. x = sx; p. y = sy; p. time = 0; B [sx] [sy] = 0; queue <point> q; q. push (p); while (! Q. empty () {p = q. front (); q. pop (); for (I = 0; I <4; I ++) {switch (I) {case 0: break; // The original direction is case 1: p. I = (p. i-1 + 4) % 4; p. time ++; break; // to the left case 2: p. I = (p. I + 2) % 4; break; // right case 3: p. I = (p. I + 1) % 4; p. time ++; // opposite} for (j = 1; j <4; j ++) {r = p. x + j * d [p. i] [0]; k = p. y + j * d [p. i] [1]; t. time = p. time + 1; if (r> = 1 & r <n & k> = 1 & k <m &&! A [r] [k] &! A [r] [k + 1] &! A [r + 1] [k] &! A [r + 1] [k + 1]) {if (B [r] [k]> t. time) B [r] [k] = t. time, t. x = r, t. y = k, t. I = p. i, q. push (t); // press} else break if it is earlier than the current time;} int main (int argc, char * argv []) {int I, j, k; while (cin> n> m & (n | m) {for (I = 1; I <= n; I ++) for (j = 1; j <= m; j ++) {cin> a [I] [j]; B [I] [j] = 1000000 ;} cin> sx> sy> ex> ey> c; if (c [0] = 'n') p. I = 0; if (c [0] = 'E') p. I = 1; if (c [0] = 's') p. I = 2; if (c [0] = 'W') p. I = 3; bfs (); if (B [ex] [ey] = 1000000) cout <-1 <endl; else cout <B [ex] [ey] <endl;} return 0 ;}