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

逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 18702    Accepted Submission(s): 4526

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
  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
 



做法:每次走一个方向就一直走究竟,路径上假设没訪问过的点,就入队。由于是bfs。每次转向数仅仅添加1,所以最先訪问,就是转向数最少的。

入队点假设再直走或者后退,那都是会訪问已经訪问的点,是没意义的。所以必会转向,所以转向数是+1的。


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <malloc.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. #include <string>
  9. #include <iostream>
  10. #include <algorithm>
  11. using namespace std;
  12. #include <stack>
  13. #include <queue>
  14. #include <vector>
  15. #include <deque>
  16. #include <set>
  17. #include <map>
  18. #define INF 999999999
  19. #define eps 0.00001
  20. #define LL __int64
  21. #define pi acos(-1.0)
  22.  
  23. struct point
  24. {
  25. int x,y;
  26. int step;//记录转弯数。
  27. };
  28. int vis[110][110];
  29. int n,m;
  30. int dir[4][2]={
  31. 1,0,
  32. -1,0,
  33. 0,1,
  34. 0,-1
  35. };
  36.  
  37. char mp[110][110];
  38. int ok(point nw)
  39. {
  40. if(nw.x>=0&&nw.x<n&&nw.y>=0&&nw.y<m&&mp[nw.x][nw.y]=='.')
  41. return 1;
  42. return 0;
  43. }
  44. int sx,sy,ex,ey;
  45.  
  46. int bfs()
  47. {
  48. memset(vis,0,sizeof vis);
  49. point sta,nw,nex;
  50. sta.x=sx;
  51. sta.y=sy;
  52. sta.step=-1;//第一次不算转弯
  53. queue<point>q;
  54. q.push(sta);
  55. while(!q.empty())
  56. {
  57. nw=q.front();
  58. if(nw.x==ex&&nw.y==ey)
  59. return max(0,nw.step);
  60. q.pop();
  61. for(int i=0;i<4;i++)
  62. {
  63. nex=nw;
  64. nex.x+=dir[i][0];
  65. nex.y+=dir[i][1];
  66. nex.step=nw.step+1;//由于是走到了尽头了。所以每一次
  67. //每次step仅仅加1,所以能够用bool vis
  68. while(ok(nex))
  69. {
  70. if(vis[nex.x][nex.y]==0)
  71. {
  72. q.push(nex);
  73. vis[nex.x][nex.y]=1;//停在这个点。
  74. }
  75. nex.x+=dir[i][0];
  76. nex.y+=dir[i][1];
  77. }
  78. }
  79. }
  80. return 999999;
  81. }
  82. int main()
  83. {
  84. int t;
  85. scanf("%d",&t);
  86. while(t--)
  87. {
  88. scanf("%d%d",&n,&m);
  89. for(int i=0;i<n;i++)
  90. scanf("%s",mp[i]);
  91. int k;
  92. scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);
  93. sy--,sx--,ey--,ex--;
  94. int ans=bfs();
  95. if(k>=ans)
  96. printf("yes\n");
  97. else
  98. printf("no\n");
  99. }
  100. return 0;
  101. }
  102. /*
  103. 2
  104. 5 5
  105. ...**
  106. *.**.
  107. .....
  108. .....
  109. *....
  110. 1 1 1 1 3
  111. 5 5
  112. ...**
  113. *.**.
  114. .....
  115. .....
  116. *....
  117. 2 1 1 1 3
  118. */



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)

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

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

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

  4. HDU 1728 逃离迷宫 BFS题

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

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

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

  6. HDU 1728 逃离迷宫(DFS)

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

  7. HDU 1728 逃离迷宫

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

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

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

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

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

随机推荐

  1. 滚动时sticky nav

    参考w3c <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...

  2. 浅谈css的行内类型标签和块级标签

    常用标签的行内类型标签有:a.span.img:块级标签有:div.p.h1~6.ul.ol.li.dl.dt.dd. 行内类型标签的特征:标签的大小由标签的内容决定,不能设置width和height ...

  3. vscode使用教程(web开发)

    1.安装 进入官网下载https://code.visualstudio.com/ 一直下一步就好了,中间可以选择把软件安装在哪个目录. 2.常用插件安装 a. 进入扩展视图界面安装/卸载 a1.快捷 ...

  4. Python多线程爬图&Scrapy框架爬图

    一.背景 对于日常Python爬虫由于效率问题,本次测试使用多线程和Scrapy框架来实现抓取斗图啦表情.由于IO操作不使用CPU,对于IO密集(磁盘IO/网络IO/人机交互IO)型适合用多线程,对于 ...

  5. Java屏幕截图及剪裁

    Java标准API中有个Robot类,该类可以实现屏幕截图,模拟鼠标键盘操作这些功能.这里只展示其屏幕截图. 截图的关键方法createScreenCapture(Rectangle rect) ,该 ...

  6. css中常见几种float方式以及倒计时(刷新页面不清)

    css中常见几种float方式 http://jingyan.baidu.com/article/72ee561a670269e16138dfd5.html <script type=" ...

  7. ThinkPHP---TP功能类之邮件

    [一]概论 (1)简介: 这里说的邮件不是平时说的email邮件(邮件地址带有@符号的),而是指的一般论坛网站的站内信息,也叫私信或者pm(private message私信) [二]站内信案例 (1 ...

  8. Java编辑编译及运行环境

    Java编辑编译及运行环境 Microsoft Windows 编辑工具 EditPlus JDK JDK(Java Development Kit,Java开发工具包)安装JDK之后,其中bin文件 ...

  9. BZOJ 2223: [Coci 2009]PATULJCI 主席树

    Code: #include<bits/stdc++.h> #define maxn 300001 #define mid ((l+r)>>1) using namespace ...

  10. Day 14B 网络应用开发

    网络应用开发 发送电子邮件 在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知.网站向用户发送一个激活账号的链接.银行向客户推广它们的理财产品等几乎都 ...