HDU ACM 1495 非常可乐(广搜BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1495
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /*题意:要求将一瓶可乐平均分成份,问能否平均分成两份,如果能输出最少需要几次否则输出NO 2 题目给出三个整数 S N M S表示可乐总量 N M分别为两个杯子的容量 且 S= N + M 3 4 分析:题目是由 S 0 0 状态 转换到 0 S/2 S/2 的状态且要找出最优解,用广搜 5 题目只有6种状态转换方式,建立一格六叉数的模型进行遍历。 6 用结构体记录步数 步数为弹出队头元素的步数+1 7 遍历完成条件为 1.找到一个方法能够平分 2.遍历所有可能情况仍然无法找到 8 9 注意: 分清楚 S N M 在标记中的位置 10 */ 11 #include <iostream> 12 #include <queue> 13 using namespace std; 14 struct FUN 15 { 16 int s,n,m; 17 int step; 18 }; 19 int s,n,m; 20 int used[110][110][110];//used[s][n][m] 21 int BFS() 22 { 23 memset(used,0,sizeof(used)); 24 queue <FUN> q; 25 FUN a; 26 a.s = s; 27 a.n = 0; 28 a.m = 0; 29 a.step = 0; 30 used[a.s][a.n][a.m]=1; 31 q.push(a); 32 while(!q.empty()) 33 { 34 FUN b; 35 a = q.front(); 36 a.step = a.step + 1; 37 q.pop(); 38 //s -> n 39 b = a; 40 if(b.s > n - b.n) //能装满 41 { 42 b.s = b.s - (n - b.n); 43 b.n = n; 44 } 45 else //不能装满 46 { 47 b.n = b.n + b.s; 48 b.s = 0; 49 } 50 if(b.m == b.n && b.s == 0 || b.m == b.s && b.n == 0 || b.n == b.s && b.m == 0) 51 { 52 return b.step; 53 } 54 if(!used[b.s][b.n][b.m]) 55 { 56 used[b.s][b.n][b.m] = 1; 57 q.push(b); 58 } 59 60 //s -> m 61 b = a; 62 if(b.s > m - b.m) 63 { 64 b.s = b.s - (m - b.m); 65 b.m = m; 66 } 67 else 68 { 69 b.m = b.m + b.s; 70 b.s = 0; 71 } 72 if(b.m == b.n && b.s == 0 || b.m == b.s && b.n == 0 || b.n == b.s && b.m == 0) 73 { 74 return b.step; 75 } 76 if(!used[b.s][b.n][b.m]) 77 { 78 used[b.s][b.n][b.m] = 1; 79 q.push(b); 80 } 81 82 //n -> m 83 b = a; 84 if(b.n > m - b.m) 85 { 86 b.n = b.n - (m - b.m); 87 b.m = m; 88 } 89 else 90 { 91 b.m = b.m + b.n; 92 b.n = 0; 93 } 94 if(b.m == b.n && b.s == 0 || b.m == b.s && b.n == 0 || b.n == b.s && b.m == 0) 95 { 96 return b.step; 97 } 98 if(!used[b.s][b.n][b.m]) 99 { 100 used[b.s][b.n][b.m] = 1; 101 q.push(b); 102 } 103 104 //n -> s 105 b = a; 106 if(b.n > s - b.s) 107 { 108 b.n = b.n - (s - b.s); 109 b.s = s; 110 } 111 else 112 { 113 b.s = b.s + b.n; 114 b.n = 0; 115 } 116 if(b.m == b.n && b.s == 0 || b.m == b.s && b.n == 0 || b.n == b.s && b.m == 0) 117 { 118 return b.step; 119 } 120 if(!used[b.s][b.n][b.m]) 121 { 122 used[b.s][b.n][b.m] = 1; 123 q.push(b); 124 } 125 126 //m -> s 127 b = a; 128 if(b.m > s - b.s) 129 { 130 b.m = b.m - (s - b.s); 131 b.s = s; 132 } 133 else 134 { 135 b.s = b.s + b.m; 136 b.m = 0; 137 } 138 if(b.m == b.n && b.s == 0 || b.m == b.s && b.n == 0 || b.n == b.s && b.m == 0) 139 { 140 return b.step; 141 } 142 if(!used[b.s][b.n][b.m]) 143 { 144 used[b.s][b.n][b.m] = 1; 145 q.push(b); 146 } 147 148 //m -> n 149 b = a; 150 if(b.m > n - b.n) 151 { 152 b.m = b.m - (n - b.n); 153 b.n = n; 154 } 155 else 156 { 157 b.n = b.n + b.m; 158 b.m = 0; 159 } 160 if(b.m == b.n && b.s == 0 || b.m == b.s && b.n == 0 || b.n == b.s && b.m == 0) 161 { 162 return b.step; 163 } 164 if(!used[b.s][b.n][b.m]) 165 { 166 used[b.s][b.n][b.m] = 1; 167 q.push(b); 168 } 169 } 170 return 0; 171 } 172 int main() 173 { 174 while(cin>>s>>n>>m,s+n+m) 175 { 176 if(s % 2) 177 { 178 cout<<"NO"<<endl; 179 } 180 else 181 { 182 int m = BFS(); 183 if(m) 184 { 185 cout<<m<<endl; 186 } 187 else 188 { 189 cout<<"NO"<<endl; 190 } 191 } 192 } 193 return 0; 194 }