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
|
/*
题意很简单 给你两个瓶子容量分别为a、b
然后可以进行六种操作 看看能使其中一个瓶子中装的水恰好是c升。
These are all integers in the range from 1 to 100 and C≤max(A,B).
一看就是典型的bfs()求最少的操作次数。
六个操作分别为FILL(1)、FILL(2)、DROP(1)、DROP(2)、POUR(1,2)、POUR(2,1)
相当于典型bfs中的6个方向 可以用for(int i=0;i<6;i++)进行控制种类进行入队列。
然后路径的话可以用二维数组来存 进行一一对应的关系。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 110;
struct node
{int x,y,step;
};
struct nope
{int x,y,type;
}R[maxn][maxn];
bool visited[maxn][maxn];
int a,b,c;
int flag=0;
void output(int x,int y)
{
stack<nope>p;
while(1)
{
if(x==0&&y==0) break;
p.push(R[x][y]);
int temp_x=R[x][y].x;
int temp_y=R[x][y].y;
x=temp_x;
y=temp_y;
}
while(!p.empty())
{
switch(p.top().type){
case 0:printf("FILL(1)\n");break;
case 1:printf("FILL(2)\n");break;
case 2:printf("DROP(1)\n");break;
case 3:printf("DROP(2)\n");break;
case 4:printf("POUR(1,2)\n");break;
case 5:printf("POUR(2,1)\n");break;
}
p.pop();
}
}
void bfs()
{
queue<node>Q;
Q.push((node){0,0,0});
visited[0][0]=true;
while(!Q.empty())
{
node head=Q.front();
Q.pop();
for(int i=0; i<6; i++)
{
node next;
if(i==0&&head.x!=a)//FILL(1)
{
next.x=a;
next.y=head.y;
next.step=head.step+1;
if(!visited[next.x][next.y])
{
visited[next.x][next.y]=true;
Q.push(next);
R[next.x][next.y].x=head.x;
R[next.x][next.y].y=head.y;
R[next.x][next.y].type=0;
}
}
else if(i==1&&head.y!=b)//FILL(2)
{
next.x=head.x;
next.y=b;
next.step=head.step+1;
if(!visited[next.x][next.y])
{
visited[next.x][next.y]=true;
Q.push(next);
R[next.x][next.y].x=head.x;
R[next.x][next.y].y=head.y;
R[next.x][next.y].type=1;
}
}
else if(i==2&&head.x!=0)//DROP(1)
{
next.x=0;
next.y=head.y;
next.step=head.step+1;
if(!visited[next.x][next.y])
{
visited[next.x][next.y]=true;
Q.push(next);
R[next.x][next.y].x=head.x;
R[next.x][next.y].y=head.y;
R[next.x][next.y].type=2;
}
}
else if(i==3&&head.y!=0)//DROP(2)
{
next.x=head.x;
next.y=0;
next.step=head.step+1;
if(!visited[next.x][next.y])
{
visited[next.x][next.y]=true;
Q.push(next);
R[next.x][next.y].x=head.x;
R[next.x][next.y].y=head.y;
R[next.x][next.y].type=3;
}
}
else if(i==4&&head.x!=0&&head.y!=b)//POUR(1,2)
{
int temp=min(head.x,b-head.y);
next.x=head.x-temp;
next.y=head.y+temp;
next.step=head.step+1;
if(!visited[next.x][next.y])
{
visited[next.x][next.y]=true;
Q.push(next);
R[next.x][next.y].x=head.x;
R[next.x][next.y].y=head.y;
R[next.x][next.y].type=4;
}
}
else if(i==5&&head.x!=a&&head.y!=0)//POUR(2,1)
{
int temp=min(a-head.x,head.y);
next.x=head.x+temp;
next.y=head.y-temp;
next.step=head.step+1;
if(!visited[next.x][next.y])
{
visited[next.x][next.y]=true;
Q.push(next);
R[next.x][next.y].x=head.x;
R[next.x][next.y].y=head.y;
R[next.x][next.y].type=5;
}
}
if(next.x==c||next.y==c)
{
flag=1;
printf("%d\n",next.step);
output(next.x,next.y);
return ;
}
}
}
puts("impossible");
}
int main(void)
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
flag=0;
memset(visited,false,sizeof(visited));
bfs();
}
return 0;
}