HDU 1180题 诡异的楼梯

举报
Linux猿 发表于 2021/08/05 23:19:20 2021/08/05
【摘要】  题目链接~~>                      这题其实很简单,在走楼梯时只要判断是否能走就可以,如果不能走,可以选择等下一分钟再走,还有一点就是楼...

 题目链接~~>

                     这题其实很简单,在走楼梯时只要判断是否能走就可以,如果不能走,可以选择等下一分钟再走,还有一点就是楼梯不能标记,可以从不同方向过。

代码:


  
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std ;
  4. int dx[4]={-1,1,0,0},dy[4]={0,0,1,-1} ;
  5. char s[25][25] ;
  6. int m,n ;
  7. struct zhang
  8. {
  9. int x,y,bu ;
  10. } ;
  11. int bfs(int x,int y)
  12. {
  13. queue<zhang>q ;
  14. zhang current,next ;
  15. int sx,sy ;
  16. current.x=x ;
  17. current.y=y ;
  18. s[x][y]='*' ;
  19. current.bu=0 ;
  20. q.push(current) ;
  21. while(!q.empty())
  22. {
  23. current=q.front() ;
  24. q.pop() ;
  25. for(int i=0;i<4;i++)
  26. {
  27. next.x=current.x+dx[i] ;
  28. next.y=current.y+dy[i] ;
  29. if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&s[next.x][next.y]!='*')
  30. {
  31. next.bu=current.bu+1 ;
  32. sx=next.x+dx[i] ;
  33. sy=next.y+dy[i] ;
  34. if(s[next.x][next.y]=='|'&&s[sx][sy]!='*')
  35. {
  36. if(current.bu%2&&(i==3||i==2))
  37. {
  38. next.x=sx ;
  39. next.y=sy ;
  40. if(s[sx][sy]=='T')
  41. return next.bu ;
  42. s[sx][sy]='*' ;
  43. q.push(next) ;
  44. }
  45. else if(current.bu%2==0&&(i==0||i==1))
  46. {
  47. next.x=sx ;
  48. next.y=sy ;
  49. if(s[sx][sy]=='T')
  50. return next.bu ;
  51. s[sx][sy]='*' ;
  52. q.push(next) ;
  53. }
  54. else {
  55. next.x=current.x ;
  56. next.y=current.y ;
  57. q.push(next) ;
  58. }
  59. }
  60. else if(s[next.x][next.y]=='-'&&s[sx][sy]!='*')
  61. {
  62. if(current.bu%2==0&&(i==3||i==2))
  63. {
  64. next.x=sx ;
  65. next.y=sy ;
  66. if(s[sx][sy]=='T')
  67. return next.bu ;
  68. s[sx][sy]='*' ;
  69. q.push(next) ;
  70. }
  71. else if(current.bu%2==1&&(i==0||i==1))
  72. {
  73. next.x=sx ;
  74. next.y=sy ;
  75. if(s[sx][sy]=='T')
  76. return next.bu ;
  77. s[sx][sy]='*' ;
  78. q.push(next) ;
  79. }
  80. else {
  81. next.x=current.x ;
  82. next.y=current.y ;
  83. q.push(next) ;
  84. }
  85. }
  86. else if(s[next.x][next.y]=='.')
  87. {
  88. s[next.x][next.y]='*' ;
  89. q.push(next) ;
  90. }
  91. else if(s[next.x][next.y]=='T')
  92. return next.bu ;
  93. }
  94. }
  95. }
  96. return -1 ;
  97. }
  98. int main()
  99. {
  100. int i,j,ax,ay ;
  101. while(scanf("%d%d",&n,&m)!=EOF)
  102. {
  103. for(i=0;i<n;i++)
  104. {
  105. scanf("%s",s[i]) ;
  106. for(j=0;j<m;j++)
  107. if(s[i][j]=='S')
  108. {
  109. ax=i ;
  110. ay=j ;
  111. }
  112. }
  113. printf("%d\n",bfs(ax,ay)) ;
  114. }
  115. return 0 ;
  116. }


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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