逃离迷宫 1728hdu bfs + dp

/*
一开始我以为是简单的bfs,可是后来百度后发现要bfs + dp
好奇怪,我用hash标记了已经走过的点,就是wrong,还是纠结的
但是后来感觉hash是错的,以为从一个点到另一个点有很多走法,
结果是要去到达该点最小步数的点,标记了就不可以走了
*/
 
#include<iostream> //2417041 2010-05-04 21:16:19 Accepted 1728 78MS 412K 2278 B C++ 悔惜晟
#include<queue>
using namespace std;

int x1, y1, x2, y2;
int n, m;
int k;
char map[105][105];
//bool hash[105][105];
 int hush[105][105];//标记最小转弯的次数
 int dir[4][2] ={ {0, 1}, {0, -1}, {1, 0}, {-1, 0} };//上 下 右 左
struct node
{
 int x;
 int y;
 int dir;//标记方向
 int st; //标记转弯的次数
};

int bfs(int si, int sj)
{
 node P, N;
 int i;
 queue<node> q;
 N.x = si;
 N.y = sj;
 if(si == y2 && sj == x2)
 {
  cout<<"yes"<<endl;
  return 1;
 }
 q.push(N);
 N = q.front();
 q.pop();
 hush[N.x][N.y] = 0;
 //hash[si][sj] = true;
 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 = 0;
  if(P.x >= 1 && P.x <= n && P.y >= 1 && P.y <= m && map[P.x][P.y] != '*')
  {
   
   q.push(P);
   //hash[P.x][P.y] = true;
   hush[P.x][P.y] = 0;
  }
 }
 while(!q.empty())
 {
  N = q.front();
  /*
  if(N.x == y2 && N.y == x2)
  if(hush[N.x][N.y] <= k)
  {
   cout<<"yes"<<endl;
   return 1;
  }
  else
  {
   cout<<"no"<<endl;
   return 1;
  }
  */
  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(P.x >= 1 && P.x <= n && P.y >= 1 && P.y <= m  && map[P.x][P.y] != '*')// && hush[N.x][N.y] <= k)
   {
    
    if(N.dir != P.dir )
    {
     P.st = N.st + 1;
     if(P.st <= k && P.st <= hush[P.x][P.y])
     {
      hush[P.x][P.y] = P.st;
      q.push(P);
      //hash[P.x][P.y] = true;
     }
     //hush[P.x][P.y] = hush[N.x][N.y] + 1;
    }
    else
    { P.st = N.st;
     if(P.st <= k && P.st <= hush[P.x][P.y])
     {
      hush[P.x][P.y] = P.st;
      q.push(P);
      //hash[P.x][P.y] = true;
     }
     //hush[P.x][P.y] = hush[N.x][N.y];
    }
    //q.push(P);
    //hash[P.x][P.y] = true;

   }
  }
 }
 if(hush[y2][x2] <= k)
 {
  cout<<"yes"<<endl;
  return 1;
 }
 else
 {
  cout<<"no"<<endl;
  return 1;
 }
 //return 1;
}
int main()
{
 int t;
 int i, j;
 cin>>t;
 while(t--)
 {
  cin>>n>>m;
  for(i = 1; i <= n; i++)
  for(j = 1; j <= m; j++)
  {
   cin>>map[i][j];
   hush[i][j] = 12;
  }
  cin>>k>>x1>>y1>>x2>>y2;
  //memset(hash, false, sizeof(hash));
  //memset(hush, 0, sizeof(hush));
  bfs(y1, x1);
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值