poj 3414 Pots

探讨使用两个不同容量的水壶通过填充、倾倒及相互倒水等操作获得目标水量的问题,采用广度优先搜索策略寻找最短操作路径。
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14638 Accepted: 6165 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

做了3个小时,呵呵!!
题意:
/*
有二个水壶,对水壶有三种操作,1)FILL(i),将i水壶的水填满,2)DROP(i),将水壶i中的水全部倒掉,3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量。如果不可能则输出impossible。初始时两个水壶是空的,没有水。
*/
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

struct node
{
    int aw, bw, cw;
    int step;
};
stack<int>s;
int xx[102][102], yy[102][102];
queue<node> q;
bool vis[102][102];
int visi[102][102];
int x, y;
int a, b, c;
string display[9] = {" ", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(2,1)", "POUR(2,1)","POUR(1,2)","POUR(1,2)"};

int bfs( )
{
    while ( !q.empty() )
        q.pop();
    node temp;
    temp.aw = temp.bw = temp.cw = 0;
    temp.step = 0;
    q.push(temp);
    vis[0][0] = 1;
    while ( !q.empty() )
    {
        node head, tail;
        head = q.front();
        q.pop();

        if ( !vis[a][head.bw] )
        {
            visi[a][head.bw] = 1;
            xx[a][head.bw] = head.aw;
            yy[a][head.bw] = head.bw;
            tail.aw = a;
            tail.bw = head.bw;
            tail.step = head.step+1;
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
            q.push(tail);
            vis[tail.aw][tail.bw] = 1;
        }

        if ( !vis[head.aw][b] )
        {
            visi[head.aw][b] = 2;
            xx[head.aw][b] = head.aw;
            yy[head.aw][b] = head.bw;
            tail.aw = head.aw;
            tail.bw = b;
            tail.step = head.step+1;
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
            q.push(tail);
            vis[tail.aw][tail.bw] = 1;
        }

        if ( !vis[0][head.bw] )
        {
            visi[0][head.bw] = 3;
            xx[0][head.bw] = head.aw;
            yy[0][head.bw] = head.bw;
            tail.aw = 0;
            tail.bw = head.bw;
            tail.step = head.step+1;
            q.push(tail);
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
            vis[tail.aw][tail.bw] = 1;
        }

        if ( !vis[head.aw][0] )
        {
            visi[head.aw][0] = 4;
            xx[head.aw][0] = head.aw;
            yy[head.aw][0] = head.bw;
            tail.aw = head.aw;
            tail.bw = 0;
            tail.step = head.step+1;
            q.push(tail);
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
            vis[tail.aw][tail.bw] = 1;
        }

        if ( head.bw <= a-head.aw )
        {
            if ( !vis[head.aw+head.bw][0] )
            {
                visi[head.aw+head.bw][0] = 5;
            xx[head.aw+head.bw][0] = head.aw;
            yy[head.aw+head.bw][0] = head.bw;
                tail.aw = head.aw+head.bw;
                tail.bw = 0;
                tail.step = head.step+1;
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
                q.push(tail);
                vis[tail.aw][tail.bw] = 1;
            }
        }
        else
        {
            if ( !vis[a][head.bw-(a-head.aw)] )
            {
                visi[a][head.bw-(a-head.aw)] = 6;
            xx[a][head.bw-(a-head.aw)] = head.aw;
            yy[a][head.bw-(a-head.aw)] = head.bw;
                tail.aw = a;
                tail.bw = head.bw-(a-head.aw);
                tail.step = head.step+1;
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
                q.push(tail);
                vis[tail.aw][tail.bw] = 1;
            }
        }

        if ( head.aw <= b-head.bw )
        {
            if ( !vis[0][head.aw+head.bw] )
            {
                visi[0][head.aw+head.bw] = 7;
            xx[0][head.aw+head.bw] = head.aw;
            yy[0][head.aw+head.bw] = head.bw;
                tail.bw = head.aw+head.bw;
                tail.aw = 0;
                tail.step = head.step+1;
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
                q.push(tail);
                vis[tail.aw][tail.bw] = 1;
            }
        }
        else
        {
            if ( !vis[head.aw-(b-head.bw)][b] )
            {
                visi[head.aw-(b-head.bw)][b] = 8;
            xx[head.aw-(b-head.bw)][b] = head.aw;
            yy[head.aw-(b-head.bw)][b] = head.bw;
                tail.aw = head.aw-(b-head.bw);
                tail.bw = b;
                tail.step = head.step+1;
                if ( tail.aw == c||tail.bw == c )
                {
                    x = tail.aw;
                    y = tail.bw;
                    return tail.step;
                }
                q.push(tail);
                vis[tail.aw][tail.bw] = 1;
            }
        }
    }
    return 0;
}

void change(int g)
{
        x = xx[x][y];
        y = yy[x][y];
}

int main()
{
    scanf ( "%d %d %d", &a, &b, &c );
    memset( vis, 0, sizeof(vis) );
    int ok = bfs( );
    if ( ok == 0 )
        printf ( "impossible\n" );
    else
    {
        printf ( "%d\n", ok );
        for (int i = ok-1;i >= 0; i-- )
        {
            s.push(visi[x][y]);
            int t = x;
            x = xx[x][y];
            y = yy[t][y];
        }
        while ( !s.empty() )
        {
            cout << display[s.top()]<<endl;
            s.pop();
        }
    }
}

代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值