三个水杯
-
描述
- 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
-
输入
-
第一行一个整数N(0<N<50)表示N组测试数据
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态
输出
- 每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1 样例输入
-
2 6 3 1 4 1 1 9 3 2 7 1 1
样例输出
-
3 -1
来源
- 经典题目 上传者
广度优先+对列
#include<stdio.h>
#include<string.h>
int va, vb, vc , ta, tb, tc;
bool vist[205][205][205];
typedef struct
{
int a;
int b;
int c;
int step;
}Node;Node queue[10000];
bool check(Node t2)
{
if(t2.a==ta&&t2.b==tb&&t2.c==tc)
return true;
else return false;
}
int BFS(int sa, int sb, int sc)
{
Node t1, t2;
int rear=0, font=0;
t1.a = sa;
t1.b = sb;
t1.c = sc;
t1.step = 0;
queue[rear++] = t1;
while(rear!=font)
{
t1 = queue[font++];
//a->b
if(t1.b!=vb&&t1.a)
{
if(t1.a<(vb-t1.b))
{
t2.a = 0;
t2.b = t1.b+t1.a;
t2.c = t1.c;
}else
{
t2.a = t1.a-(vb-t1.b);
t2.b = vb;
t2.c = t1.c;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//a->c
if(t1.c!=vc&&t1.a)
{
if(t1.a<(vc-t1.c))
{
t2.a = 0;
t2.b = t1.b;
t2.c = t1.c+t1.a;
}else
{
t2.a = t1.a-(vc-t1.c);
t2.b = t1.b;
t2.c = vc;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//b->a
if(t1.a!=va&&t1.b)
{
if(t1.b <= va - t1.a)
{
t2.b = 0;
t2.a = t1.b + t1.a;
t2.c = t1.c;
}
else
{
t2.b = t1.b - (va - t1.a);
t2.a = va;
t2.c = t1.c;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//b->c;
if(t1.c!=vc&&t1.b)
{
if(t1.b <= vc - t1.c)
{
t2.b = 0;
t2.a = t1.a;
t2.c = t1.b + t1.c;
}
else
{
t2.b = t1.b - (vc - t1.c);
t2.a = t1.a;
t2.c = vc;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//c-a
if(t1.a!=va&&t1.c)
{
if(t1.c <= va - t1.a)
{
t2.c = 0;
t2.a = t1.a + t1.c;
t2.b = t1.b ;
}
else
{
t2.c = t1.c - (va - t1.a);
t2.a = va;
t2.b = t1.b;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//c->b;
if(t1.b!=vb&&t1.c)
{
if(t1.c <= vb - t1.b)
{
t2.c = 0;
t2.b = t1.b + t1.c;
t2.a = t1.a ;
}
else
{
t2.c = t1.c - (vb - t1.b);
t2.b = vb;
t2.a = t1.a;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(vist,false,sizeof(vist));
scanf("%d%d%d",&va,&vb,&vc);
scanf("%d%d%d",&ta,&tb,&tc);
vist[va][0][0] = true;
printf("%d\n",BFS(va,0,0));
}
}
#include<stdio.h>
#include<string.h>
int va, vb, vc , ta, tb, tc;
bool vist[205][205][205];
typedef struct
{
int a;
int b;
int c;
int step;
}Node;Node queue[10000];
bool check(Node t2)
{
if(t2.a==ta&&t2.b==tb&&t2.c==tc)
return true;
else return false;
}
int BFS(int sa, int sb, int sc)
{
Node t1, t2;
int rear=0, font=0;
t1.a = sa;
t1.b = sb;
t1.c = sc;
t1.step = 0;
queue[rear++] = t1;
while(rear!=font)
{
t1 = queue[font++];
//a->b
if(t1.b!=vb&&t1.a)
{
if(t1.a<(vb-t1.b))
{
t2.a = 0;
t2.b = t1.b+t1.a;
t2.c = t1.c;
}else
{
t2.a = t1.a-(vb-t1.b);
t2.b = vb;
t2.c = t1.c;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//a->c
if(t1.c!=vc&&t1.a)
{
if(t1.a<(vc-t1.c))
{
t2.a = 0;
t2.b = t1.b;
t2.c = t1.c+t1.a;
}else
{
t2.a = t1.a-(vc-t1.c);
t2.b = t1.b;
t2.c = vc;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//b->a
if(t1.a!=va&&t1.b)
{
if(t1.b <= va - t1.a)
{
t2.b = 0;
t2.a = t1.b + t1.a;
t2.c = t1.c;
}
else
{
t2.b = t1.b - (va - t1.a);
t2.a = va;
t2.c = t1.c;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//b->c;
if(t1.c!=vc&&t1.b)
{
if(t1.b <= vc - t1.c)
{
t2.b = 0;
t2.a = t1.a;
t2.c = t1.b + t1.c;
}
else
{
t2.b = t1.b - (vc - t1.c);
t2.a = t1.a;
t2.c = vc;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//c-a
if(t1.a!=va&&t1.c)
{
if(t1.c <= va - t1.a)
{
t2.c = 0;
t2.a = t1.a + t1.c;
t2.b = t1.b ;
}
else
{
t2.c = t1.c - (va - t1.a);
t2.a = va;
t2.b = t1.b;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
//c->b;
if(t1.b!=vb&&t1.c)
{
if(t1.c <= vb - t1.b)
{
t2.c = 0;
t2.b = t1.b + t1.c;
t2.a = t1.a ;
}
else
{
t2.c = t1.c - (vb - t1.b);
t2.b = vb;
t2.a = t1.a;
}
if(!vist[t2.a][t2.b][t2.c])
{
t2.step = t1.step+1;
if(check(t2))
{
return t2.step;
}
vist[t2.a][t2.b][t2.c] = true;
queue[rear++] = t2;
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(vist,false,sizeof(vist));
scanf("%d%d%d",&va,&vb,&vc);
scanf("%d%d%d",&ta,&tb,&tc);
vist[va][0][0] = true;
printf("%d\n",BFS(va,0,0));
}
}