http://acm.hdu.edu.cn/showproblem.php?pid=1728

逃离迷宫

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, 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
 
2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3
 
Sample Output
 
no
yes
 
思路:比较简单的BFS,WA了两次是一开始vis数组只记录了x和y的,没有考虑不同方向和不同转次数也可能走出不同的结果,这和上次训练赛一道题悟空救唐僧有点类似,都要用多维的vis数组去记录,以后要搜索要多考虑这种情况。
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. #define N 105
  7.  
  8. struct node
  9. {
  10. int x, y, dre, t;
  11. node () {}
  12. node (int x, int y, int dre, int t) : x(x), y(y), dre(dre), t(t) {}
  13. };
  14. char maze[N][N];
  15. bool vis[N][N][][];
  16. int dx[] = {, -, , }, dy[] = {, , , -};
  17. int sx, sy, ex, ey, k, n, m;
  18.  
  19. bool check(int x, int y)
  20. {
  21. if(<=x&&x<m&&<=y&&y<n&&maze[x][y]!='*') return true;
  22. return false;
  23. }
  24. //&&!vis[x][y]
  25. /*
  26. 2
  27. 5 5
  28. .....
  29. .*.*.
  30. .....
  31. .*.*.
  32. .....
  33. 1 1 1 3 4
  34. */
  35. bool bfs()
  36. {
  37. queue<node> que;
  38. memset(vis , ,sizeof(vis));
  39. que.push(node(sx, sy, -, ));
  40. while(!que.empty()) {
  41. node u = que.front(); que.pop();
  42. if(u.x == ex && u.y == ey && u.t <= k) return true;
  43. if(u.t > k) continue;
  44. if(u.dre != -) {
  45. if(vis[u.x][u.y][u.dre][u.t]) continue;
  46. vis[u.x][u.y][u.dre][u.t] = ;
  47. }
  48. for(int i = ; i < ; i++) {
  49. int nx = u.x + dx[i], ny = u.y + dy[i];
  50. if(!check(nx, ny)) continue;
  51. if(u.dre == -) {
  52. que.push(node(nx, ny, i, u.t));
  53. continue;
  54. }
  55. que.push(node(nx, ny, i, u.dre == i ? u.t : u.t + ));
  56. }
  57. }
  58. return false;
  59. }
  60.  
  61. int main()
  62. {
  63. int t;
  64. scanf("%d", &t);
  65. while(t--) {
  66. scanf("%d%d", &m, &n);
  67. for(int i = ; i < m; i++)
  68. scanf("%s", maze[i]);
  69. scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
  70. sy--, sx--, ey--, ex--;
  71. if(maze[sx][sy] == '*' || maze[ex][ey] == '*') {
  72. puts("no"); continue;
  73. }
  74. if(bfs()) puts("yes");
  75. else puts("no");
  76. }
  77. return ;
  78. }

HDU 1728:逃离迷宫(BFS)的更多相关文章

  1. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  2. hdu 1728 逃离迷宫 bfs记步数

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  3. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  4. hdu 1728 逃离迷宫 BFS加优先队列 DFS()

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...

  5. HDU 1728 逃离迷宫 BFS题

    题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...

  6. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  7. HDU 1728 逃离迷宫(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)  ...

  8. HDU 1728 逃离迷宫

    [题目描述 - Problem Description] 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,glo ...

  9. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. hdu 1728:逃离迷宫(DFS,剪枝)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 知识准备-JOIN/EXISTS

    10:40 2013-08-29 JOIN ON...AND A left join B on A.col1=B.col1 and A.col2=xx A left join B on A.col1= ...

  2. PLSQL 的简单命令之三

    -- 查找两个表中ID相等的 select a.id, a.name,b.math from stu a,scores b where a.id = b.id -- 右外连接 select b.id, ...

  3. Springfox Reference Documentation

    1. Introduction The Springfox suite of java libraries are all about automating the generation of mac ...

  4. 异常处理原则--good

    异常机制是现代主流语言的标配,但是异常处理问题虽然已经被讨论很多,也有很多经典书籍的论述,却一直都充满争议.很多人都觉得异常处理很难拿捏,同时也难以理解一些语言或库的异常处理设计.我使用Java近10 ...

  5. JAVA线程基础

    一.线程状态 由于参考的维度不一样,线程状态划分也不一样,我这里简单的分为5大类,并且会说明状态变迁的详细过程:

  6. PostgreSQL数据库中跨库访问解决方案

    PostgreSQL跨库访问有3种方法:Schema,dblink,postgres_fdw. 方法A:在PG上建立不同SCHEMA,将数据和存储过程分别放到不同的schema上,经过权限管理后进行访 ...

  7. WebDriver 随笔

    在webDriver中通过 driver.findElement进行定位元素时,往往是从页面的上到下依次寻找,根据该等位方式寻找到第一个元素. driver.findElement(By.id())有 ...

  8. struts自定义拦截器

    第01步:配置web.xml,启动struts框架 <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  9. haskell笔记1

    haskell platform下载:https://www.haskell.org/platform/ 进入haskell控制台,终端输入 $ ghci 编译文件 :l file.hs 数组操作 & ...

  10. 针对Android 模拟器启动慢的问题

    Android 模拟器一直以运行速度慢著称,可以使用intel HAXM技术为Andorid模拟器加速.使模拟器运行度媲美真机, 彻底解决模拟器运行慢的问题. 1. Intel HAXM 是什么 In ...