hdu 1175 bfs

/*
 很纠结的题目,自己一开始写的代码就是wrong,一直改不出来,
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int x1, y1, x2, y2;
int n, m;
int  map[1002][1002];
int hash[1002][1002];
//bool hush[1002][1002];
int dir[4][2] = { {1, 0}, {-1, 0}, {0 , 1}, {0, -1} };
struct node
{
 int x;
 int y;
 int st; //标记转弯的次数
 int dir;//标记方向
};

int bfs()
{
 queue<node> q;
 node N, P;
 int i;
 int j;
 for(i = 1; i <= n; i++)
 for(j = 1; j <= m; j++)
 {
  //scanf("%d", &map[i][j]);
  hash[i][j] = 100;
  //hush[i][j] = false;
 }
 N.x = x1;
 N.y = y1;
 N.st = 0;
 N.dir = 0;
 q.push(N);
 //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.st = 0;
  P.dir = i;
  if(P.x < 1 || P.x > n || P.y < 1 || P.y > m)
    continue;
  if(!map[P.x][P.y] || map[P.x][P.y] == map[x2][y2])
  {
   hash[P.x][P.y] = 0;
   q.push(P);
   //hush[P.x][P.y] = true;
  }
 }
 q.pop();
 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.dir = i;
   P.st = N.st;
   if(P.x < 1 || P.x > n || P.y < 1 || P.y > m)
    continue;
   if(P.dir != N.dir)
   {
    P.st = N.st + 1;
   }
   if((!map[P.x][P.y] || map[P.x][P.y] == map[x2][y2]) && P.st <= 2 && P.st <= hash[P.x][P.y])
   {
    q.push(P);
    hash[P.x][P.y] = P.st;
    //hush[P.x][P.y] = true;
   }

  }
  if(hash[x2][y2] <= 2)
  {
   printf("YES/n");
   return 0;

  }
 }
 
 //else
 //{
  printf("NO/n");
  return 0;
 //}
}

int main()
{
 int i, j, t;
 while(scanf("%d%d", &n, &m) != EOF)
 {
  if(n == 0 && m == 0)
   continue;
  for(i = 1; i <= n; i++)
  for(j = 1; j <= m; j++)
  {
   scanf("%d", &map[i][j]);
   //hash[i][j] = 10;
  }
  scanf("%d", &t);
  while(t--)
  {
   scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
   if(map[x1][y1] == map[x2][y2] && map[x1][y1] && map[x2][y2] && !(x1 == x2 && y1 == y2))
   {
    bfs();
   }
   else
   {
    printf("NO/n");
   }
  }
 }
}
 后来看了下论坛,找参考以下的方法
*/

#include<iostream>//2425744 2010-05-06 23:07:59 Accepted 1175 156MS 6260K 1634 B G++ 悔惜晟
#include<queue>
#include<cstdio>
using namespace std;
int x1, y1, x2, y2;
int n, m;
int  map[1002][1002];
int hash[1002][1002];
int dir[4][2] = { {1, 0}, {-1, 0}, {0 , 1}, {0, -1} };
struct node
{
 int x;
 int y;
 int dir;//标记方向
};

int bfs()
{
 queue<node> q;
 node N, P;
 int i;
 int sum;
 N.x = x1;
 N.y = y1;
 N.dir = -1;
 q.push(N);
 memset(hash, 0, sizeof(hash));
 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.dir = i;
   if(N.dir != P.dir)
   {
    sum = hash[N.x][N.y] + 1;

   }
   else
   {
    sum = hash[N.x][N.y];
   }
   if(P.x >= 1 && P.x <= n && P.y >= 1 && P.y <= m && (sum <= hash[P.x][P.y]|| hash[P.x][P.y] == 0))//写了P.y <= n 找了下才找出来
   {
    if(P.x == x2 && P.y == y2 && map[P.x][P.y] == map[x2][y2])
    {

     hash[P.x][P.y] = sum;
     if(hash[P.x][P.y] <= 3)
     {
      puts("YES");
      return 1;
     }
    }
    if(map[P.x][P.y] == 0)
    {
     hash[P.x][P.y] = sum;
     if(hash[P.x][P.y] == 4)
      continue;
     q.push(P);
    }
   }
   

  }
 }
 puts("NO");
 return 1;
 
}


int main()
{
 int i, j, t;
 while(scanf("%d%d", &n, &m) != EOF)
 {
  if(n == 0 && m == 0)
   continue;
  for(i = 1; i <= n; i++)
  for(j = 1; j <= m; j++)
  {
   scanf("%d", &map[i][j]);
  }
  scanf("%d", &t);
  while(t--)
  {
   scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
   if(map[x1][y1] == map[x2][y2] && map[x1][y1] && map[x2][y2] && !(x1 == x2 && y1 == y2))
   {
    bfs();
   }
   else
   {
    printf("NO/n");
   }
  }
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值