【POJ 3414】Pots(BFS)

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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)

题目大意

给你两个杯子的容量和一个目标容量,问你能否通过题目中所描述的队两个杯子操作达到目标容量。如果可以输出最小的操作步数和操作流程。否则,输出impossible。
所给的操作有:
FILL(i):装满第i个杯子
DROP(i):清空第i个杯子
POUR(i,j):将i杯子中的水倒到j杯子中

思路

通过分析可以发现倒水的方式总共就六种:FILL(1),FILL(2),DROP(1),DROP(2),POUR(1,2),POUR(2,1),枚举这六种倒法进行BFS就可以了,注意在进行POUR(i,j)这个操作中,要判断i杯子中的水能不能装满j杯子,如果能装满那么i杯子中水不一定会全部倒入j杯子中,自己可能还会有剩。如果装不满,那不用说了,i被子肯定被倒空。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>

using namespace std;

struct proc
{
    int x,y;
    int step;
    int flag;
    proc *pre;//用于记录这个状态由那个状态转换而来,即用于存取步骤。
};

queue<proc> q;
stack<int> r;
int a,b,e,vis[120][120],ans;

void bfs(int x,int y)
{
    proc c;
    proc t[320];
    c.x=c.y=c.flag=0;
    c.pre=NULL;
    c.step=0;
    q.push(c);
    vis[x][y]=1;
    int cnt=-1;
    while(!q.empty())
    {
        cnt++;
        t[cnt]=q.front();
        q.pop();
        for(int i=1;i<=6;i++)
        {
            switch(i)
            {
                case 1:                     //fill a
                    c.x = a;
                    c.y = t[cnt].y;
                    c.flag = 1;
                    break;
                case 2:                     //fill b
                    c.x = t[cnt].x;
                    c.y = b;
                    c.flag = 2;
                    break;
                case 3:                     //drop a
                    c.x = 0;
                    c.y = t[cnt].y;
                    c.flag = 3;
                    break;
                case 4:                     //drop b
                    c.x = t[cnt].x;
                    c.y = 0;
                    c.flag = 4;
                    break;
                case 5:                     //pour a to b
                    if(t[cnt].x > b-t[cnt].y)
                    {
                        c.x = t[cnt].x-(b-t[cnt].y);
                        c.y = b;
                    }
                    else
                    {
                        c.x = 0;
                        c.y = t[cnt].y+t[cnt].x;
                    }
                    c.flag = 5;
                    break;
                case 6:                     //pour b to a
                    if(t[cnt].y > a-t[cnt].x)
                    {
                        c.y = t[cnt].y - (a-t[cnt].x);
                        c.x = a;
                    }
                    else
                    {
                        c.x = t[cnt].x+t[cnt].y;
                        c.y = 0;
                    }
                    c.flag = 6;
                    break;  
            }
            if(vis[c.x][c.y]) continue;
            vis[c.x][c.y]=1;
            c.step=t[cnt].step+1;
            c.pre=&t[cnt];//记录步骤
            if(c.x==e||c.y==e)
            {
                ans=c.step;
                while(c.pre)
                {
                    r.push(c.flag);
                    c=*c.pre;
                }
                return;
            }
            q.push(c);
        }
    }
    ans=-1;
    return;
}

void print()
{
    if(ans!=-1)
    {
        printf("%d\n",ans); 
        while(!r.empty())
        {
            int i=r.top();
            r.pop();
            switch(i)
            {
                case 1:cout<<"FILL(1)"<<endl;break;
                case 2:cout<<"FILL(2)"<<endl;break;
                case 3:cout<<"DROP(1)"<<endl;break;
                case 4:cout<<"DROP(2)"<<endl;break;
                case 5:cout<<"POUR(1,2)"<<endl;break;
                case 6:cout<<"POUR(2,1)"<<endl;break;
            }
        }
    }
    else printf("impossible\n");
}

int main()
{
    while(~scanf("%d %d %d",&a,&b,&e))
    {
        while(!q.empty())
        {
            q.pop();
        }
        while(!r.empty())
        {
            r.pop();
        }
        memset(vis,0,sizeof(vis));
        bfs(0,0);
        print();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/iceiceicpc/article/details/52097354