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 4Sample Output
6Hint
FILL(2)POUR(2,1)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int p[100][100];
int i,j;
int a,b,c;
struct node
{
int x,y,count;
}q[5000];
void BFS()
{
int s = 0,e = 0;
struct node t,f;
t.x = 0;
t.y = 0;
t.count = 0;
q[e++] = t;
p[t.x][t.y] = 1;
while(s<e)
{
t = q[s++];
if(t.x == c || t.y == c)
{
printf("%d\n",t.count);
return ;
}
for(i=0;i<6;i++)
{
if(i == 0)
{
f.x = a;
f.y = t.y;
}
else if(i == 1)
{
f.x = t.x;
f.y = b;
}
else if(i == 2)
{
f.x = 0;
f.y = t.y;
}
else if(i == 3)
{
f.x = t.x;
f.y = 0;
}
else 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;
}
}
else 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(p[f.x][f.y] == 0)
{
f.count = t.count + 1;
q[e++] = f;
p[f.x][f.y] = 1;
}
}
}
printf("impossible\n");
return ;
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
memset(p,0,sizeof(p));
BFS();
}
return 0;
}