7 4 3 4 1 3 0 0 0
NO 3
分析:可以将本题看成一个隐式图,枚举所有的情况直到能够平分,或者枚举完。
代码:
#include <cstdio>
#include <queue> //用到了优先队列
#include <cstring>
#include <algorithm>
const int M = 101;
using namespace std;
bool vis[101][101][101]; //标记状态
struct node{
int v[3];
int step;
bool operator < (const node &a) const{
return step > a.step;
}
};
int hal, v[3]; //hal是一半,v数组是每个杯子的容积
bool match(node a){
int ans = 0;
for(int i = 0; i < 3; ++ i)
if(a.v[i] == hal) ++ans;
if(ans == 2) return 1; //
return 0;
}
int bfs(){
memset(vis, 0, sizeof(vis));
node st;
st.v[0] = v[0]; st.v[1] = st.v[2] = 0;
st.step = 0;
priority_queue<node >q;
q.push(st);
vis[v[0]][0][0] = 1;
if(match(st)) return st.step;
while(!q.empty()){
node temp = q.top();
q.pop();
for(int i = 0; i < 3; ++ i){
if(temp.v[i]){ //如果一个杯子有水,就让他往其他两个杯子倒水
for(int j = 1; j < 3; ++ j){
node cur = temp;
int w = (i+j)%3;
if(cur.v[w] < v[w]){ //倒的时候也分是不是满了,如果不满那么还需要判断能不能倒满还是不能倒满
if(cur.v[i] <= (v[w]-cur.v[w])){
cur.v[w] += cur.v[i]; cur.v[i] = 0;
if(match(cur)) return cur.step+1;
}
else {
cur.v[i] -= (v[w]-cur.v[w]);
cur.v[w] = v[w];
if(match(cur)) return cur.step+1;
}
if(!vis[cur.v[0]][cur.v[1]][cur.v[2]]){
vis[cur.v[0]][cur.v[1]][cur.v[2]] = 1;
cur.step++;
q.push(cur);
}
}
}
}
}
}
return -1;
}
int main(){
while(scanf("%d%d%d", &v[0], &v[1], &v[2]), v[0]||v[1]||v[2]){
if(v[0]&1){
printf("NO\n");
continue;
}
else {
hal = v[0]/2;
int ans = bfs();
if(ans >= 0) printf("%d\n", ans);
else printf("NO\n");
}
}
return 0;
}原文:http://blog.csdn.net/shengweisong/article/details/44425231