PotsCrawling in process...Crawling failedTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
Hint
FILL(2)
POUR(2,1)
题意
有两个容量A,B一定的罐子,分别有装满,倒空,从一个瓶子倒入另一个瓶子这三种操作,求至少经过多少次操作后会使得其中一个瓶子中的水的体积为C
思路
因为求最少,可以用bfs模拟倒水
应该很容易就能分析出,对应于每一种状态的下一步只有六种情况:
一:用水池中的水装满 A
二:用水池中的水装满 B
三:把 A 中的水全部倒掉
四:把 B 中的水全部倒掉
五:把 A 中的水倒进 B, 但是主意不能溢出。
那么对应可能会有两种状态:用 f.x 表示 A 中的水, f.b 表示 B 中的水
如果 f.x+f.y >= B 则 f.x = f.x+f.y-B; f.y= B 【能够装满容器 B】
否则 k1 = k1+k2; k2 = 0 【不能能够装满容器 B】
六:把 B 中的水倒进 A 【不能溢出】
也有两种情况,分析同上;
一:用水池中的水装满 A
二:用水池中的水装满 B
三:把 A 中的水全部倒掉
四:把 B 中的水全部倒掉
五:把 A 中的水倒进 B, 但是主意不能溢出。
那么对应可能会有两种状态:用 f.x 表示 A 中的水, f.b 表示 B 中的水
如果 f.x+f.y >= B 则 f.x = f.x+f.y-B; f.y= B 【能够装满容器 B】
否则 k1 = k1+k2; k2 = 0 【不能能够装满容器 B】
六:把 B 中的水倒进 A 【不能溢出】
也有两种情况,分析同上;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int x;
int y;
int ans;
}q[5010],t,f;
int mv[110][110];
void bfs(int a,int b,int c)
{
int i;
int s = 0;
int e = 0;
t.x = 0;
t.y = 0;
t.ans = 0;
q[e++] = t;
mv[t.x][t.y] = 1;
while(s<e)
{
t = q[s++];
if(t.x == c || t.y == c)
{
printf("%d\n",t.ans);
return ;
}
for(i=0;i<6;i++)
{
if(i == 0)
{
f.x = a;
f.y = t.y;
}
if(i == 1)
{
f.x = t.x;
f.y = b;
}
if(i == 2)
{
f.x = 0;
f.y = t.y;
}
if(i == 3)
{
f.x = t.x;
f.y = 0;
}
if(i == 4)
{
f.x = t.x + t.y;
if(f.x>a)
{
f.y = f.x - a;
f.x = a;
}
else
{
f.y = 0;
}
}
if(i == 5)
{
f.y = t.x + t.y;
if(f.y>b)
{
f.x = f.y - b;
f.y = b;
}
else
{
f.x = 0;
}
}
if(mv[f.x][f.y] == 0)
{
f.ans = t.ans + 1;
q[e++] = f;
mv[f.x][f.y] = 1;
}
}
}
printf("impossible\n");
return ;
}
int main()
{
int a,b,c;
while(~scanf("%d %d %d",&a,&b,&c))
{
memset(mv,0,sizeof(mv));
bfs(a,b,c);
}
return 0;
}