POJ3414 Pots

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20243 Accepted: 8600 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 iis 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)

没想到做了一个寒假连搜索题都没做完orz

又是bfs。

题目大意是给你两个瓶子的容量,你可以进行灌水和倒水,也可以两个瓶子相互倒。

要求出最短的倒出c的路径。

方法是bfs,每一次将6种路径逐一尝试加入队列。

输出路径的方法是每一个element带有一个string类的成员变量。

每次加入队列时便将在末尾加对应路径。

用一个字符串数组存储路径最后输出即可。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
int a,b,c;
const int maxn = 1e5;
using namespace std;
bool used[200][200];
typedef pair<int,int> P;
typedef struct node
{
    P point;
    string result;
    node(P pp,string str)
    {
        point = pp;
        result = str;
    }
} Node;
char way[][10]  = {"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
void bfs()
{
    used[0][0] = 1;
    Node t = Node(P(0,0),"");
    queue<Node> Q;
    Q.push(t);
    while(Q.size())
    {
        Node n = Q.front();
        Q.pop();
        if(n.point.first == c || n.point.second == c)
        {
            cout<<n.result.size()<<endl;
            for(int i = 0; i < n.result.size(); i++)
                cout<<way[n.result[i]-'0']<<endl;
            return ;
        }
        for(int i = 0; i < 6; i++)
        {
            switch(i)
            {
            case 0:
                if(n.point.first < a && used[a][n.point.second] == 0)
                {
                    used[a][n.point.second] = 1;
                    Q.push(Node(P(a,n.point.second),n.result + "0"));
                }
                break;
            case 1:
                if(n.point.second < b && used[n.point.first][b] == 0)
                {
                    used[n.point.first][b] = 1;
                    Q.push(Node(P(n.point.first,b),n.result + "1"));
                }
                break;
            case 2:
                if(n.point.first > 0 && used[0][n.point.second] == 0)
                {
                    used[0][n.point.second] = 1;
                    Q.push(Node(P(0,n.point.second),n.result + "2"));
                }
                break;
            case 3:
                if(n.point.second > 0 && used[n.point.first][0] == 0)
                {
                    used[n.point.first][0] = 1;
                    Q.push(Node(P(n.point.first,0),n.result + "3"));
                }
                break;
            case 4:
                if(n.point.first > 0 && n.point.second < b)
                {
                    int pa = 0,pb = n.point.second + n.point.first;
                    if(pb > b)
                    {
                        pa = pb - b;
                        pb = b;
                    }
                    if(used[pa][pb] == 0)
                    {
                        Q.push(Node(P(pa,pb),n.result + "4"));
                        used[pa][pb] = 1;
                    }

                }
                break;
            case 5:
                if(n.point.first < a && n.point.second > 0)
                {
                    int pb = 0,pa = n.point.second + n.point.first;
                    if(pa > a)
                    {
                        pb = pa - a;
                        pa = a;
                    }
                    if(used[pa][pb] == 0)
                    {
                        used[pa][pb] = 1;
                        Q.push(Node(P(pa,pb),n.result + "5"));
                    }
                }
                break;
            default:
                break;
            }
        }

    }
    cout<<"impossible"<<endl;

}
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>a>>b>>c;
    bfs();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值