bfs
思路:三维标记状态 && 处理好 - | 和时刻的关系即可

  1. /*
  2. bfs
  3. 思路:三维标记状态 && 处理好 - | 和时刻的关系即可
  4. */
  5. #include<algorithm>
  6. #include<iostream>
  7. #include<string.h>
  8. #include<stdlib.h>
  9. #include<stdio.h>
  10. #include<math.h>
  11. #include<queue>
  12. #include<stack>
  13. #include<map>
  14. #include<set>
  15. using namespace std;
  16. typedef long long int64;
  17. //typedef __int64 int64;
  18. typedef pair<int64,int64> PII;
  19. #define MP(a,b) make_pair((a),(b))
  20. const int inf = 0x3f3f3f3f;
  21. const double pi=acos(-1.0);
  22. const int dx[]={,-,,};
  23. const int dy[]={,,,-};
  24. const double eps = 1e-;
  25. const int maxm = ;
  26. const int maxn = ;
  27.  
  28. struct Point {
  29. int x,y,t;
  30. };
  31. Point s,e;
  32.  
  33. bool vis[ maxn ][ maxn ][ ];
  34. char mat[ maxn ][ maxn ];
  35. queue<Point>q;
  36.  
  37. int bfs( int n,int m ){
  38. memset( vis,false,sizeof( vis ) );
  39. while( !q.empty() )
  40. q.pop();
  41. Point cur = s;
  42. vis[ s.x ][ s.y ][ s.t% ] = true;
  43. q.push( cur );
  44. while( !q.empty() ){
  45. cur = q.front();
  46. q.pop();
  47. //printf("\ncur:[%d,%d] t = %d\n",cur.x,cur.y,cur.t);
  48. if( cur.x==e.x && cur.y==e.y ){
  49. e.t = min( e.t,cur.t );
  50. }
  51. if( cur.t>=e.t ) continue;
  52. for( int i=;i<;i++ ){
  53. Point nxt;
  54. nxt.x = cur.x + dx[ i ];
  55. nxt.y = cur.y + dy[ i ];
  56. nxt.t = cur.t + ;
  57. if( nxt.x<||nxt.x>=n||nxt.y<||nxt.y>=m ) continue;
  58. if( mat[ nxt.x ][ nxt.y ]=='*' ) continue;
  59. if( mat[ nxt.x ][ nxt.y ]=='.' ){
  60. if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==true ) continue;
  61. vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
  62. q.push( nxt );
  63. //printf("0 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
  64. continue;
  65. }//
  66. nxt.x = cur.x + dx[ i ];
  67. nxt.y = cur.y + dy[ i ];
  68. nxt.t = cur.t + ;
  69. if(( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )){
  70. if( cur.x==nxt.x&&nxt.y>cur.y ) nxt.y = cur.y + ;
  71. if( cur.x==nxt.x&&nxt.y<cur.y ) nxt.y = cur.y - ;
  72. if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
  73. if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
  74. vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
  75. q.push( nxt );
  76. //printf("1 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
  77. continue;
  78. }
  79. }
  80. }//1 ‘-’ 上下无需等待时间
  81. nxt.x = cur.x + dx[ i ];
  82. nxt.y = cur.y + dy[ i ];
  83. nxt.t = cur.t + ;
  84. /*恢复nxt很重要*/
  85. if(( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )){
  86. nxt.t = cur.t + ;
  87. if( cur.x==nxt.x&&nxt.y>cur.y ) nxt.y = cur.y + ;
  88. if( cur.x==nxt.x&&nxt.y<cur.y ) nxt.y = cur.y - ;
  89. if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
  90. if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
  91. vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
  92. q.push( nxt );
  93. //printf("2 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
  94. continue;
  95. }
  96. }
  97. }//2 ‘-’ 上下需要等待时间
  98. nxt.x = cur.x + dx[ i ];
  99. nxt.y = cur.y + dy[ i ];
  100. nxt.t = cur.t + ;
  101. /*恢复nxt很重要*/
  102. if(( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )){
  103. if( cur.y==nxt.y&&nxt.x>cur.x ) nxt.x = cur.x + ;
  104. if( cur.y==nxt.y&&nxt.x<cur.x ) nxt.x = cur.x - ;
  105. if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
  106. if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
  107. vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
  108. q.push( nxt );
  109. //printf("3 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
  110. continue;
  111. }
  112. }
  113. }//3 ‘|’ 左右无需等待时间
  114. nxt.x = cur.x + dx[ i ];
  115. nxt.y = cur.y + dy[ i ];
  116. nxt.t = cur.t + ;
  117. /*恢复nxt很重要*/
  118. if(( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )){
  119. nxt.t = cur.t + ;
  120. if( cur.y==nxt.y&&nxt.x>cur.x ) nxt.x = cur.x + ;
  121. if( cur.y==nxt.y&&nxt.x<cur.x ) nxt.x = cur.x - ;
  122. if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
  123. if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
  124. vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
  125. q.push( nxt );
  126. //printf("4 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
  127. continue;
  128. }
  129. }
  130. }//4 ‘|’ 左右需要等待时间
  131. }
  132. }
  133. return e.t;
  134. }
  135.  
  136. int main(){
  137. int n,m;
  138. //freopen( "in.txt","r",stdin );
  139. while( scanf("%d%d",&n,&m)== ){
  140. for( int i=;i<n;i++ ){
  141. scanf("%s",mat[ i ]);
  142. for( int j=;j<m;j++ ){
  143. if( mat[ i ][ j ]=='S' ){
  144. s.x = i;
  145. s.y = j;
  146. s.t = ;
  147. mat[ i ][ j ] = '.';
  148. }
  149. else if( mat[ i ][ j ]=='T' ){
  150. e.x = i;
  151. e.y = j;
  152. e.t = inf;
  153. mat[ i ][ j ] = '.';
  154. }
  155. }
  156. }
  157. printf("%d\n",bfs( n,m ));
  158. }
  159. return ;
  160. }

