这题忘记每次侧的时候初始化mark WA了无数次。。。。说多了都是泪
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct{
int x, y;
int derect;
int num;
}Point;
int moves[][2] = { -1, 0, 0, 1, 1, 0, 0, -1 };
int mark[1100][1100];
int map[1100][1100];
int beginx, beginy, endx, endy, MAX;
int N, M;
void BFS(){
if( map[beginx][beginy] == 0 || map[endx][endy] == 0 || map[beginx][beginy] != map[endx][endy] ){
cout << "NO" << endl;
return;
}
Point begin;
begin.x = beginx;
begin.y = beginy;
begin.derect = -1;
begin.num = -1;
queue<Point> q;
q.push( begin );
while( !q.empty() ){
Point p;
p = q.front();
q.pop();
for( int i = 0; i < 4; i++ ){
Point temp;
temp.x = p.x + moves[i][0];
temp.y = p.y + moves[i][1];
temp.derect = i;
if( temp.derect != p.derect ){
temp.num = p.num + 1;
}else{
temp.num = p.num;
}
if( temp.x == endx && temp.y == endy && temp.num <= MAX ){
cout << "YES" << endl;
return;
}
if( temp.x < 1 || temp.x > M || temp.y < 1 || temp.y > N || temp.num > MAX || map[temp.x][temp.y] != 0 ){
continue;
}
if( temp.num < mark[temp.x][temp.y] ){//注意这里有个等于号,因为虽然拐弯数相同,但方向可能不同!!!!
mark[temp.x][temp.y] = temp.num;
q.push( temp );
}
}
}
cout << "NO" << endl;
}
int main(){
int t;
while( scanf( "%d%d", &M, &N ) && M && N ){
memset( map, 0, sizeof( map ) );
for( int i = 1; i <= M; i++ ){
for( int j = 1; j <= N; j++ ){
cin >> map[i][j];
mark[i][j] = 3;
}
}
cin >> t;
while( t-- ){
memset( mark, 127, sizeof( mark ) );
cin >> beginx >> beginy >> endx >> endy;
MAX = 2;
BFS();
}
}
return 0;
}