HDU 5040 Instrusive

举报
Linux猿 发表于 2021/08/05 01:47:47 2021/08/05
【摘要】 题目链接~~> 做题感悟:这题在网络赛的时候没有解出来,当时最后才写的有点慌了,so~>思路一点不清楚。 解题思路:               这题首先弄清楚在走每一步的时候是人先走,还是摄像头先走,当然是人先走,如果一个人从 A ---> B ,那么需要判断前一秒A 和 B 是...

题目链接~~>

做题感悟:这题在网络赛的时候没有解出来,当时最后才写的有点慌了,so~>思路一点不清楚。

解题思路:

              这题首先弄清楚在走每一步的时候是人先走,还是摄像头先走,当然是人先走,如果一个人从 A ---> B ,那么需要判断前一秒A 和 B 是否被摄像头照到,因为人先走,如果曾被摄像头找到,那么走过去会被发现,这样可以选择等一秒,下一秒同样再判断一次,如果还是不可以,就需要带着箱子走。还要注意时间第一秒时摄像头转到先一个方向。因为时间不是都加 1 ,需要用优先队列,同时一个点可以去多次,需要用时间标记所为第三维。

代码:


  
  1. #include<iostream>
  2. #include<sstream>
  3. #include<map>
  4. #include<cmath>
  5. #include<fstream>
  6. #include<queue>
  7. #include<vector>
  8. #include<sstream>
  9. #include<cstring>
  10. #include<cstdio>
  11. #include<stack>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<string>
  15. #include<cctype>
  16. #include<iomanip>
  17. #include<algorithm>
  18. using namespace std ;
  19. #define INT long long int
  20. const int INF = 0x3f3f3f ;
  21. const double esp = 0.0000000001 ;
  22. const double PI = acos(-1.0) ;
  23. const INT mod = 1000000007 ;
  24. const int MY = 100 + 5 ;
  25. const int MX = 500 + 5 ;
  26. int n ,ex ,ey ;
  27. char s[MX][MX] ;
  28. bool dir[MX][MX][4] ,vis[MX][MX][4] ;
  29. int dx[4] = {-1 ,0 ,1 ,0} ,dy[4] = {0 ,1 ,0 ,-1} ;
  30. struct node
  31. {
  32. int x ,y ,step ;
  33. friend bool operator < (const node& a ,const node& b)
  34. {
  35. return a.step > b.step ;
  36. }
  37. } ;
  38. int bfs(int x ,int y) // 人先走,摄像头再走 !!!!!
  39. {
  40. int sx ,sy ,step ;
  41. memset(vis ,false ,sizeof(vis)) ;
  42. priority_queue<node> q ;
  43. node curt ,next ;
  44. curt.x = x ;
  45. curt.y = y ;
  46. curt.step = 0 ;
  47. vis[x][y][0] = true ;
  48. q.push(curt) ;
  49. while(!q.empty())
  50. {
  51. curt = q.top() ;
  52. q.pop() ;
  53. if(ex == curt.x && ey == curt.y)
  54. return curt.step ;
  55. for(int i = 0 ;i < 4 ; ++i)
  56. {
  57. next.x = sx = curt.x + dx[i] ;
  58. next.y = sy = curt.y + dy[i] ;
  59. next.step = step = curt.step + 1 ;
  60. if(sx >= 0 && sx < n && sy >= 0 && sy < n && s[sx][sy] != '#') // 判断出界
  61. {
  62. if(dir[sx][sy][curt.step%4]|| dir[curt.x][curt.y][curt.step%4]) // 判断当前点前一秒 和 下一个点前一秒是否曾被照到
  63. {
  64. if(!dir[sx][sy][step%4] && !dir[curt.x][curt.y][step%4])
  65. {
  66. next.step = curt.step + 2 ;
  67. if(!vis[sx][sy][next.step%4])
  68. {
  69. vis[sx][sy][next.step%4] = true ;
  70. q.push(next) ;
  71. }
  72. }
  73. else
  74. {
  75. next.step = curt.step + 3 ;
  76. if(!vis[sx][sy][next.step%4])
  77. {
  78. vis[sx][sy][next.step%4] = true ;
  79. q.push(next) ;
  80. }
  81. }
  82. }
  83. else if(!vis[sx][sy][step%4])
  84. {
  85. vis[sx][sy][step%4] = true ;
  86. q.push(next) ;
  87. }
  88. }
  89. }
  90. }
  91. return -1 ;
  92. }
  93. void init(int x ,int y) // 处理摄像头方向
  94. {
  95. int sx ,sy ,key = 0 ;
  96. for(int i = 0 ;i < 4 ; ++i)
  97. {
  98. dir[x][y][i] = true ;
  99. sx = x + dx[i] ;
  100. sy = y + dy[i] ;
  101. if(s[x][y] == 'E')
  102. key = 3 ;
  103. else if(s[x][y] == 'S')
  104. key = 2 ;
  105. else if(s[x][y] == 'W')
  106. key = 1 ;
  107. if(sx >= 0 && sx < n && sy >= 0 && sy < n && s[sx][sy] != '#')
  108. dir[sx][sy][(key+i)%4] = true ;
  109. }
  110. s[x][y] = '.' ;
  111. }
  112. int main()
  113. {
  114. int Tx ,sx ,sy ,cse = 1 ;
  115. scanf("%d" ,&Tx) ;
  116. while(Tx--)
  117. {
  118. cin>>n ;
  119. memset(dir ,false ,sizeof(dir)) ;
  120. for(int i = 0 ;i < n ; ++i)
  121. {
  122. cin>>s[i] ;
  123. for(int j = 0 ;j < n ; ++j)
  124. if(s[i][j] == 'M')
  125. {
  126. sx = i ; sy = j ;
  127. s[i][j] = '.' ;
  128. }
  129. else if(s[i][j] == 'T')
  130. {
  131. ex = i ; ey = j ;
  132. s[i][j] = '.' ;
  133. }
  134. else if(s[i][j] == 'N' || s[i][j] == 'S' || s[i][j] == 'W' || s[i][j] == 'E')
  135. init(i ,j) ;
  136. }
  137. cout<<"Case #"<<cse++<<": "<<bfs(sx ,sy)<<endl ;
  138. }
  139. return 0 ;
  140. }


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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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