Guest User

入门2-O-非常可乐

a guest
Feb 3rd, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int S,N,M;
  4. int vis[102][102];
  5. struct pos{
  6. int Vol;
  7. int volN;
  8. int volM;
  9. int steps;
  10. };
  11. void bfs();
  12. int main(){
  13. while(scanf("%d %d %d",&S,&N,&M)==3){
  14. if(S==0) return 0;
  15. if(S%2!=0) {
  16. printf("NO\n");
  17. continue;
  18. }
  19. for(int i=0;i<=101;i++)
  20. for(int j=1;j<=101;j++){
  21. vis[i][j]=0;
  22. }
  23. bfs();
  24. }
  25. return 0;
  26. }
  27.  
  28. void bfs()
  29. {
  30. pos cur,nex;
  31. cur.Vol=S;
  32. cur.volN=0;
  33. cur.volM=0;
  34. cur.steps=0;
  35. queue<pos>qu;
  36. qu.push(cur);
  37. vis[cur.volN][cur.volM]=1;
  38. while(!qu.empty()){
  39. cur=qu.front();
  40. qu.pop();
  41. if(cur.Vol==S/2&&cur.volN==S/2||cur.Vol==S/2&&cur.volM==S/2||cur.volM==S/2&&cur.volN==S/2){
  42. printf("%d\n",cur.steps);
  43. return;
  44. }
  45. int temp;
  46. temp=N-cur.volN;
  47. if(cur.Vol<=temp){
  48. nex.volN=cur.Vol+cur.volN;
  49. nex.Vol=0;
  50. nex.volM=cur.volM;
  51. }
  52. else{
  53. nex.volN=N;
  54. nex.Vol=cur.Vol-temp;
  55. nex.volM=cur.volM;
  56. }
  57. nex.steps=cur.steps+1;
  58. if(nex.Vol<=S&&nex.Vol>=0&&nex.volN<=N&&nex.volN>=0){
  59. if(vis[nex.volN][nex.volM]==0){
  60. vis[nex.volN][nex.volM]=1;
  61. qu.push(nex);
  62. }
  63. }
  64.  
  65. temp=M-cur.volM;
  66. if(cur.Vol<=temp){
  67. nex.volM=cur.Vol+cur.volM;
  68. nex.volN=cur.volN;
  69. nex.Vol=0;
  70. }
  71. else{
  72. nex.volM=M;
  73. nex.Vol=cur.Vol-temp;
  74. nex.volN=cur.volN;
  75. }
  76. nex.steps=cur.steps+1;
  77. if(nex.Vol<=S&&nex.Vol>=0&&nex.volM<=M&&nex.volM>=0){
  78. if(vis[nex.volN][nex.volM]==0){
  79. vis[nex.volN][nex.volM]=1;
  80. qu.push(nex);
  81. }
  82. }
  83.  
  84. temp=S-cur.Vol;
  85. if(cur.volN<=temp){
  86. nex.Vol=cur.Vol+cur.volN;
  87. nex.volM=cur.volM;
  88. nex.volN=0;
  89. }
  90. else{
  91. nex.Vol=S;
  92. nex.volN=cur.volN-temp;
  93. nex.volM=cur.volM;
  94. }
  95. nex.steps=cur.steps+1;
  96. if(nex.Vol<=S&&nex.Vol>=0&&nex.volN<=N&&nex.volN>=0){
  97. if(vis[nex.volN][nex.volM]==0){
  98. vis[nex.volN][nex.volM]=1;
  99. qu.push(nex);
  100. }
  101. }
  102.  
  103. temp=S-cur.Vol;
  104. if(cur.volM<=temp){
  105. nex.Vol=cur.Vol+cur.volM;
  106. nex.volN=cur.volN;
  107. nex.volM=0;
  108. }
  109. else{
  110. nex.Vol=S;
  111. nex.volM=cur.volM-temp;
  112. nex.volN=cur.volN;
  113. }
  114. nex.steps=cur.steps+1;
  115. if(nex.Vol<=S&&nex.Vol>=0&&nex.volM<=M&&nex.volM>=0){
  116. if(vis[nex.volN][nex.volM]==0){
  117. vis[nex.volN][nex.volM]=1;
  118. qu.push(nex);
  119. }
  120. }
  121.  
  122. temp=M-cur.volM;
  123. if(cur.volN<=temp){
  124. nex.volM=cur.volN+cur.volM;
  125. nex.Vol=cur.Vol;
  126. nex.volN=0;
  127. }
  128. else{
  129. nex.volM=M;
  130. nex.volN=cur.volN-temp;
  131. nex.Vol=cur.Vol;
  132. }
  133. nex.steps=cur.steps+1;
  134. if(nex.volN<=N&&nex.volN>=0&&nex.volM<=M&&nex.volM>=0){
  135. if(vis[nex.volN][nex.volM]==0){
  136. vis[nex.volN][nex.volM]=1;
  137. qu.push(nex);
  138. }
  139. }
  140.  
  141. temp=N-cur.volN;
  142. if(cur.volM<=temp){
  143. nex.volN=cur.volM+cur.volN;
  144. nex.volM=0;
  145. nex.Vol=cur.Vol;
  146. }
  147. else{
  148. nex.volN=N;
  149. nex.volM=cur.volM-temp;
  150. nex.Vol=cur.Vol;
  151. }
  152. nex.steps=cur.steps+1;
  153. if(nex.volN<=N&&nex.volN>=0&&nex.volM<=M&&nex.volM>=0){
  154. if(vis[nex.volN][nex.volM]==0){
  155. vis[nex.volN][nex.volM]=1;
  156. qu.push(nex);
  157. }
  158. }
  159. }
  160. printf("NO\n");
  161. return;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment