Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 12571 | Accepted: 5300 | 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)
题目链接:http://poj.org/problem?id=3414
题目大意:给定两个容量分别为A、B的杯子和目标容量C,要求仅用两个杯子精确得到体积为C的油,如果能得到,给出最少操作步骤数和操作过程,如果不能,输出“impossible”。FILL(1)表示杯子1倒满油,DROP(1)表示把杯子1里的油倒空,POUR(1,2)表示把杯子1里的油倒入杯子2中。
解题思路:两次DFS,第一次搜素所有满足条件的情况中最少的操作次数,第二次搜索最短路径。
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=10000000;
int path[maxn],vis[1000000];
int A,B,C,ans,n;
bool p;
string s[6]={"DROP(1)","FILL(1)","DROP(2)","FILL(2)","POUR(1,2)","POUR(2,1)"}; //输出路径的预处理
void dfs(int sa,int sb,int cnt)
{
if(sa==C||sb==C) //如果一个杯子里的油量已经满足条件,更新ans并返回。
{
ans=min(ans,cnt);
return ;
}
for(int i=0;i<6;i++) //枚举6种情况
{
int a=sa;
int b=sb;
if(i==0) //把杯子1里的油倒出
{
if(a)
a=0;
else
continue;
}
else if(i==1) //把杯子1倒满油
{
if(a<A)
a=A;
else
continue;
}
else if(i==2) //把杯子2里的油倒出
{
if(b)
b=0;
else
continue;
}
else if(i==3) //把杯子2倒满油
{
if(b<B)
b=B;
else
continue;
}
else if(i==4) //把杯子1中的油倒入杯子2
{
if(a && b<B)
{
int cur=B-b;
if(a<cur)
{
b+=a;
a=0;
}
else
{
a-=cur;
b=B;
}
}
else
continue;
}
else if(i==5) //把杯子2中的油倒入杯子1
{
if( b && a<A)
{
int cur=A-a;
if(b<cur)
{
a+=b;
b=0;
}
else
{
b-=cur;
a=A;
}
}
else
continue;
}
int cur=a*1000+b; //cur用来表示当前两个杯子中油量的情况
if(vis[cur])continue; //如果这种情况处理过,跳过
vis[cur]=1; //标记
dfs(a,b,cnt+1);
vis[cur]=0;
}
}
void dfs_find_path(int sa,int sb,int cnt,int d) //d表示路径里的情况编号
{
if(sa==C||sb==C)
{
if(ans==cnt) //找到最小情况了,返回
p=true;
return ;
}
for(int i=0;i<6;i++)
{
int a=sa;
int b=sb;
if(i==0)
{
if(a)
a=0;
else
continue;
}
else if(i==1)
{
if(a<A)
a=A;
else
continue;
}
else if(i==2)
{
if(b)
b=0;
else
continue;
}
else if(i==3)
{
if(b<B)
b=B;
else
continue;
}
else if(i==4)
{
if(a && b<B)
{
int cur=B-b;
if(a<cur)
{
b+=a;
a=0;
}
else
{
a-=cur;
b=B;
}
}
else
continue;
}
else if(i==5)
{
if( b && a<A)
{
int cur=A-a;
if(b<cur)
{
a+=b;
b=0;
}
else
{
b-=cur;
a=A;
}
}
else
continue;
}
int cur=a*1000+b;
if(vis[cur])continue;
vis[cur]=1;
dfs_find_path(a,b,cnt+1,i);
if(p)
{
path[n++]=i;
return ;
}
vis[cur]=0;
}
}
int main(void)
{
p=false;
ans=maxn,n=0;
memset(vis,0,sizeof(vis)); //标记当前情况(两个杯子里的油量)是否被访问过。
scanf("%d%d%d",&A,&B,&C);
vis[0]=1;
dfs(0,0,0); //第一次dfs查找所有步骤数中最少的步骤数。
memset(vis,0,sizeof(vis));
vis[0]=1;
dfs_find_path(0,0,0,10); //第二次dfs查找最短的路径。
if(ans==maxn)
printf("impossible\n");
else
{
printf("%d\n",ans );
for(int i=n-1;i>=0;i--)
cout<<s[path[i]]<<endl;
}
}