1. 程式人生 > >HDU - 1495 非常可樂(bfs)

HDU - 1495 非常可樂(bfs)

ostream pre ios rec scrip miss type span %d

非常可樂

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20347 Accepted Submission(s): 8248


Problem Description 大家一定覺的運動以後喝可樂是一件很愜意的事情,但是seeyou卻不這麽認為。因為每次當seeyou買了可樂以後,阿牛就要求和seeyou一起分享這一瓶可樂,而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子,它們的容量分別是N 毫升和M 毫升 可樂的體積為S (S<101)毫升 (正好裝滿一瓶) ,它們三個之間可以相互倒可樂 (都是沒有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聰明的ACMER你們說他們能平分嗎?如果能請輸出倒可樂的最少的次數,如果不能輸出"NO"。 Input 三個整數 : S 可樂的體積 , N 和 M是兩個杯子的容量,以"0 0 0"結束。 Output 如果能平分的話請輸出最少要倒的次數,否則輸出"NO"。 Sample Input 7 4 3 4 1 3 0 0 0 Sample Output NO 3 Author seeyou Source “2006校園文化活動月”之“校慶杯”大學生程序設計競賽暨杭州電子科技大學第四屆大學生程序設計競賽 Recommend LL
Statistic | Submit | Discuss | Note

思路:a可以往b中倒水,可以往c倒。。。。總共有六種情況,BFS廣度搜索就行了

註意搜索的時候要標記一下狀態,用visit,不然會queue會超限。

就是代碼有點多,要仔細。。。

網上大佬用數學方法做的orz。。。。https://blog.csdn.net/v5zsq/article/details/52097459,看不懂啊。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <queue>
  4 #include <cstring>
  5 
  6 using namespace
std; 7 8 typedef struct info{ 9 int a,b,c; 10 int countt; 11 }cup; 12 13 int s,m,n; 14 cup c1; 15 cup now,nextt; 16 int visit[105][105][105]; 17 18 19 int bfs(cup c){ 20 // while(!q.empty()){ 21 // q.pop(); 22 // } 23 queue<cup> q; 24 q.push(c); 25 while
(!q.empty()){ 26 now=q.front(); q.pop(); 27 28 if((now.a==s/2&&now.b==s/2)||(now.a==s/2&&now.c==s/2)||(now.b==s/2&&now.c==s/2)){ 29 return now.countt; 30 } 31 if(now.a!=0){//第一個杯子向其他兩個倒水 32 int vol=n-now.b; 33 if(vol!=0){ 34 if(now.a>=vol){ 35 nextt.b=n; 36 nextt.a=now.a-vol; 37 nextt.c=now.c; 38 nextt.countt=now.countt+1; 39 }else{ 40 nextt.b=now.b+now.a; 41 nextt.a=0; 42 nextt.c=now.c; 43 nextt.countt=now.countt+1; 44 } 45 if(!visit[nextt.a][nextt.b][nextt.c]){ 46 visit[nextt.a][nextt.b][nextt.c]=1; 47 q.push(nextt); 48 } 49 } 50 51 vol=m-now.c; 52 if(vol!=0){ 53 if(now.a>=vol){ 54 nextt.c=m; 55 nextt.a=now.a-vol; 56 nextt.b=now.b; 57 nextt.countt=now.countt+1; 58 q.push(nextt); 59 }else{ 60 nextt.c=now.c+now.a; 61 nextt.a=0; 62 nextt.b=now.b; 63 nextt.countt=now.countt+1; 64 } 65 if(!visit[nextt.a][nextt.b][nextt.c]){ 66 visit[nextt.a][nextt.b][nextt.c]=1; 67 q.push(nextt); 68 } 69 } 70 } 71 72 if(now.b!=0){//第二個杯子忘其他兩個倒水 73 int vol=s-now.a; 74 if(vol!=0){ 75 if(now.b>=vol){ 76 nextt.a=s; 77 nextt.b=now.b-vol; 78 nextt.c=now.c; 79 nextt.countt=now.countt+1; 80 }else{ 81 nextt.a=now.a+now.b; 82 nextt.b=0; 83 nextt.c=now.c; 84 nextt.countt=now.countt+1; 85 } 86 if(!visit[nextt.a][nextt.b][nextt.c]){ 87 visit[nextt.a][nextt.b][nextt.c]=1; 88 q.push(nextt); 89 } 90 } 91 92 vol=m-now.c; 93 if(vol!=0){ 94 if(now.b>=vol){ 95 nextt.c=m; 96 nextt.a=now.a; 97 nextt.b=now.b-vol; 98 nextt.countt=now.countt+1; 99 }else{ 100 nextt.c=now.c+now.b; 101 nextt.b=0; 102 nextt.a=now.a; 103 nextt.countt=now.countt+1; 104 } 105 if(!visit[nextt.a][nextt.b][nextt.c]){ 106 visit[nextt.a][nextt.b][nextt.c]=1; 107 q.push(nextt); 108 } 109 } 110 } 111 112 if(now.c!=0){//第三個杯子忘其他兩個倒水 113 int vol=s-now.a; 114 if(vol!=0){ 115 if(now.c>=vol){ 116 nextt.a=s; 117 nextt.c=now.c-vol; 118 nextt.b=now.b; 119 nextt.countt=now.countt+1; 120 }else{ 121 nextt.a=now.a+now.c; 122 nextt.c=0; 123 nextt.b=now.b; 124 nextt.countt=now.countt+1; 125 } 126 if(!visit[nextt.a][nextt.b][nextt.c]){ 127 visit[nextt.a][nextt.b][nextt.c]=1; 128 q.push(nextt); 129 } 130 } 131 132 vol=n-now.b; 133 if(vol!=0){ 134 if(now.c>=vol){ 135 nextt.b=n; 136 nextt.c=now.c-vol; 137 nextt.a=now.a; 138 nextt.countt=now.countt+1; 139 }else{ 140 nextt.b=now.b+now.c; 141 nextt.c=0; 142 nextt.a=now.a; 143 nextt.countt=now.countt+1; 144 } 145 if(!visit[nextt.a][nextt.b][nextt.c]){ 146 visit[nextt.a][nextt.b][nextt.c]=1; 147 q.push(nextt); 148 } 149 } 150 } 151 } 152 return 0; 153 } 154 155 int main() 156 { 157 while(~scanf("%d %d %d",&s,&n,&m)){ 158 memset(visit,0,sizeof(visit)); 159 if(s==0&&m==0&&n==0){break;} 160 if(m==n){ 161 printf("1\n"); 162 }else if(s&1==1){ 163 printf("NO\n"); 164 }else{ 165 c1.a=s; c1.b=0; c1.c=0; c1.countt=0; 166 int countt=bfs(c1); 167 if(countt==0){ 168 printf("NO\n"); 169 }else{ 170 printf("%d\n",countt); 171 } 172 } 173 } 174 return 0; 175 }

HDU - 1495 非常可樂(bfs)