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

注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是水平的还是垂直的。

并且我们需要知道当前到达楼梯这个点的方向,这样才知道下一个往哪个方向走,可以根据dir数组来判断方向。

楼梯不用判重。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. char field[][];
  8. int dir[][]={{,},{-,},{,},{,-}};
  9. int n,m;
  10. struct point
  11. {
  12. int x,y,time;
  13. bool operator < (const point a) const
  14. {
  15. return time>a.time;
  16. }
  17. };
  18. point s,e;
  19.  
  20. bool check(point t)
  21. {
  22. if(s.x>=&&s.x<n&&s.y>=&&s.y<m&&field[s.x][s.y]!='*')
  23. return true;
  24. return false;
  25. }
  26.  
  27. int bfs()
  28. {
  29. priority_queue<point>que;
  30. que.push(s);
  31. field[s.x][s.y]='*';
  32. while(que.size())
  33. {
  34. point t=que.top(); que.pop();
  35.  
  36. if(t.x==e.x&&t.y==e.y) return t.time;
  37. for(int i=;i<;i++)
  38. {
  39. s=t;
  40. s.x=t.x+dir[i][];
  41. s.y=t.y+dir[i][];
  42. if(check(s))
  43. {
  44. if(field[s.x][s.y]=='|')
  45. {
  46. //printf("%d %d %d %d %d\n",dir[i][0],dir[i][1],t.x,t.y,t.time);
  47. if(s.time%==) //偶数 那么 还是 '|'
  48. {
  49. if(dir[i][]!=) //如果是垂直方向只要1分钟就可以到达
  50. {
  51. s.x+=dir[i][];
  52. // printf("%d %d %d\n",s.x,s.y,s.time);
  53. if(!check(s)) continue;
  54. s.time+=;
  55. // printf("%d %d %d\n",s.x,s.y,s.time);
  56. field[s.x][s.y]='*';
  57. que.push(s);
  58. }
  59. else //是水平方向,就要等2秒 因为要等一秒
  60. {
  61. s.y+=dir[i][];
  62. if(!check(s)) continue;
  63. s.time+=;
  64. field[s.x][s.y]='*';
  65. que.push(s);
  66. }
  67. }
  68. else //奇数 那么变成了 '-' 下面同理
  69. {
  70. if(dir[i][]!=)
  71. {
  72. s.x+=dir[i][];
  73. // printf("%d %d %d\n",s.x,s.y,s.time);
  74. if(!check(s)) continue;
  75. s.time+=;
  76. // printf("%d %d %d\n",s.x,s.y,s.time);
  77. field[s.x][s.y]='*';
  78. que.push(s);
  79. }
  80. else
  81. {
  82. s.y+=dir[i][];
  83. if(!check(s)) continue;
  84. s.time+=;
  85. field[s.x][s.y]='*';
  86. que.push(s);
  87. }
  88. }
  89. }
  90. else if(field[s.x][s.y]=='-')
  91. {
  92. if(s.time%==)
  93. {
  94. if(dir[i][]!=)
  95. {
  96. s.x+=dir[i][];
  97. if(!check(s)) continue;
  98. s.time+=;
  99. field[s.x][s.y]='*';
  100. que.push(s);
  101. }
  102. else
  103. {
  104. s.y+=dir[i][];
  105. if(!check(s)) continue;
  106. s.time+=;
  107. field[s.x][s.y]='*';
  108. que.push(s);
  109. }
  110. }
  111. else
  112. {
  113. if(dir[i][]!=)
  114. {
  115. s.x+=dir[i][];
  116. if(!check(s)) continue;
  117. s.time+=;
  118. field[s.x][s.y]='*';
  119. que.push(s);
  120. }
  121. else
  122. {
  123. s.y+=dir[i][];
  124. if(!check(s)) continue;
  125. s.time+=;
  126. field[s.x][s.y]='*';
  127. que.push(s);
  128. }
  129. }
  130. }
  131. else
  132. {
  133. s.time+=;
  134. field[s.x][s.y]='*';
  135. que.push(s);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. int main()
  142. {
  143. //freopen("a.txt","r",stdin);
  144. while(~scanf("%d%d",&n,&m))
  145. {
  146. getchar();
  147. for(int i=;i<n;i++)
  148. {
  149. scanf("%s",field[i]);
  150. // printf("%s\n",field[i]);
  151. for(int j=;j<m;j++)
  152. {
  153. if(field[i][j]=='S')
  154. {
  155. s.x=i;
  156. s.y=j;
  157. s.time=;
  158. }
  159. else if(field[i][j]=='T')
  160. {
  161. e.x=i;
  162. e.y=j;
  163. }
  164. }
  165. }
  166. printf("%d\n",bfs());
  167. }
  168. return ;
  169. }

hdu - 1180 诡异的楼梯 (bfs+优先队列)的更多相关文章

  1. hdu 1180 诡异的楼梯 (bfs)

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

  2. hdu 1180诡异的楼梯(bfs)

    诡异的楼梯 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submis ...

  3. hdu 1180 诡异的楼梯(优先队列)

    Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.  比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向 ...

  4. HDU 1180 诡异的楼梯(超级经典的bfs之一,需多回顾)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)     ...

  5. HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】

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

  6. HDU 1180 诡异的楼梯(BFS)

    诡异的楼梯 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  7. hdu 1180:诡异的楼梯(BFS广搜)

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

  8. hdu 1180 诡异的楼梯

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

  9. HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)

    Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯在 ...

随机推荐

  1. SqlServer 临时表、表变量、函数 替代游标

    http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html 临时表 存放在tempdb中 --存储过程中将多表连接结果写入到临时表中, ...

  2. dblink应用

    当我们要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中就必须要创建远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据. 用法例子: (1) ...

  3. BLUR

    ssao的blur遇到个麻烦 花了两三天时间...终于大概知道原因了. 在nvidia的ssao(http://developer.download.nvidia.com/SDK/10.5/direc ...

  4. C#中Config文件中,特殊符号的书写方法。

    App.config: 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration& ...

  5. 单片机模拟 1/2 Bias、1/4 Duty的 LCD 驱动使用方法

    工作原理 方式一     根据 LCD 的驱动原理可知,LCD 像素点上只能加上 AC 电压,LCD 显示器的对比度由 COM脚上的电压值减去 SEG 脚上的电压值决定,当这个电压差大于 LCD 的饱 ...

  6. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  7. hdu 1800 Flying to the Mars(简单模拟,string,字符串)

    题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...

  8. 对MySQL DELETE语法的详细解析

    以下的文章主要描述的是MySQL DELETE语法的详细解析,首先我们是从单表语法与多表语法的示例开始的,假如你对MySQL DELETE语法的相关内容十分感兴趣的话,你就可以浏览以下的文章对其有个更 ...

  9. POJ 1279 Art Gallery(半平面交求多边形核的面积)

    题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...

  10. (.iso)光盘镜像文件的打开与安装

    直接解压就可以打开,然后就可以安装.exe文件