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)
<pre name="code" class="html">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[110][110];
int a,b,c;
struct node
{
int k1,k2,step;
};
void bfs()
{
queue<node>Q;
node next={0,0,0};
vis[0][0]=1;
Q.push(next);
while(!Q.empty())
{
node f=Q.front();Q.pop();
if(f.k1==c||f.k2==c)
{
next.step=f.step;
printf("%d\n",next.step);
return ;
}
for(int i=1;i<=6;i++)
{
if(i==1)//fill(1)
{
next.k1=a;
next.k2=f.k2;
}
if(i==2)
{
next.k1=f.k1;
next.k2=b;
}
if(i==3)//drop(1)
{
next.k1=0;
next.k2=f.k2;
}
if(i==4)
{
next.k2=0;
next.k1=f.k1;
}
if(i==5)//pour(1,2)
{
if(f.k1+f.k2<=b)
{
next.k1=0;
next.k2=f.k1+f.k2;
}
else
{
next.k1=f.k1+f.k2-b;
next.k2=b;
}
}
if(i==6)
{
if(f.k1+f.k2<=a)
{
next.k1=f.k1+f.k2;
next.k2=0;
}
else
{
next.k2=f.k1+f.k2-a;
next.k1=a;
}
}
if(!vis[next.k1][next.k2])
{
vis[next.k1][next.k2]=1;
next.step=f.step+1;
Q.push(next);
}
}
}
puts("impossible");
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}
或者
</pre><pre code_snippet_id="454159" snippet_file_name="blog_20140819_3_1876495" name="code" class="html"><pre name="code" class="html">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[110][110];
int a,b,c;
struct node
{
int k1,k2,step;
};
void bfs()
{
queue<node>Q;
node next= {0,0,0};
vis[0][0]=1;
Q.push(next);
while(!Q.empty())
{
node f=Q.front();
Q.pop();
if(f.k1==c||f.k2==c)
{
printf("%d\n",f.step);
return ;
}
if(f.k1!=a)//fill(1)
{
next.k1=a;
next.k2=f.k2;
next.step=f.step+1;
if(!vis[next.k1][next.k2])
{
vis[next.k1][next.k2]=1;
Q.push(next);
}
}
if(f.k2!=b)
{
next.k1=f.k1;
next.k2=b;
next.step=f.step+1;
if(!vis[next.k1][next.k2])
{
vis[next.k1][next.k2]=1;
Q.push(next);
}
}
if(f.k1!=0)//drop(1)
{
next.k1=0;
next.k2=f.k2;
next.step=f.step+1;
if(!vis[next.k1][next.k2])
{
vis[next.k1][next.k2]=1;
Q.push(next);
}
}
if(f.k2!=0)
{
next.k2=0;
next.k1=f.k1;
next.step=f.step+1;
if(!vis[next.k1][next.k2])
{
vis[next.k1][next.k2]=1;
Q.push(next);
}
}
if(f.k1!=0&&f.k2!=b)//pour(1,2)
{
if(f.k1+f.k2<=b)
{
next.k1=0;
next.k2=f.k1+f.k2;
next.step=f.step+1;
}
else
{
next.k1=f.k1+f.k2-b;
next.k2=b;
next.step=f.step+1;
}
if(!vis[next.k1][next.k2])
{
vis[next.k1][next.k2]=1;
Q.push(next);
}
}
if(f.k1!=a&&f.k2!=0)
{
if(f.k1+f.k2<=a)
{
next.k1=f.k1+f.k2;
next.k2=0;
next.step=f.step+1;
}
else
{
next.k2=f.k1+f.k2-a;
next.k1=a;
next.step=f.step+1;
}
if(!vis[next.k1][next.k2])
{
vis[next.k1][next.k2]=1;
Q.push(next);
}
}
}
puts("impossible");
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}