One morning, a whole morning, my God, there are a lot of errors
However, the idea is quite simple. BFS is used for this question. However, we cannot closely mark whether a vertex has been accessed. In this way, we direct wa because the accessed vertex does not meet the conditions, it is probably because there are too many turns, but there is a possibility that the path with less turns will access this point later, therefore, the VIS [] [] array is used to save the minimum number of turns that have accessed this point. In this way, the number of turns that have accessed this point is less than the minimum number of turns that have visited the store before, then you can join
I don't understand why MLE ......
# Include <iostream> # define maxn 1001 # include <queue> using namespace STD; int map [maxn] [maxn]; int vis [maxn] [maxn]; int N, m, Sx, Sy, ex, ey, Q; // int mov [4] [2] = {1, 0}, {-1, 0}, {0, -1 },{ 0, 1 }}; // The search sequence affects the result int dir [4] [2] ={{ 0, 1 },{ 1, 0 }, {0,-1}, {-1, 0 }}; struct node {int X; int y; int turn; int dir ;}; void BFS () {queue <node> q; node p, q; p. X = SX; p. y = sy; vis [SX] [sy] = 0; p. dir =-1; P. Turn = 0; q. Push (p); While (! Q. empty () {q = Q. front (); q. pop (); If (Q. X = ex & Q. y = ey) {cout <"yes" <Endl; return;} // 0 Lower, 1 upper, 2 left, 3 right for (INT I = 0; I <4; ++ I) {P. X = Q. X + dir [I] [0]; p. y = Q. Y + dir [I] [1]; p. turn = Q. turn; p. dir = Q. dir; If (Q. dir =-1) {P. dir = I; p. turn = 0;} else if (Q. dir! = I) {P. turn ++; p. dir = I;} If (P. x <1 | P. x> N | P. Y <1 | P. y> m) continue; If (Map [p. x] [p. y] &! (P. X = ex & P. y = ey) | P. turn> 2) continue; If (vis [p. x] [p. y]> P. turn) {vis [p. x] [p. y] = P. turn; q. push (p) ;}}cout <"no" <Endl ;}void res () {scanf ("% d", & Q); While (Q --) {scanf ("% d", & Sx, & Sy, & Ex, & ey); If (SX = ex & Sy = ey) {cout <"no" <Endl; continue;} If (Map [SX] [sy]! = Map [Ex] [ey] | map [SX] [sy] = 0 | map [Ex] [ey] = 0) {cout <"no" <Endl; continue;} For (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) {vis [I] [J] = int_max ;}} BFS () ;}} int main () {While (scanf ("% d", & N, & M) {If (n = 0 & M = 0) break; for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) {scanf ("% d", & map [I] [J]) ;}} res ();} return 0 ;}