POJ 3414 - Pots (DFS)

13 篇文章 0 订阅

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19042 Accepted: 8027 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

题意:

就是有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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值