Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 19046 | Accepted: 8028 | 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)
Source
题意:有两个体积分别为A,B的 Pot ,给它们分别标号1,2,刚开始两个 Pot 都是空的,对 Pot 有如下几种操作
- FILL( i ) 将标号为 i 的 Pot 注满水
- DROP( i ) 将标号为 i 的Pot 倒空
- POUR( i , j ) 将标号为 i 的 Pot 中的水倒入标号为 j 的 Pot 中,若 j Pot 倒满了则停止继续倒水,剩余的水依旧在 i Pot 中
思路:DFS,记录到每一个状态的最少步数,当到达该状态的步数比之前记录的最小步数来的小时才能继续下搜
#include<iostream>
#include<stdio.h>
#include<string.h>
#define LL long long
using namespace std;
const int maxn = 1e5+10;
int a,b,C,ans,load[maxn],ans_load[maxn],vis[111][111];
void dfs(int A,int B,int step)
{
if(step>=ans&&ans!=-1) return;
if(A==C||B==C)
{
if(ans==-1||step<ans)
{
for(int i=0; i<step; i++) ans_load[i] = load[i];
ans = step;
}
return ;
}
//fill 1
load[step] = 1;
if(vis[a][B]>step+1||vis[a][B]==-1) vis[a][B] = step+1,dfs(a,B,step+1);
//fill 2
load[step] = 2;
if(vis[A][b]>step+1||vis[A][b]==-1) vis[A][b] = step+1,dfs(A,b,step+1);
//drop 1
load[step] = 3;
if(vis[0][B]>step+1||vis[0][B]==-1) vis[0][B]=step+1,dfs(0,B,step+1);
//drop 2
load[step] = 4;
if(vis[A][0]>step+1||vis[A][0]==-1) vis[A][0]= step+1,dfs(A,0,step+1);
//1 - > 2
int AA,BB;
load[step] = 5;
AA = B + A - b;
if(AA<0) AA = 0,BB = B + A;
else BB = b;
if(vis[AA][BB]>step+1||vis[AA][BB]==-1) vis[AA][BB] = step+1,dfs(AA,BB,step+1);
//2 - > 1
load[step] = 6;
BB = B + A - a;
if(BB<0) AA = B + A,BB = 0;
else AA = a;
if(vis[AA][BB]>step+1||vis[AA][BB]==-1) vis[AA][BB] = step+1,dfs(AA,BB,step+1);
}
int main()
{
scanf("%d%d%d",&a,&b,&C);
for(int i=0;i<=a;i++) for(int j=0;j<=b;j++) vis[i][j] = -1;
ans = -1;
dfs(0,0,0);
if(ans==-1)
{
printf("impossible\n");
return 0;
}
printf("%d\n",ans);
for(int i=0; i<ans; i++)
{
switch(ans_load[i])
{
case 1:
printf("FILL(1)\n");
break;
case 2:
printf("FILL(2)\n");
break;
case 3:
printf("DROP(1)\n");
break;
case 4:
printf("DROP(2)\n");
break;
case 5:
printf("POUR(1,2)\n");
break;
default:
printf("POUR(2,1)\n");
break;
}
}
return 0;
}