HDU 2653 Waiting ten thousand years for Love

举报
Linux猿 发表于 2021/08/05 23:38:15 2021/08/05
【摘要】 题目链接~~> 做题感悟:这题有点失败,没考虑到必须用三维标记,开始感觉用二维就可以的,但是后来看到别人用三维才考虑到这样有的点就遍历不到。 解题思路:BFS + 优先队列 :如果一个格子不是 @ 或者到达的格子也不为 @  那么就可以走过去 | | 飞过去,剩下的情况必须飞过去。(如果两个@相连且你从其中一个到达另一个只需要一个magic power 就...

题目链接~~>

做题感悟:这题有点失败,没考虑到必须用三维标记,开始感觉用二维就可以的,但是后来看到别人用三维才考虑到这样有的点就遍历不到。

解题思路:BFS + 优先队列 :如果一个格子不是 @ 或者到达的格子也不为 @  那么就可以走过去 | | 飞过去,剩下的情况必须飞过去。(如果两个@相连且你从其中一个到达另一个只需要一个magic power 就可以)。

代码:


  
  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 = 85 ;
  21. int n,m,tx,kx ;
  22. char s[MX][MX] ;
  23. bool vis[MX][MX][MX] ;
  24. int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1} ;
  25. struct node
  26. {
  27. int x,y,step,p ;
  28. friend bool operator < (const node &a,const node &b) // 优先队列
  29. {
  30. return a.step > b.step ;
  31. }
  32. } ;
  33. bool search(int x,int y)
  34. {
  35. if(x<0||y<0||x>=n||y>=m||s[x][y]=='#')
  36. return false;
  37. return true ;
  38. }
  39. int bfs(int x,int y,int kx)
  40. {
  41. int sx,sy ;
  42. priority_queue<node>q ;
  43. node curt,next ;
  44. memset(vis,false,sizeof(vis)) ;
  45. curt.x=x ;
  46. curt.y=y ;
  47. curt.step=0 ;
  48. curt.p=kx ;
  49. vis[x][y][kx]=true ;
  50. q.push(curt) ;
  51. while(!q.empty())
  52. {
  53. curt=q.top() ;
  54. q.pop() ;
  55. if(curt.step>tx) return -1 ; // 剪枝最少的时间都大于 tx so……
  56. if(s[curt.x][curt.y]=='L')
  57. return curt.step ;
  58. for(int i=0 ;i<4 ;i++)
  59. {
  60. next.x=sx=curt.x+dx[i] ;
  61. next.y=sy=curt.y+dy[i] ;
  62. next.p=curt.p ;
  63. if(search(sx,sy))
  64. {
  65. if(s[curt.x][curt.y]!='@'&&s[sx][sy]!='@'&&!vis[sx][sy][next.p]&&curt.step+2<=tx)// 走过去
  66. {
  67. next.step=curt.step+2 ;
  68. vis[sx][sy][next.p]=true ;
  69. q.push(next) ;
  70. }
  71. if(next.p>0&&!vis[sx][sy][next.p-1]&&curt.step+1<=tx) // 飞过去
  72. {
  73. next.p-- ;
  74. next.step=curt.step+1 ;
  75. vis[sx][sy][next.p]=true ;
  76. q.push(next) ;
  77. }
  78. }
  79. }
  80. }
  81. return -1 ;
  82. }
  83. int main()
  84. {
  85. int sx,sy,q=1 ;
  86. while(~scanf("%d%d%d%d",&n,&m,&tx,&kx))
  87. {
  88. for(int i=0 ;i<n ;i++)
  89. {
  90. scanf("%s",s[i]) ;
  91. for(int j=0 ;j<m ;j++)
  92. if(s[i][j]=='Y')
  93. {
  94. sx=i ;
  95. sy=j ;
  96. }
  97. }
  98. int mx=bfs(sx,sy,kx) ;
  99. printf("Case %d:\n",q++) ;
  100. if(mx!=-1)
  101. printf("Yes, Yifenfei will kill Lemon at %d sec.\n",mx) ;
  102. else
  103. printf("Poor Yifenfei, he has to wait another ten thousand years.\n") ;
  104. }
  105. return 0 ;
  106. }


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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