#include<stdio.h>
struct point {
int step;
int bot[3];
};
int book[101][101][101] = {0};
struct point que[1000000];
int head , tail , s , m , n ;
int b[3];
int flag;
int Is (int x , int y , int z){
if (x == y && z == 0) return 1 ;
else if(x == z && y == 0) return 1 ;
else if(y == z && x == 0) return 1 ;
else return 0;
}
int main()
{
int i , j , k;
while( scanf("%d %d %d" , &s , &m , &n) != EOF )
{
if(s == 0 || m == 0 || n == 0) break; // 输入条件
b[0] = s; b[1] = m ; b[2] = n; //填入数组,方便循环;
if(s % 2 == 1) { printf("NO\n"); continue; } //奇数直接GG
que[0].bot[0] = s;
que[0].bot[1] = 0;
que[0].bot[2] = 0;
que[0].step = 0;
book[0][0][0] = 1;
head = tail = 0;
tail ++;
flag = 0; //初始化
while(head < tail){
i = 0 ; j = 0 ;
for(i=0 ; i<3 ; i++){
for(j=0 ; j<3 ; j++){
que[tail] = que[head]; //初始化que[tail];
if(i != j){
// 自己不倒给自己;
if( que[head].bot[i] != 0 ){
// 倒水不能为0
// printf("qian %d %d %d\n",que[tail].bot[0],que[tail].bot[1] , que[tail].bot[2]);
//开始倒水 i -> j
if(que[tail].bot[i] + que[tail].bot[j] > b[j]) {
que[tail].bot[i] -= b[j] - que[tail].bot[j];
que[tail].bot[j] = b[j];
}
else{
que[tail].bot[j] += que[tail].bot[i];
que[tail].bot[i] = 0;
}
//倒水结束
// printf("hou %d %d %d\n\n",que[tail].bot[0],que[tail].bot[1] , que[tail].bot[2]);
//判断是否出现过这种情况;
if(book[que[tail].bot[0]][que[tail].bot[1]][que[tail].bot[2]] == 0){
//没有出现过
book[que[tail].bot[0]][que[tail].bot[1]][que[tail].bot[2]] = 1;
//标记
que[tail].step = que[head].step + 1;
//步数+1
if(Is(que[tail].bot[0] , que[tail].bot[1] , que[tail].bot[2])) {
flag = 1;
printf("%d\n",que[tail].step);
break;
}
//判断是否 达成目的
tail ++;
}
}
}
}
if(flag == 1) break;
}
head ++;
if(flag == 1) break;
}
if(flag == 0) printf("NO\n");
//重新归零book
for(i = 0 ; i < 101 ; i ++ )
for(j = 0 ; j < 101 ; j ++ )
for(k = 0 ; k < 101 ; k ++ )
book[i][j][k] = 0;
}
}
简单bfs
最开始没有思路 dfs的一塌糊涂
有思路之后实现代码又花了两天 做了三四遍
在实现代码方面有很大的问题;