HDU 1728题逃离迷宫

举报
Linux猿 发表于 2021/08/05 22:17:53 2021/08/05
【摘要】 题目链接~~>        这题开始时以为是简单的搜索题,错了好几次,上网上参考了一下,才恍然大悟,搜一个方向要一直搜到底,或者让一些点重新入队,实现再次搜索,这样写完后还是wa,又重新看了一下代码,写错了一个字符。 参考代码: #include<stdio.h>#include<qu...

题目链接~~>

       这题开始时以为是简单的搜索题,错了好几次,上网上参考了一下,才恍然大悟,搜一个方向要一直搜到底,或者让一些点重新入队,实现再次搜索,这样写完后还是wa,又重新看了一下代码,写错了一个字符。

参考代码:


  
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4. int n,m,k;
  5. char s[105][105];
  6. int vis[105][105],dx[4]={1,-1,0,0},dy[4]={0,0,-1,1};
  7. struct zhang
  8. {
  9. int x,y,bu,d;
  10. };
  11. bool diy(int x,int y)
  12. {
  13. if(x>=0&&x<n&&y>=0&&y<m&&s[x][y]=='.')
  14. return true ;
  15. else return false ;
  16. }
  17. void bfs(int x1,int y1)
  18. {
  19. int i;
  20. queue<zhang>q;
  21. zhang current,next;
  22. for(i=0;i<4;i++)
  23. {
  24. current.x=x1+dx[i];
  25. current.y=y1+dy[i];
  26. current.bu=0;
  27. current.d=i;
  28. if(diy(current.x,current.y))
  29. {
  30. vis[current.x][current.y]=0;
  31. q.push(current);
  32. }
  33. }
  34. while(!q.empty())
  35. {
  36. current=q.front();
  37. q.pop();
  38. for(i=0;i<4;i++)
  39. {
  40. next.x=current.x+dx[i];
  41. next.y=current.y+dy[i];
  42. if(current.d!=i)
  43. next.bu=current.bu+1;
  44. else next.bu=current.bu;
  45. next.d=i;
  46. if(next.bu<=k&&diy(next.x,next.y))
  47. {
  48. if(next.bu<=vis[next.x][next.y])//很重要!如果步数相等则再次入队,
  49. { //等于又在另一个方向上进行
  50. vis[next.x][next.y]=next.bu;
  51. q.push(next);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. int main()
  58. {
  59. int T,i,y1,x1,x2,y2;
  60. scanf("%d",&T);
  61. while(T--)
  62. {
  63. scanf("%d%d",&n,&m);
  64. for(i=0;i<n;i++)
  65. scanf("%s",s[i]);
  66. scanf("%d%d%d%d%d",&k,&y1,&x1,&y2,&x2);
  67. y1--;x1--;y2--;x2--;
  68. for(i=0;i<n;i++)
  69. for(int j=0;j<m;j++)
  70. vis[i][j]=20;
  71. bfs(x1,y1);
  72. if(vis[x2][y2]<=k)
  73. printf("yes\n");
  74. else printf("no\n");
  75. }
  76. return 0 ;
  77. }


源代码:


  
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4. int vis[150][150];
  5. char s[150][150];
  6. int n,m,k,y2,x2;
  7. struct zhang
  8. {
  9. int x,y,bu,d;
  10. };
  11. void bfs(int x1,int y1)
  12. {
  13. int i;
  14. zhang current,next;
  15. queue<zhang>q;
  16. current.x=x1;
  17. current.y=y1;
  18. current.bu=0;
  19. current.d=-1;
  20. vis[current.x][current.y]=-1;
  21. q.push(current);
  22. while(!q.empty())
  23. {
  24. current=q.front();
  25. q.pop();
  26. if(current.d==-1)//刚开始搜索时
  27. {
  28. for(i=current.y+1;i<m;i++)//右 1
  29. if(s[current.x][i]!='*')
  30. {
  31. next.d=1;
  32. next.x=current.x;
  33. next.y=i;
  34. next.bu=0;
  35. vis[next.x][next.y]=0;
  36. q.push(next);
  37. }
  38. else break;
  39. for(i=current.y-1;i>=0;i--)// 左 2
  40. if(s[current.x][i]!='*')
  41. {
  42. next.d=2;
  43. next.x=current.x;
  44. next.y=i;
  45. next.bu=0;
  46. vis[next.x][next.y]=0;
  47. q.push(next);
  48. }
  49. else break;
  50. for(i=current.x-1;i>=0;i--)// 上 3
  51. if(s[i][current.y]!='*')
  52. {
  53. next.d=3;
  54. next.y=current.y;
  55. next.x=i;
  56. next.bu=0;
  57. vis[next.x][next.y]=0;
  58. q.push(next);
  59. }
  60. else break;
  61. for(i=current.y+1;i<n;i++)// 下 4
  62. if(s[i][current.y]!='*')
  63. {
  64. next.d=4;
  65. next.y=current.y;
  66. next.x=i;
  67. next.bu=0;
  68. vis[next.x][next.y]=0;
  69. q.push(next);
  70. }
  71. else break;
  72. }
  73. else {//沿着四个方向一直搜到底
  74. for(i=current.y+1;i<m;i++)// 右 1
  75. if(s[current.x][i]!='*')
  76. {
  77. if(current.d!=1)
  78. next.bu=current.bu+1;
  79. else
  80. next.bu=current.bu;
  81. if(next.bu<vis[current.x][i])
  82. {
  83. vis[current.x][i]=next.bu;
  84. next.d=1;
  85. next.x=current.x;
  86. next.y=i;
  87. q.push(next);
  88. }
  89. }
  90. else break;
  91. for(i=current.y-1;i>=0;i--)// 左 2
  92. if(s[current.x][i]!='*')
  93. {
  94. if(current.d!=2)
  95. next.bu=current.bu+1;
  96. else
  97. next.bu=current.bu;
  98. if(next.bu<vis[current.x][i])
  99. {
  100. vis[current.x][i]=next.bu;
  101. next.d=2;
  102. next.x=current.x;
  103. next.y=i;
  104. q.push(next);
  105. }
  106. }
  107. else break;
  108. for(i=current.x-1;i>=0;i--)// 上 3
  109. if(s[i][current.y]!='*')
  110. {
  111. if(current.d!=3)
  112. next.bu=current.bu+1;
  113. else
  114. next.bu=current.bu;
  115. if(next.bu<vis[i][current.y])
  116. {
  117. vis[i][current.y]=next.bu;
  118. next.d=3;
  119. next.x=i;
  120. next.y=current.y;
  121. q.push(next);
  122. }
  123. }
  124. else break;
  125. for(i=current.x+1;i<n;i++)// 下 4
  126. if(s[i][current.y]!='*')
  127. {
  128. if(current.d!=4)
  129. next.bu=current.bu+1;
  130. else
  131. next.bu=current.bu;
  132. if(next.bu<vis[i][current.y])
  133. {
  134. vis[i][current.y]=next.bu;
  135. next.d=4;
  136. next.x=i;
  137. next.y=current.y;
  138. q.push(next);
  139. }
  140. }
  141. else break;
  142. }
  143. }
  144. }
  145. int main()
  146. {
  147. int T,x1,y1;
  148. scanf("%d",&T);
  149. while(T--)
  150. {
  151. scanf("%d%d",&n,&m);
  152. for(int i=0;i<n;i++)
  153. scanf("%s",s[i]);
  154. scanf("%d%d%d%d%d",&k,&y1,&x1,&y2,&x2);
  155. y2--; x2--;
  156. for(int i=0;i<n;i++)
  157. for(int j=0;j<m;j++)
  158. vis[i][j]=20;
  159. bfs(x1-1,y1-1);
  160. if(vis[x2][y2]<=k)
  161. printf("yes\n");
  162. else
  163. printf("no\n");
  164. }
  165. return 0;
  166. }
  167. //必须全部搜完有一些点的拐弯次数不断在更新


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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