Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 17080 | Accepted: 7238 | Special Judge |
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
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)
题意:两个瓶子的容量为a,b,一开始为空,可以通过三个操作,互道,加水,倒水,求答出水量为c的最少步骤,并打印这些步骤。
思路:简单的bfs每次搜索6个操作 用vis数组来判断这个状态是否已经出现过进行剪枝,同时用结构存储他的每一步操作以及他前一步的状态;
最后输出时在做个倒置处理,因为他是回溯路径,是反过来的
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define maxn 150
bool vis[maxn][maxn];
int a,b,c;
int n,m;
struct work
{
string s;
int x,y;
int lasta,lastb;
}ans[maxn][maxn],step[maxn+20000];
bool check(int x,int y)//检查是否已经得到c
{
if(x==c||y==c)return true;
return false;
}
void push(int& fx,int x,int &fy)//fy 倒入 fx x为容量
{
if(fx+fy>=x)
{
fy=fy-(x-fx);
fx=x;
}
else
{
fx=fx+fy;
fy=0;
}
}
void bfs()
{
int x,y;
x=0,y=0;
queue<pair<int,int> >q;
q.push(make_pair(0,0));
vis[x][y]=true;
while(!q.empty())
{
pair<int ,int >p;
p=q.front();
q.pop();
x=p.first;
y=p.second;
if(check(x,y))
{
m=x,n=y;
break;
}
push(x,a,y);//y 倒入 x
if(!vis[x][y])
{
vis[x][y]=true;
ans[x][y].x=2;
ans[x][y].y=1;
ans[x][y].lasta=p.first;
ans[x][y].lastb=p.second;
ans[x][y].s="POUR";
q.push(make_pair(x,y));
}
x=p.first;
y=p.second;
push(y,b,x);//x倒入y
if(!vis[x][y])
{
vis[x][y]=true;
ans[x][y].x=1;
ans[x][y].y=2;
ans[x][y].s="POUR";
ans[x][y].lasta=p.first;
ans[x][y].lastb=p.second;
q.push(make_pair(x,y));
}
x=p.first;
y=p.second;
x=a;//加满a
if(!vis[x][y])
{
vis[x][y]=true;
ans[x][y].x=1;
ans[x][y].y=0;
ans[x][y].s="FILL";
ans[x][y].lasta=p.first;
ans[x][y].lastb=p.second;
q.push(make_pair(x,y));
}
x=p.first;
y=p.second;
y=b;//加满b
if(!vis[x][y])
{
vis[x][y]=true;
ans[x][y].x=2;
ans[x][y].y=0;
ans[x][y].s="FILL";
ans[x][y].lasta=p.first;
ans[x][y].lastb=p.second;
q.push(make_pair(x,y));
}
x=p.first;
y=p.second;
x=0;//倒空a
if(!vis[x][y])
{
vis[x][y]=true;
ans[x][y].x=1;
ans[x][y].y=0;
ans[x][y].s="DROP";
ans[x][y].lasta=p.first;
ans[x][y].lastb=p.second;
q.push(make_pair(x,y));
}
x=p.first;
y=p.second;
y=0;//倒空b
if(!vis[x][y])
{
vis[x][y]=true;
ans[x][y].x=2;
ans[x][y].y=0;
ans[x][y].s="DROP";
ans[x][y].lasta=p.first;
ans[x][y].lastb=p.second;
q.push(make_pair(x,y));
}
}
}
int main()
{
while(~scanf("%d%d%d",&a,&b,&c))
{
m=-1;n=-1;
memset(vis,0,sizeof vis);
bfs();
if(m==-1||n==-1) printf("impossible\n");
int tot=0;
while(1)
{
step[tot].s=ans[m][n].s;
step[tot].x=ans[m][n].x;
step[tot].y=ans[m][n].y;
tot++;
int m1=ans[m][n].lasta;
int n1=ans[m][n].lastb;
m=m1,n=n1;
if(m==0&&n==0)break;
}
printf("%d\n",tot);
for(int i=tot-1;i>=0;i--)
{
cout<<step[i].s;
if(step[i].s=="POUR")printf("(%d,%d)\n",step[i].x,step[i].y);
else if(step[i].s=="FILL")printf("(%d)\n",step[i].x);
else if(step[i].s=="DROP")printf("(%d)\n",step[i].x);
}
}
return 0;
}