HDU 1495 非常可乐(bfs)|POJ 3414 Pots(bfs+打印路径)

举报
Linux猿 发表于 2021/08/05 23:13:41 2021/08/05
【摘要】   题目链接~~>           这个题很有意义,很久以前就注意她了,不知道怎么搜!开始我用 dfs 写,写完后样例倒是能过,但是自己测试了几组数据就卡了,调试了老半天还是不行,接下来想怎么剪枝,郁闷了很久,决定上网搜一下,一搜原来用广搜,回来又开始用...

  题目链接~~>

          这个题很有意义,很久以前就注意她了,不知道怎么搜!开始我用 dfs 写,写完后样例倒是能过,但是自己测试了几组数据就卡了,调试了老半天还是不行,接下来想怎么剪枝,郁闷了很久,决定上网搜一下,一搜原来用广搜委屈,回来又开始用广搜写,简单调试了一下 1A。以后做题一定要先想好要用什么方法,这种方法可行不,如果超时还不如不写,以后做题要慎重啊!

代码:


  
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std ;
  5. int a,b,c,m;
  6. int vis[100][100];//标记!!
  7. struct zhang
  8. {
  9. int x,y,z,bu;
  10. };
  11. void bfs(int x)//倒水只有六种情况
  12. {
  13. queue<zhang>q;
  14. zhang current,next;
  15. memset(vis,0,sizeof(vis));
  16. current.x=x;
  17. current.y=0;
  18. current.z=0;
  19. current.bu=0;
  20. q.push(current);
  21. while(!q.empty())
  22. {
  23. current=q.front();
  24. q.pop();
  25. if(vis[current.y][current.z])
  26. continue;
  27. if(current.x>0&¤t.y<b)
  28. {
  29. if(current.x+current.y<=b)
  30. {
  31. next.x=0;next.y=current.x+current.y;
  32. next.z=current.z;next.bu=current.bu+1;
  33. if(next.x==next.y&&next.x*2==a)
  34. {
  35. m=next.bu;
  36. return ;
  37. }
  38. else
  39. q.push(next);
  40. }
  41. else{
  42. next.x=current.x+current.y-b;
  43. next.y=b;next.z=current.z;
  44. next.bu=current.bu+1;
  45. if(next.x==next.y&&next.x*2==a)
  46. {
  47. m=next.bu;
  48. return ;
  49. }
  50. else
  51. q.push(next);
  52. }
  53. }
  54. if(current.x>0&¤t.z<c)
  55. {
  56. if(current.x+current.z<=c)
  57. {
  58. next.x=0;next.y=current.y;
  59. next.z=current.x+current.z;
  60. next.bu=current.bu+1;
  61. if(next.x==next.y&&next.x*2==a)
  62. {
  63. m=next.bu;
  64. return ;
  65. }
  66. else
  67. q.push(next);
  68. }
  69. else
  70. {
  71. next.x=current.x+current.z-c;
  72. next.y=current.y;next.z=c;
  73. next.bu=current.bu+1;
  74. if(next.x==next.y&&next.x*2==a)
  75. {
  76. m=next.bu;
  77. return ;
  78. }
  79. else
  80. q.push(next);
  81. }
  82. }
  83. if(current.y>0)
  84. {
  85. next.x=current.x+current.y;
  86. next.y=0;next.z=current.z;
  87. next.bu=current.bu+1;
  88. if(next.x==next.y&&next.x*2==a)
  89. {
  90. m=next.bu;
  91. return ;
  92. }
  93. else
  94. q.push(next);
  95. }
  96. if(current.y>0&¤t.z<c)
  97. {
  98. if(current.y+current.z<=c)
  99. {
  100. next.x=current.x;next.y=0;
  101. next.z=current.y+current.z;
  102. next.bu=current.bu+1;
  103. if(next.x==next.y&&next.x*2==a)
  104. {
  105. m=next.bu;
  106. return ;
  107. }
  108. else
  109. q.push(next);
  110. }
  111. else
  112. {
  113. next.x=current.x;next.y=current.y+current.z-c;
  114. next.z=c;next.bu=current.bu+1;
  115. if(next.x==next.y&&next.x*2==a)
  116. {
  117. m=next.bu;
  118. return ;
  119. }
  120. else
  121. q.push(next);
  122. }
  123. }
  124. if(current.z>0)
  125. {
  126. next.x=current.x+current.z;
  127. next.y=current.y;next.z=0;
  128. next.bu=current.bu+1;
  129. if(next.x==next.y&&next.x*2==a)
  130. {
  131. m=next.bu;
  132. return ;
  133. }
  134. else
  135. q.push(next);
  136. }
  137. if(current.z>0&¤t.y<b)
  138. {
  139. if(current.z+current.y<=b)
  140. {
  141. next.x=current.x;next.y=current.z+current.y;
  142. next.z=0;next.bu=current.bu+1;
  143. if(next.x==next.y&&next.x*2==a)
  144. {
  145. m=next.bu;
  146. return ;
  147. }
  148. else
  149. q.push(next);
  150. }
  151. else {
  152. next.x=current.x;
  153. next.y=b;next.z=current.z+current.y-b;
  154. next.bu=current.bu+1;
  155. if(next.x==next.y&&next.x*2==a)
  156. {
  157. m=next.bu;
  158. return ;
  159. }
  160. else
  161. q.push(next);
  162. }
  163. }
  164. vis[current.y][current.z]=1;
  165. }
  166. }
  167. int main()
  168. {
  169. while(scanf("%d%d%d",&a,&b,&c)!=EOF)
  170. {
  171. if(a==0&&b==0&&c==0)
  172. break;
  173. if(b==c)
  174. {
  175. printf("1\n");
  176. continue;
  177. }
  178. if(a%2)
  179. {
  180. printf("NO\n");
  181. continue;
  182. }
  183. m=-1;
  184. int t;
  185. if(b<c)
  186. {
  187. t=b;b=c;c=t;
  188. }
  189. bfs(a);
  190. if(m!=-1)
  191. printf("%d\n",m);
  192. else
  193. printf("NO\n");
  194. }
  195. return 0;
  196. }

