poj3414Pots(bfs倒水问题 输出路径)

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23545   Accepted: 9968   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

扫描二维码关注公众号,回复: 4141020 查看本文章

Northeastern Europe 2002, Western Subregion

重点是输出路径,用nodes数组记录一下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
#define maxn 200
#define maxm 1000005
int A,B,C;
int vis[maxn][maxn];
int dis[maxn][maxn];
int pre[maxn][maxn];
int cnt;
using namespace std;
struct node
{
    int a,b,t;
    node(){}
    node(int a,int b,int t ):a(a),b(b),t(t){}

}
nodes[maxm];
struct Ceil
{
    int a,b;
    Ceil()
    {

    }
    Ceil(int a,int b):a(a),b(b){}
};
Ceil bfs()
{
    cnt=0;
    queue<Ceil>q;
    memset(vis,0,sizeof(vis));
    dis[0][0]=0;
    vis[0][0]=1;
    q.push(Ceil(0,0));
    Ceil u;

    while(!q.empty())
    {
        u=q.front();
        q.pop();
        int a=u.a,b=u.b;

        int na,nb;
        for(int i=0;i<6;i++)
        {
           if(i==0)
            {
                na=A;
                nb=b;
            }
            else if(i==1)
            {
                nb=B;
                na=a;
            }
            else if(i==2)
            {
                na=0;
                nb=b;
            }
            else if(i==3)
            {
                nb=0;
                na=a;
            }
            else if(i==4)
            {
                if(a>=B-b)
                {
                    na=a-B+b;
                    nb=B;
                }
                else
                {
                    na=0;
                    nb=a+b;
                }
            }
            else if(i==5)
            {
                if(b>=A-a)
                {
                    nb=a+b-A;
                    na=A;
                }
                else
                {
                    nb=0;
                    na=a+b;
                }
            }


        if(!vis[na][nb])
        {
            vis[na][nb]=1;
            dis[na][nb]=dis[a][b]+1;

            nodes[cnt++]=node(a,b,i);
            pre[na][nb]=cnt-1;
            if(na==C||nb==C)
            {
                return Ceil(na,nb);
            }
            q.push(Ceil(na,nb));
        }
}
    }
    return Ceil(-1,-1);
}
int main()
{
    while(~scanf("%d%d%d",&A,&B,&C))
    {
        Ceil ceil=bfs();

        if(ceil.a==-1)
            printf("impossible\n");
            else
        {stack<int>s;
        int a=ceil.a;
        int b=ceil.b;
        while(a!=0||b!=0)
        {
            int p=pre[a][b];


            s.push(nodes[p].t);
            a=nodes[p].a;
            b=nodes[p].b;
        }
        printf("%d\n",s.size());
        while(!s.empty())
        {
            int x=s.top();
            s.pop();

            if(x==0||x==1)
                printf("FILL(%d)\n",x+1);
            if(x==2||x==3)
                printf("DROP(%d)\n",x-1);
            if(x==4)
            printf("POUR(1,2)\n");
             if(x==5)
               printf("POUR(2,1)\n");

        }
    }
}
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/84200112