hdu 1175

来源:互联网 发布:stc单片机价格 编辑:程序博客网 时间:2024/05/29 02:43

#include<iostream>
#include<queue>
using namespace std;
struct node{
 int x,y;
 int time;
 int d;
};
int map[1010][1010];
int hash[1010][1010];
int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};
int m,n,sx,sy,dx,dy;
void bfs(int x,int y)
{
 memset(hash,3,sizeof(hash));
 queue<node>q;
 node N,P;
 int i;
 N.x = x;
 N.y = y;
 N.time = 0;
 N.d = 5;
 q.push(N);
 while(!q.empty())
 {
  N = q.front();
  q.pop();
  for(i = 0; i < 4; i++)
  {
   P.x = N.x + dir[i][0];
   P.y = N.y + dir[i][1];
   P.d = i;
   if(P.x <= n && P.y <= m &&P.x > 0 && P.y > 0 && (map[P.x][P.y] == 0 || P.x == dx && P.y == dy)&& N.time <= 2)
   {
    if(P.d == N.d || N.d == 5)
    {
     P.time = N.time ;
     
    }
    else
    {
     
     P.time = N.time + 1;
    
    }
    
    if( hash[P.x][P.y] >= P.time && P.time <= 2)
    {
     hash[P.x][P.y] = P.time;
     q.push(P);

    }
    
    
   }
  }
 }
 if(hash[dx][dy] <= 2)
 cout<<"YES"<<endl;
 else
 cout<<"NO"<<endl;
}
void main()
{
 int t,i,j;
 while(cin>>n>>m)
 {
  if(m == 0 && n == 0)
  continue;
  for(i = 1; i <= n; i++)
  for(j = 1; j <= m; j++)
  {
   cin>>map[i][j];
   //hash[i][j] = 3;
  }
  cin>>t;
  for(i = 0; i < t; i++)
  {
   cin>>sx>>sy>>dx>>dy;
   if(map[sx][sy] == map[dx][dy] && map[sx][sy] != 0 && map[dx][dy] != 0 &&!(sx == dx && sy == dy))
   {
    bfs(sx,sy);
   }
   else
   {
    cout<<"NO"<<endl;
    continue;
   }
  }
  //bfs(sx,sy);
 }
}

原创粉丝点击