HDU 2364 Escape

举报
Linux猿 发表于 2021/08/05 22:52:17 2021/08/05
【摘要】 题目链接~~> 做题感悟:这种题只要将方向作为第三维标记即可,但是这题因为漏想了一个地方导致改了老半天。 解题思路:只要将方向作为第三维标记,还要注意如果左右能走就不走前,只有左右都为石头时才走前,且不能反方向走(可以走走过的路)。 代码: #include<stdio.h>#include<iostream>#include<ma...

题目链接~~>

做题感悟:这种题只要将方向作为第三维标记即可,但是这题因为漏想了一个地方导致改了老半天。

解题思路:只要将方向作为第三维标记,还要注意如果左右能走就不走前,只有左右都为石头时才走前,且不能反方向走(可以走走过的路)。

代码:


  
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<map>
  4. #include<stack>
  5. #include<string>
  6. #include<string.h>
  7. #include<stdlib.h>
  8. #include<math.h>
  9. #include<vector>
  10. #include<queue>
  11. #include<algorithm>
  12. using namespace std ;
  13. #define LEN sizeof(struct node)
  14. #define pret(a,b) memset(a,b,sizeof(a))
  15. #define lld __int64
  16. const double PI = 3.1415926 ;
  17. const double INF = 999999999 ;
  18. const double esp = 1e-4 ;
  19. const lld md= 2810778 ;
  20. const int MX = 81 ;
  21. int n,m ;
  22. char s[MX][MX] ;
  23. bool vis[MX][MX][4] ;
  24. int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1} ; // 下 0 上 1 右 2 左 3
  25. int dr[4][4]={{1,0,2},{0,1,3},{2,3,0},{3,2,1}} ;// 东 西 南 北 0 1 2 3
  26. int dc[4][4]={{3,2,0},{2,3,1},{0,1,2},{1,0,3}} ; // 走完后对应的方向
  27. struct node
  28. {
  29. int x,y,step,cnt ;
  30. } ;
  31. bool search(int x,int y)
  32. {
  33. if(x<0||x>=n||y<0||y>=m||s[x][y]=='#')
  34. return false ;
  35. return true ;
  36. }
  37. int bfs(int x,int y)
  38. {
  39. int sx,sy,cnt ;
  40. queue<node>q ;
  41. node curt,next ;
  42. memset(vis,false,sizeof(vis)) ;
  43. curt.x=x ;
  44. curt.y=y ;
  45. curt.step=0 ;
  46. for(int i=0 ;i<4 ;i++) // 初始化四个方向
  47. {
  48. curt.cnt=i ;
  49. vis[x][y][i]=true ;
  50. q.push(curt) ;
  51. }
  52. while(!q.empty())
  53. {
  54. curt=q.front() ;
  55. q.pop() ;
  56. x=curt.x ; y=curt.y ;
  57. cnt=curt.cnt ; // cnt 方向
  58. if(!x||!y||x==n-1||y==m-1)
  59. return curt.step ;
  60. bool fx=false ;
  61. for(int i=0 ;i<3 ;i++)
  62. {
  63. int z=dr[cnt][i] ;
  64. next.x=sx=x+dx[z] ;
  65. next.y=sy=y+dy[z] ;
  66. next.step=curt.step+1 ;
  67. if(search(sx,sy)&&(!fx||i<2))
  68. {
  69. int mx=dc[cnt][i] ; // 方向
  70. if(!vis[sx][sy][mx])
  71. {
  72. next.cnt=mx ;
  73. vis[sx][sy][mx]=true ;
  74. q.push(next) ;
  75. fx=true ;
  76. }
  77. else fx=true ; // fx 标记左右是否可走,开始忘记了这个!!!
  78. }
  79. }
  80. }
  81. return -1 ;
  82. }
  83. int main()
  84. {
  85. int Tx,sx,sy ;
  86. scanf("%d",&Tx) ;
  87. while(Tx--)
  88. {
  89. scanf("%d%d",&n,&m) ;
  90. for(int i=0 ;i<n ;i++)
  91. {
  92. scanf("%s",s[i]) ;
  93. for(int j=0 ;j<m ;j++)
  94. {
  95. if(s[i][j]=='@') //记录起始位置
  96. {
  97. sx=i ;
  98. sy=j ;
  99. }
  100. }
  101. }
  102. printf("%d\n",bfs(sx,sy)) ;
  103. }
  104. return 0 ;
  105. }


 

文章来源: blog.csdn.net,作者:Linux猿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nyist_zxp/article/details/23263143

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。