POJ - 3414 - Pots (DFS)

Pots
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:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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 AB, 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的 Pot ,给它们分别标号1,2,刚开始两个 Pot 都是空的,对 Pot 有如下几种操作

  1. FILL( i ) 将标号为 i 的 Pot 注满水
  2. DROP( i ) 将标号为 i 的Pot 倒空
  3. POUR( i , j ) 将标号为 i 的 Pot 中的水倒入标号为 j 的 Pot 中,若 j Pot 倒满了则停止继续倒水,剩余的水依旧在 i Pot 中
求使其中一个 Pot 内有 C 体积的水至少需要操作几次,并输出每次操作,若不能达到,则输出impossible


思路: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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值