UVA 10047 The Moncycle

举报
Linux猿 发表于 2021/08/05 01:20:41 2021/08/05
【摘要】 题目链接~~> 做题感悟:这道题属于一般的搜索题,要注意一些细节,输出的时候检查是否与样例一样。 解题思路:这题终点在标记上面,因为题目中有方向和颜色限制,so~> 需要一个四维数组来标记,如果做过POJ上的左手定则这题应该很容易做。 代码: #include<stdio.h>#include<queue>#include<s...

题目链接~~>

做题感悟:这道题属于一般的搜索题,要注意一些细节,输出的时候检查是否与样例一样。

解题思路:这题终点在标记上面,因为题目中有方向和颜色限制,so~> 需要一个四维数组来标记,如果做过POJ上的左手定则这题应该很容易做。

代码:


  
  1. #include<stdio.h>
  2. #include<queue>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #include<algorithm>
  7. #include<iostream>
  8. using namespace std ;
  9. const int MX = 30 + 10 ;
  10. int n,m ;
  11. char s[MX][MX] ;
  12. bool vis[MX][MX][MX][MX] ;
  13. int dx[5]={1,-1,0,0,0},dy[5]={0,0,1,-1,0} ;
  14. int dt[4][4]={{2,4,4},{3,4,4},{0,4,4},{1,4,4}} ;
  15. int dw[4][4]={{0,2,3},{1,2,3},{2,0,1},{3,0,1}} ;
  16. struct node
  17. {
  18. int x,y,step,drt,clr ;
  19. } ;
  20. bool judge(int x,int y)
  21. {
  22. if(x<0||y<0||x>=n||y>=m||s[x][y]=='#')
  23. return false ;
  24. return true ;
  25. }
  26. int bfs(int x,int y) // 东西南北 0 1 2 3
  27. {
  28. queue<node>q ;
  29. node curt,next ;
  30. memset(vis,false,sizeof(vis)) ;
  31. curt.x=x ;
  32. curt.y=y ;
  33. curt.step=0 ; // 时间
  34. curt.clr=0 ; // 颜色
  35. curt.drt=3 ; // 方向
  36. q.push(curt) ;
  37. vis[x][y][0][3]=true ; // 颜色+方向
  38. while(!q.empty())
  39. {
  40. curt=q.front() ;
  41. if(s[curt.x][curt.y]=='T'&&curt.clr==0)
  42. return curt.step ;
  43. q.pop() ;
  44. int cnt=curt.drt ;// 主方向
  45. for(int i=0 ;i<3 ;i++)
  46. {
  47. int tx=dt[cnt][i] ;
  48. next.x=curt.x+dx[tx] ;
  49. next.y=curt.y+dy[tx] ;
  50. if(i)
  51. next.clr=curt.clr ;
  52. else next.clr=(curt.clr+1)%5 ;
  53. next.step=curt.step+1 ;
  54. next.drt=dw[cnt][i] ;
  55. if(judge(next.x,next.y)) // 判断出界
  56. {
  57. if(vis[next.x][next.y][next.clr][next.drt]) continue ;
  58. vis[next.x][next.y][next.clr][next.drt]=true ;
  59. q.push(next) ;
  60. }
  61. }
  62. }
  63. return -1 ;
  64. }
  65. int main()
  66. {
  67. int sx,sy,cse=1 ;
  68. while(~scanf("%d%d",&n,&m)&&(n+m))
  69. {
  70. for(int i=0 ;i<n ;i++)
  71. {
  72. scanf("%s",s[i]) ;
  73. for(int j=0 ;j<m ;j++)
  74. if(s[i][j]=='S')
  75. {
  76. sx=i ;
  77. sy=j ;
  78. }
  79. }
  80. int mx=bfs(sx,sy) ;
  81. if(cse!=1) cout<<endl ;
  82. cout<<"Case #"<<cse++<<endl ;
  83. if(mx==-1)
  84. cout<<"destination not reachable"<<endl ;
  85. else cout<<"minimum time = "<<mx<<" sec"<<endl ;
  86. }
  87. return 0 ;
  88. }

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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