#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL __int64
#define mod 9973
#define N 100010
int n,m,s;
bool vis[102][110][110];
struct asd{
int cup1;
int cup2;
int cup3;
int step;
};
asd q[N];
int head,tail;
void bfs()
{
memset(vis,0,sizeof(vis));
head=0;tail=1;
q[head].cup1=s;
q[head].cup2=0;
q[head].cup3=0;
q[head].step=0;
vis[s][0][0]=1;
int x,y,z;
while(head<tail)
{
int a=q[head].cup1;
int b=q[head].cup2;
int c=q[head].cup3;
if(a==b&&a==s/2)
{
printf("%d\n",q[head].step);
return;
}
//cup1->cup2|cup1->cup3|先cup1->cup2后cup1->cup3
if(a>0)
{
//cup1->cup2只能倒满,或者倒不满
//不会出现倒不满的现象,只会倒不倒;
x=a-(n-b);
y=n;
z=c;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
x=0;
y=n;
z=m;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
x=(a-(m-c));
y=b;
z=m;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
}
//cup2->cup1|cup2->cup3|\cup2->cup1;
if(b>0)
{
x=a+b;
y=0;
z=c;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
if(b+c>=m)
{
x=a;
y=b-(m-c);
z=m;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
x=s-m;
y=0;
z=m;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
}
else
{
x=a;
y=0;
z=b+c;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
}
}
//cup3->cup1|cup3->cup2\\cup3->cup1
if(c>0)
{
x=a+c;
y=b;
z=0;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
if(b+c>=n)
{
x=a;
y=n;
z=c-(n-b);
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
x=s-n;
y=n;
z=0;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
}
else
{
x=a;
y=b+c;
z=0;
if(!vis[x][y][z])
{
vis[x][y][z]=1;
q[tail].cup1=x;
q[tail].cup2=y;
q[tail].cup3=z;
q[tail].step=q[head].step+1;
tail++;
}
}
}
head++;
}
printf("NO\n");
}
int main()
{
while(~scanf("%d%d%d",&s,&n,&m))
{
if(s==0&&n==0&&m==0)
break;
if(n<m)
{
int temp=n;
n=m;
m=temp;
}
if(n+m<s||n+m>s||s%2==1)
{
puts("NO");
continue;
}
bfs();
}
return 0;
}
hdoj1495简单BFS
最新推荐文章于 2019-05-22 20:28:55 发布