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
|
题意:
就是有4个操作,倒来倒去求获得c的水量。
POINT:
因为数据量小,DFS BFS都可以。感觉DFS写起来比较简单。就写了DFS,后来发现不是这样。因为要输出路径。
我就复制了一下DFS,在跑一次输出路径,所以代码很长。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <string.h>
using namespace std;
int mp[111][111];
int a,b,c;
int ans=9999999;
void dfs(int x,int y,int now)
{
if(x==0&&y==0&&now!=0) return;
if(now>=ans) return;
if(mp[x][y]<=now&&mp[x][y]!=0) return;
mp[x][y]=now;
if(x==c||y==c)
{
ans=min(now,ans);
return;
}
if(x>0)
{
dfs(0,y,now+1);
}
if(y>0)
{
dfs(x,0,now+1);
}
if(x<a)
{
dfs(a,y,now+1);//fill a
if(y!=0&&a-x>y)
{
dfs(x+y,0,now+1);
}
if(y!=0&&a-x<=y)
{
dfs(a,y-(a-x),now+1);
}
}
if(y<b)
{
dfs(x,b,now+1);//fill b
if(x!=0&&b-y>x)
{
dfs(0,x+y,now+1);
}
if(x!=0&&b-y<=x)
{
dfs(x-(b-y),b,now+1);
}
}
}
int ans1;
int flag=0;
stack<int> q;
void dfs1(int x,int y,int now)
{
if(x==0&&y==0&&now!=0) return;
if(now>ans1) return;
if(mp[x][y]<=now&&mp[x][y]!=0) return;
mp[x][y]=now;
if(x==c||y==c)
{
ans=min(now,ans);
if(ans1==ans)
{
flag=1;
return;
}
return;
}
if(x>0)
{
dfs1(0,y,now+1);
if(flag==1)
{
q.push(1);
//printf("DROP(%d)\n",1);
return;
}
}
if(y>0)
{
dfs1(x,0,now+1);
if(flag==1)
{
q.push(2);
// printf("DROP(%d)\n",2);
return;
}
}
if(x<a)
{
dfs1(a,y,now+1);//fill a
if(flag==1)
{
q.push(3);
// printf("FILL(%d)\n",1);
return;
}
if(y!=0&&a-x>y)
{
dfs1(x+y,0,now+1);
if(flag==1)
{
q.push(4);
// printf("POUR(%d,%d)\n",2,1);
return;
}
}
if(y!=0&&a-x<=y)
{
dfs1(a,y-(a-x),now+1);
if(flag==1)
{
q.push(5);
// printf("POUR(%d,%d)\n",2,1);
return;
}
}
}
if(y<b)
{
dfs1(x,b,now+1);//fill b
if(flag==1)
{
q.push(6);
// printf("FILL(%d)\n",2);
return;
}
if(x!=0&&b-y>x)
{
dfs1(0,x+y,now+1);
if(flag==1)
{
q.push(7);
// printf("POUR(%d,%d)\n",1,2);
return;
}
}
if(x!=0&&b-y<=x)
{
dfs1(x-(b-y),b,now+1);
if(flag==1)
{
q.push(8);
// printf("POUR(%d,%d)\n",1,2);
return;
}
}
}
}
int main()
{
while(~scanf("%d %d %d",&a,&b,&c))
{
ans=9999999;
memset(mp,0,sizeof mp);
dfs(0,0,0);
ans1=ans;
if(ans1==9999999)
{
printf("impossible\n");
continue;
}
flag=0;
memset(mp,0,sizeof mp);
dfs1(0,0,0);
printf("%d\n",ans);
while(!q.empty())
{
int a=q.top();
q.pop();
if(a==1)
{
printf("DROP(%d)\n",1);
}
else if(a==2)
{
printf("DROP(%d)\n",2);
}
else if(a==3)
{
printf("FILL(%d)\n",1);
}
else if(a==4||a==5)
{
printf("POUR(%d,%d)\n",2,1);
}
else if(a==6)
{
printf("FILL(%d)\n",2);
}
else if(a==7||a==8)
{
printf("POUR(%d,%d)\n",1,2);
}
}
}
return 0;
}