题目链接~~>

                    POJ 的 3414题 就多了一个打印路径。

代码:


  
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std ;
  5. const int INF=100000 ;
  6. int a,b,c ;
  7. bool vis[105][105] ;
  8. struct zhang
  9. {
  10. int x,y,z,step,pre ;
  11. }q[INF] ;
  12. void search(int x)
  13. {
  14. if(!x||x==1)
  15. printf("FILL(%d)\n",x+1) ;
  16. else if(x==2||x==3)
  17. printf("DROP(%d)\n",x-1) ;
  18. else if(x==4)
  19. printf("POUR(1,2)\n") ;
  20. else if(x==5)
  21. printf("POUR(2,1)\n") ;
  22. }
  23. void print(int k)
  24. {
  25. if(k!=-1)
  26. {
  27. print(q[k].pre) ;
  28. search(q[k].z) ;
  29. }
  30. return ;
  31. }
  32. int bfs(int x,int y)
  33. {
  34. int st=-1,head=0 ;
  35. zhang curt,next ;
  36. memset(vis,false,sizeof(vis)) ;
  37. curt.x=x ;
  38. curt.y=y ;
  39. curt.z=-1 ;
  40. curt.pre=-1 ;
  41. curt.step=0 ;
  42. vis[x][y]=true ;
  43. q[++st]=curt ;
  44. while(head<=st)
  45. {
  46. curt=q[head++] ;
  47. if(curt.x==c||curt.y==c)
  48. {
  49. printf("%d\n",curt.step) ;
  50. print(head-1) ;
  51. return true ;
  52. }
  53. next.step=curt.step+1 ;
  54. next.pre=head-1 ;
  55. if(curt.x<a) //倒满A
  56. {
  57. next.x=a ;
  58. next.y=curt.y ;
  59. next.z=0 ;
  60. if(!vis[next.x][next.y])
  61. {
  62. vis[next.x][next.y]=true ;
  63. q[++st]=next ;
  64. }
  65. }
  66. if(curt.y<b)//倒满B
  67. {
  68. next.x=curt.x ;
  69. next.y=b ;
  70. next.z=1 ;
  71. if(!vis[next.x][next.y])
  72. {
  73. vis[next.x][next.y]=true ;
  74. q[++st]=next ;
  75. }
  76. }
  77. if(curt.x) // 倒掉A
  78. {
  79. next.x=0 ;
  80. next.y=curt.y ;
  81. next.z=2 ;
  82. if(!vis[next.x][next.y])
  83. {
  84. vis[next.x][next.y]=true ;
  85. q[++st]=next ;
  86. }
  87. }
  88. if(curt.y)// 倒掉B
  89. {
  90. next.x=curt.x ;
  91. next.y=0 ;
  92. next.z=3 ;
  93. if(!vis[next.x][next.y])
  94. {
  95. vis[next.x][next.y]=true ;
  96. q[++st]=next ;
  97. }
  98. }
  99. if(curt.x>0&&curt.y<b)// 从A 中向B 中倒水
  100. {
  101. next.z=4 ;
  102. if(curt.x<=b-curt.y)
  103. {
  104. next.x=0 ;
  105. next.y=curt.y+curt.x ;
  106. if(!vis[next.x][next.y])
  107. {
  108. vis[next.x][next.y]=true ;
  109. q[++st]=next ;
  110. }
  111. }
  112. else
  113. {
  114. next.x=curt.x+curt.y-b ;
  115. next.y=b ;
  116. if(!vis[next.x][next.y])
  117. {
  118. vis[next.x][next.y]=true ;
  119. q[++st]=next ;
  120. }
  121. }
  122. }
  123. if(curt.x<a&&curt.y>0)// 从B 中向A 中倒水
  124. {
  125. next.z=5 ;
  126. if(curt.y<=a-curt.x)
  127. {
  128. next.x=curt.x+curt.y ;
  129. next.y=0 ;
  130. if(!vis[next.x][next.y])
  131. {
  132. vis[next.x][next.y]=true ;
  133. q[++st]=next ;
  134. }
  135. }
  136. else
  137. {
  138. next.x=a ;
  139. next.y=curt.y+curt.x-a ;
  140. if(!vis[next.x][next.y])
  141. {
  142. vis[next.x][next.y]=true ;
  143. q[++st]=next ;
  144. }
  145. }
  146. }
  147. }
  148. return false ;
  149. }
  150. int main()
  151. {
  152. while(scanf("%d%d%d",&a,&b,&c)!=EOF)
  153. {
  154. if(!bfs(0,0))
  155. printf("impossible\n") ;
  156. }
  157. }

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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