Pots
Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots. 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. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. 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 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1) Source
Northeastern Europe 2002, Western Subregion
|
题意:给出两个无刻度的瓶子,对于每个瓶子有三种操作,倒掉瓶子里面的水、注满水、将瓶子里面的水倒入另一个瓶子中,多出来的水留在该瓶子中。求得到给定的水最少步数,并输出操作。
解题思路:这个题是个典型的BFS题,用两个变量模拟瓶子中的水数,然后对于每个瓶子的三种操作写出来,用flag数组标记已经进行过的状态,最后得到给定的水。前面的很简单,主要是记录路径。因为我们用BFS查找肯定有不同的分支,冒然的用数字存肯定会把左右的查找过的点全部存下。我们找到的最后一个节点,从他到起点是最短的路无疑,那么对于这条路上的所有节点到起点都是最短,那么我们存储的时候只需要把此节点的操作和上一个节点的下标存入即可,最后可以用递归或者栈输出。我这里用的栈。以下是我的AC代码
#include <cstdio>
#include <iostream>
using namespace std;
#include <queue>
#include <stack>
#include <memory.h>
#define maxn 105
int flag[maxn][maxn];
int a,b,n;
struct trip
{
int x,y,step;
int pre;
int id;
int cz;
} t[maxn];
void bfs()
{
memset(flag,0,sizeof(flag));
queue<trip> q;
stack<int> s;
trip now,next;
t[0].pre=-1;
int cnt=0;
now.x=0;
now.y=0;
now.step=0;
now.pre=-1;
now.id=0;
now.cz=0;
flag[now.x][now.y]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==n||now.y==n)
{
printf("%d\n",now.step);
int z=now.id;
while(t[z].pre!= -1)
{
s.push(t[z].cz);
z=t[z].pre;
}
for(int i=0;i<now.step;i++)
{
int a = s.top();
if(a==1) printf("FILL(1)");
else if(a==2) printf("DROP(1)");
else if(a==3) printf("POUR(1,2)");
else if(a==4) printf("FILL(2)");
else if(a==5) printf("DROP(2)");
else if(a==6) printf("POUR(2,1)");
s.pop();
printf("\n");
}
return;
}
if(now.x)
{
next=now;
next.x=a;// 注满1号杯
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=1;
t[next.id].pre=now.id;
q.push(next);
}
next=now;
next.x=0;// 清空1号杯
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=2;
t[next.id].pre=now.id;
q.push(next);
}
next=now;
next.x=now.x-(min(b,now.y+now.x)-now.y);
next.y=min(b,now.y+now.x); //1倒入2
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=3;
t[next.id].pre=now.id;
q.push(next);
}
}
else
{
next=now;
next.x=a;//注满1号杯
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=1;
t[next.id].pre=now.id;
q.push(next);
}
}
if(now.y)
{
next=now;
next.y=b;//注满2号杯
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=4;
t[next.id].pre=now.id;
q.push(next);
}
next=now;
next.y=0;//清空2号杯
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=5;
t[next.id].pre=now.id;
q.push(next);
}
next=now;
next.y=now.y-(min(a,now.y+now.x)-now.x);
next.x=min(a,now.y+now.x);//2倒入1
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=6;
t[next.id].pre=now.id;
q.push(next);
}
}
else
{
next=now;
next.y=b; //注满2号杯
if(!flag[next.x][next.y])
{
next.step=now.step+1;
flag[next.x][next.y]=1;
next.id=++cnt;
t[next.id].cz=4;
t[next.id].pre=now.id;
q.push(next);
}
}
}
printf("impossible\n");
}
int main()
{
scanf("%d%d%d",&a,&b,&n);
bfs();
return 0;
}