hdu 1495 非常可乐 广搜

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
int vi[][][];
using namespace std;
struct Point{
int a,b,c,v;
Point(int x,int y,int z,int vv){a=x,b=y,c=z,v=vv;}
};
void pu(int &a,int &b,int c)
{
if(b==c)
return ;
if(a+b<=c)
b+=a,a=;
else
a-=c-b,b=c;
}
int Min=;
void bfs(int a,int b,int c)
{
int i,j,x,y,z;
queue<Point> q;
Point p(,,c,);
q.push(p);
int s=c,n=a,m=b;
while(!q.empty())
{
p=q.front();
q.pop();
x=p.a,y=p.b,z=p.c;
//cout<<x<<" "<<y<<" "<<z<<"* "<<p.v<<endl;
if(x*==s)
{
if(y*==s||z*==s)
{
if(p.v<Min)
Min=p.v;
}
else
{
if(p.v+<Min)
Min=p.v+;
}
}
if(y*==s)
{
if(x*==s||z*==s)
{
if(p.v<Min)
Min=p.v;
}
else//cout<<x<<" "<<y<<" "<<" "<<s<<" "<<Min<<" "<<p.v+1<<endl;
if(p.v+<Min)
{
Min=p.v+; }
}
if(z*==s)
{
if(x*==s||y*==s)
{
if(p.v<Min)
Min=p.v;
}
else
{
if(p.v+<Min)
Min=p.v+;
}
}
vi[x][y][z]=;
int t1,t2;
t1=x,t2=y;
pu(t1,t2,m);
if(!vi[t1][t2][z])
q.push(Point(t1,t2,z,p.v+));
t1=x,t2=z;
pu(t1,t2,s);
if(!vi[t1][y][t2])
q.push(Point(t1,y,t2,p.v+));
t1=y,t2=x;
pu(t1,t2,n);
if(!vi[t2][t1][z])
q.push(Point(t2,t1,z,p.v+));
t1=y,t2=z;
pu(t1,t2,s);
if(!vi[x][t1][t2])
q.push(Point(x,t1,t2,p.v+));
t1=z,t2=x;
pu(t1,t2,n);
if(!vi[t2][y][t1])
q.push(Point(t2,y,t1,p.v+));
t1=z,t2=y;
pu(t1,t2,m);
if(!vi[x][t2][t1])
q.push(Point(x,t2,t1,p.v+));
}
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c),a||b||c)
{
if(a%==||a==||b==||c==)
{
printf("NO\n");
continue;
}
memset(vi,,sizeof(vi));
Min=;
bfs(b,c,a);
if(Min==)
printf("NO\n");
else
printf("%d\n",Min);
}
return ;
}
上一篇:《Java编程思想》笔记 第十二章 通过异常处理错误


下一篇:CentOS虚拟机中安装VMWare Tools