POJ 3414 Pots

本文介绍了一个经典的倒水问题,并提供了一种通过广度优先搜索算法来寻找最短操作序列的方法,以达到指定水量的目标。

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 ≤ i ≤ 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 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

/*
     问题描述:
             给定两个杯子的大小,一开始都为空,操作有三种,倒空,倒满和相互倒。问有没有可能倒出第三个数的水。
     解决方案:
             经典的倒水问题,不过在程序上可以暴力解决。因为两个杯子不管怎么倒的可能性只有6种,依次枚举6种状态,存在结构体中就ok,找到就输出,否则就impossible,属于广搜的简单题目。
*/

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <cstdio>
#include <map>
#include <set>
const int INF = 0x3f3f3f3f;

using namespace std;

char str[6][10]={"FILL(1)","FILL(2)","POUR(1,2)","POUR(2,1)","DROP(1)","DROP(2)"};
struct node
{
    int x,y;
    string num;
};
bool vis[110][110];

bool BFS(int x,int y,int z)
{
    queue<node>Q;
    node ans;
    ans.x = 0;ans.y = 0;ans.num = "";
    Q.push(ans);
    vis[0][0] = true;
    while(!Q.empty())
    {
        ans = Q.front();Q.pop();
        if(ans.x == z || ans.y == z)
        {
            cout<<ans.num.length() << endl;
            for(int i=0;ans.num[i]!='\0';i++)
                printf("%s\n",str[ans.num[i]-'0']);
            return false;
        }
        for(int i=0;i<6;i++)
        {
            node ant;
            if(i == 0 && !vis[x][ans.y])
            {
                ant.x = x;
                ant.y = ans.y;
                ant.num = ans.num + "0";
                Q.push(ant);
                vis[ant.x][ant.y]  = true;
            }
            if(i == 1 && !vis[ans.x][y])
            {
                ant.x = ans.x;
                ant.y = y;
                ant.num = ans.num + "1";
                Q.push(ant);
                vis[ant.x][ant.y]  = true;
            }
            if(i == 2)
            {
                int h = y - ans.y;
                if(ans.x >= h)
                {
                    ant.y = y;
                    ant.x = ans.x - h;
                }
                else
                {
                    ant.y = ans.y + ans.x;
                    ant.x = 0;
                }
                ant.num = ans.num + "2";
                if(!vis[ant.x][ant.y])
                {
                    Q.push(ant);
                    vis[ant.x][ant.y]  = true;
                }
            }
            if(i == 3)
            {
                int h = x - ans.x;
                if(ans.y >= h)
                {
                    ant.x = x;
                    ant.y = ans.y - h;
                }
                else
                {
                    ant.x = ans.x + ans.y;
                    ant.y = 0;
                }
                ant.num = ans.num + "3";
                if(!vis[ant.x][ant.y])
                {
                    Q.push(ant);
                    vis[ant.x][ant.y]  = true;
                }
            }
            if(i == 4 && !vis[0][ans.y])
            {
                ant.x = 0;
                ant.y = ans.y;
                ant.num = ans.num + "4";
                Q.push(ant);
                vis[ant.x][ant.y]  = true;
            }
            if(i == 5 && !vis[ans.x][0])
            {
                ant.x = ans.x;
                ant.y = 0;
                ant.num = ans.num + "5";
                Q.push(ant);
                vis[ant.x][ant.y]  = true;
            }
        }
    }
    return true;
}
int main()
{
    int x,y,z;
    while(~scanf("%d %d %d",&x,&y,&z))
    {
        memset(vis,false,sizeof(vis));
        if(BFS(x,y,z))
            printf("impossible\n");
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值