hdu 1175

#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);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值