刚开始我以为这是一道类似于博弈的题目,还在那儿思考有没有什么办法直接推出答案,看了别人的代码才知道这题没啥好办法,老老实实的模拟才是王道。
应该说是很有意思的搜索,每次根据之前的三个杯子的不同状态产生下一个状态,一直到搜索到结果或者返回-1为止。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 101
int map[N][N][N];
int x,y,z;
struct node
{
int x,y,z;
int step;
};
int BFS()
{
queue<node>q;
node cur,next;
cur.x=x;
cur.y=0;
cur.z=0;
cur.step=0;
q.push(cur);
memset(map,0,sizeof(map));
map[x][0][0]=1;
while(!q.empty())
{
cur=q.front();
q.pop();
next.step=cur.step+1;
if(cur.x==0&&cur.y==cur.z||cur.y==0&&cur.x==cur.z||cur.z==0&&cur.x==cur.y)
return cur.step;
if(cur.x)
{
if(cur.x>y-cur.y)
{
next.y=y;
next.z=cur.z;
next.x=cur.x-(y-cur.y);
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
else
{
next.y=cur.y+cur.x;
next.x=0;
next.z=cur.z;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
if(cur.x>z-cur.z)
{
next.z=z;
next.x=cur.x-(z-cur.z);
next.y=cur.y;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
else
{
next.x=0;
next.z=cur.z+cur.x;
next.y=cur.y;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
}
if(cur.y)
{
next.x=cur.x+cur.y;
next.y=0;
next.z=cur.z;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
if(cur.y>z-cur.z)
{
next.z=z;
next.y=cur.y-(z-cur.z);
next.x=cur.x;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
else
{
next.y=0;
next.z=cur.z+cur.y;
next.x=cur.x;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
}
if(cur.z)
{
next.x=cur.x+cur.z;
next.z=0;
next.y=cur.y;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
if(cur.z>y-cur.y)
{
next.y=y;
next.z=cur.z-(y-cur.y);
next.x=cur.x;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
else
{
next.y=cur.y+cur.z;
next.z=0;
next.x=cur.x;
if(!map[next.x][next.y][next.z])
{
q.push(next);
map[next.x][next.y][next.z]=1;
}
}
}
}
return -1;
}
int main()
{
int ans;
while(scanf("%d%d%d",&x,&y,&z),x||y||z)
{
if(x%2!=0)
{
printf("NO\n");
continue;
}
ans=BFS();
if(ans==-1)
printf("NO\n");
else
printf("%d\n",ans);
}
return 0;
}