HDU1180+BFS的更多相关文章

  1. 诡异的楼梯(bfs)hdu1180

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submiss ...

  2. HDU1180:诡异的楼梯(bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里, ...

  3. hdu1180 优先队列bfs+判断方向

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  4. hdu1180奇怪的楼梯……bfs迷阵……wa该16二级,我太渣滓

    #include<iostream> #include<queue> #include<cstring> using namespace std; int row, ...

  5. hdu1180 诡异的楼梯 bfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1180/ 题目和不同的bfs有个不同的地方就是存在横着的或者竖着的楼梯,楼梯每过一个时刻就改变一次横竖的走向,人可 ...

  6. BFS的小结

    写这类搜索题.首先感觉要有个框架.比如我的框架对于BFS来说(对于DFS,我想有两个一个是递归版一个是栈版).这里是BFS小结.所以介绍一下BFS.我的框架.(也是搜集了网上许多神人的作品.) 1:节 ...

  7. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  8. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  9. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

随机推荐

  1. swift基础知识学习

    用let来声明常量,用var来声明变量   声明变量: var x = 0.0, y = 0.0, z = 0.0 var welcomeMessage: String 常量和变量的命名 你可以用任何 ...

  2. 用css3做标签

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 函数 swap

    1,default swap namespace std { template<typename T> void swap( T& a, T& b) { T temp(a) ...

  4. fedora下python3 安装tkinter和pygame

    root 下用 “yum search tkinter”,如下图所示: 然后再根据系统选择安装就好了. tkinter安装完毕. 接下来是安装pygame在fedora的python3上,我的是pyt ...

  5. c#汉字转为拼音

    using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...

  6. grails的插件

    今天来歪理邪说一下grails的插件. 有个问题让本人困惑了一段时间,插件是属于grails的,还是属于某个工程的?为什么会有这个问题呢,这涉及到grails插件的安装方式. grails的插件像是一 ...

  7. 认识HTML5

    引言,认识两个标准制定的组织 在讲什么是Html5之前得先了解两个组织:WHATWG :网页超文本技术工作小组(英语:Web Hypertext Application Technology Work ...

  8. T-SQL实例 函数结果设置为列别名

    本文分享一个T-SQL的例子,将自定义函数的结果作为别名列,是个不错的应用实例,有兴趣的朋友研究下. T-SQL实例,学习下将函数结果作为别名列的方法. 代码: view source print? ...

  9. C# 返回json结果集,js日期格式化

    asp.net mvc返回json结果集 return Json(new { total = totalCount, rows = result }, JsonRequestBehavior.Allo ...

  10. 《WPF程序设计指南》读书笔记——第7章 Canvas

    1.Canvas面板 using System; using System.Windows; using System.Windows.Controls; using System.Windows.M ...