HDU 1728逃离迷宫(DFS或者BFS)

本文介绍了一道关于迷宫寻路的问题,通过限定转弯次数来寻找从起点到终点的路径。提供了两种解决方法:深度优先搜索(DFS)与广度优先搜索(BFS),并附带详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12273    Accepted Submission(s): 2964


Problem Description
  给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
 

Input
  第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
  第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x 1, y 1, x 2, y 2 (1 ≤ k ≤ 10, 1 ≤ x 1, x 2 ≤ n, 1 ≤ y 1, y 2 ≤ m),其中k表示gloria最多能转的弯数,(x 1, y 1), (x 2, y 2)表示两个位置,其中x 1,x 2对应列,y 1, y 2对应行。
 

Output
  每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
 

Sample Input
   
2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
 

Sample Output
   
no yes
 


DFS代码:
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<string>  
  5. using namespace std;  
  6. int m, n, ex, ey, k, p[105][105];  
  7. //p[x][y]代表某位置  
  8. char map1[105][105];  
  9. int x_move[4] = {-1, 0, 1, 0};  
  10. int y_move[4] = {0, 1, 0, -1};  
  11. bool flag;  
  12.   
  13. void dfs (int x, int y, int dir) //dir为当前方向  
  14. {  
  15.     if (x == ex && y == ey)  
  16.     {  
  17.         if (p[x][y] <= k)  
  18.             flag = true;  
  19.         return ;  
  20.     }  
  21.     //x !=ex && y != ey 说明必须至少再转一次弯,但是已经不能再转了  
  22.     if ((x !=ex && y != ey && p[x][y] == k)||p[x][y]>k)  
  23.         return ;  
  24.     for (int i = 0; i < 4; i++)  
  25.     {  
  26.         int tx = x + x_move[i];  
  27.         int ty = y + y_move[i];  
  28.         if (tx < 0 || tx >= m|| ty < 0 || ty >= n)  //判断越界  
  29.             continue;  
  30.         //转弯数相等不可剪掉  
  31.         if (map1[tx][ty] == '*' || p[tx][ty] < p[x][y])  //说明已经搜过或不可到达  
  32.             continue;  
  33.         if (dir != -1 && i != dir && p[tx][ty] == p[x][y] )  
  34.             continue;          //需要剪枝  
  35.         if (dir != -1 && i != dir)  
  36.             p[tx][ty]=p[x][y]+1; //如果方向变了转弯+1  
  37.         else p[tx][ty] = p[x][y];  
  38.         dfs (tx, ty, i);  
  39.         if (flag) return ;  
  40.     }  
  41. }  
  42.   
  43. int main()  
  44. {  
  45.     int t,i,j,sx,sy; //sx, sy是起点  
  46.     scanf ("%d", &t);  
  47.     while (t--)  
  48.     {  
  49.         scanf("%d%d",&m,&n);  
  50.         for(i=0;i<m;i++)  
  51.             scanf ("%s",map1[i]);  
  52.         scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);  
  53.         sx--,sy--,ex--,ey--;  //从0开始编号,而题目是从1开始  
  54.         for (i = 0; i < m; i++)  
  55.             for (j = 0; j < n; j++)  
  56.                 p[i][j] = 10005; //初始化转弯数为无穷大  
  57.         p[sx][sy] = 0; //到达起点的转弯数  
  58.         flag = false;  
  59.         dfs (sx, sy, -1); //一开始可以走任意方向,所以设方向为-1  
  60.         if (flag) puts ("yes");  
  61.         else puts ("no");  
  62.     }  
  63.     return 0;  
  64. }  


BFS代码:
  1. #include<iostream>  
  2. #include<cstring>  
  3. #include<cstdio>  
  4. #include<map>  
  5. #include<string>  
  6. #include<queue>  
  7. using namespace std;  
  8. int enx,eny,m,n;  
  9. char mp[105][105];  
  10. int mstep;  
  11. int flag;  
  12. int visi[105][105];  
  13. int q[105][105];  
  14.   
  15. int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};  
  16. struct node  
  17. {  
  18.     int x;  
  19.     int y;  
  20. }nod;  
  21.   
  22. void bfs(node p)  
  23. {  
  24.     queue<node> mq;  
  25.     mq.push(p);  
  26.     node cur,pp;  
  27.     int i;  
  28.   
  29.     while(!mq.empty())  
  30.     {  
  31.         cur=mq.front();  
  32.         mq.pop();  
  33.         if(q[cur.x][cur.y]>=mstep) break;  
  34.   
  35.         int px,py;  
  36.         for(i=0;i<4;i++)  
  37.         {  
  38.             px=cur.x+dir[i][0],py=cur.y+dir[i][1];  
  39.             while(px>=0&&px<m&&py>=0&&py<n)  
  40.             {  
  41.                 if(mp[px][py]=='*')  
  42.                     break;  
  43.                 if(!visi[px][py])  
  44.                 {  
  45.                     visi[px][py]=1;  
  46.                     pp.x=px,pp.y=py;  
  47.                     q[px][py]=q[cur.x][cur.y]+1;  
  48.                     mq.push(pp);  
  49.                 }  
  50.                 px+=dir[i][0],py+=dir[i][1];  
  51.             }  
  52.         }  
  53.     }  
  54. }  
  55.   
  56. int main()  
  57. {  
  58.      int stx,sty;  
  59.      int tes,i,j;  
  60.      cin>>tes;  
  61.      while(tes--)  
  62.      {  
  63.          flag=0;  
  64.          cin>>m>>n;  
  65.          for(i=0;i<m;i++)  
  66.             cin>>mp[i];  
  67.          cin>>mstep;  
  68.          cin>>stx>>sty>>enx>>eny;  
  69.          stx--,sty--,enx--,eny--;  
  70.          swap(stx,sty); swap(enx,eny);  
  71.          node p;  
  72.          p.x=stx,p.y=sty;  
  73.          memset(visi,0,sizeof(visi));  
  74.          for(i=0;i<m;i++)  
  75.             for(j=0;j<n;j++)  
  76.                 q[i][j]=1e8;  
  77.          visi[p.x][p.y]=1;  
  78.          q[p.x][p.y]=-1;  
  79.          bfs(p);  
  80.   
  81.          if(q[enx][eny]<=mstep) flag=1;  
  82.          /*for(i=0;i<m;i++) 
  83.          { 
  84.             for(j=0;j<n;j++) 
  85.                 cout<<q[i][j]<<" "; 
  86.             cout<<endl; 
  87.          } 
  88.          cout<<mstep<<endl;*/  
  89.   
  90.          if(flag) puts("yes");  
  91.          else puts("no");  
  92.      }  
  93.      return 0;  
  94. }  
  95.   
  96. // 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值