HDU 1728(BFS)

约定不等于承诺〃 2022-06-08 12:25 305阅读 0赞

问题描述:

给定一个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, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。

Output

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

Sample Input

  1. 2
  2. 5 5
  3. ...**
  4. *.**.
  5. .....
  6. .....
  7. *....
  8. 1 1 1 1 3
  9. 5 5
  10. ...**
  11. *.**.
  12. .....
  13. .....
  14. *....
  15. 2 1 1 1 3

Sample Output

  1. no
  2. yes

题目题意:题目给我们一个图,问我们能否在转弯不大于K次到达终点(第一次的方向随意)

题目分析:BFS 我们需要在结构体加入记录拐弯次数的step,然后每一次走到不能走到的时候(每一个方向下面,写一个循环),再拐弯。

这个题目的输入有变化,和我们一般思维不同,输入的是y x 坐标。

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<queue>
  7. using namespace std;
  8. const int maxn=110;
  9. struct note
  10. {
  11. int x,y;
  12. int step;
  13. bool operator==(const note &a) const {
  14. return x==a.x&&y==a.y;
  15. }
  16. }s,e;
  17. queue<struct note> que;
  18. char mmap[maxn][maxn];
  19. char vis[maxn][maxn];
  20. bool flag;
  21. int n,m,k,dx,dy;
  22. int Next[4][2]={
  23. {-1,0},{1,0},{0,-1},{0,1}};
  24. void bfs()
  25. {
  26. while (que.size()) que.pop();
  27. s.step=0;
  28. que.push(s);
  29. vis[s.x][s.y]=true;
  30. while (que.size()) {
  31. struct note cur,next;
  32. cur=que.front();
  33. que.pop();
  34. if (cur==e&&cur.step<=k+1) { flag=true; return; }//因为第一次方向任意,可以相当于多拐一次
  35. for (int i=0;i<4;i++) {
  36. dx=cur.x+Next[i][0];
  37. dy=cur.y+Next[i][1];
  38. next.step=cur.step+1;
  39. while (dx>=0&&dx<n&&dy>=0&&dy<m&&mmap[dx][dy]=='.') {//每一次走到尽头,这里面的是方向朝一个方向的
  40. if (!vis[dx][dy]) {//注意这个不能加到循环里面判断,因为可以重复走以前的地方
  41. vis[dx][dy]=true;
  42. next.x=dx,next.y=dy;
  43. if (next==e&&next.step<=k+1) { flag=true; return;}
  44. que.push(next);
  45. }
  46. dx+=Next[i][0];
  47. dy+=Next[i][1];
  48. }
  49. }
  50. }
  51. }
  52. int main()
  53. {
  54. int t;
  55. scanf("%d",&t);
  56. while (t--) {
  57. scanf("%d%d",&n,&m);
  58. memset (mmap,0,sizeof (mmap));
  59. for (int i=0;i<n;i++)
  60. scanf("%s",mmap[i]);
  61. scanf("%d%d%d%d%d",&k,&s.y,&s.x,&e.y,&e.x);
  62. s.x--,s.y--,e.x--,e.y--;//我的图是从0开始的,所以--
  63. memset (vis,false,sizeof (vis));
  64. flag=false;
  65. bfs();
  66. if (flag)
  67. printf("yes\n");
  68. else
  69. printf("no\n");
  70. }
  71. return 0;
  72. }

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1240(bfs)

    题意:给出一个三维空间,宇宙飞船的起始地点,目标地点,求最短飞行距离。   include <iostream> include <str

    相关 HDU 1728(BFS)

    问题描述: 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些