POJ - 3414 Pots 【路徑打印+bfs】

 Pots

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)

題意:給你兩個容器的容量爲A和B的容器 和 一個給定的值C,找出能使其中任何一個容器裏的水恰好有C升,找出最少操作數並給出操作過程。

思路:廣搜+路徑打印;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int maxn=1005;
int a,b,c;
int len;
int inf=0x3f3f3f3f;
int vis[maxn][maxn];
struct Node
{
    char c;
    int i;
    int j;
    int from;

} path[maxn];
queue<pair<pair<int,int>,int> >q;
int bfs(int x,int y)
{
    vis[x][y]=1;
    q.push(make_pair(make_pair(x,y),len));
           while(!q.empty())
{
    int x1=q.front().first.first;
        int y1=q.front().first.second;
        int temp=q.front().second;
        if(x1==c||y1==c)
        {
            return temp;
        }
        q.pop();

        ///進行DROP
        if(vis[x1][0]==0)///先把2容器倒完
        {
            len++;
            vis[x1][0]=1;
            path[len].c='D';
            path[len].i=2;
            path[len].from=temp;
            q.push(make_pair(make_pair(x1,0),len));
        }
        if(vis[0][y1]==0)///把1容器倒完
        {
            len++;
            vis[0][y1]=1;
            path[len].c='D';
            path[len].i=1;
            path[len].from=temp;
            q.push(make_pair(make_pair(0,y1),len));
        }
        ///進行FILL
        if(vis[a][y1]==0)///先把1裝滿
        {
            len++;
            vis[a][y1]=1;
            path[len].c='F';
            path[len].i=1;
            path[len].from=temp;
            q.push(make_pair(make_pair(a,y1),len));

        }
        if(vis[x1][b]==0)///先把2裝滿
        {
            len++;
            vis[x1][b]=1;
            path[len].c='F';
            path[len].i=2;
            path[len].from=temp;
            q.push(make_pair(make_pair(x1,b),len));

        }
        ///POUR
        if(x1+y1<=a)///把2的全倒入1中,無剩餘
        {

            if(vis[x1+y1][0]==0)
            {
                len++;
                vis[x1+y1][0]=1;
                path[len].c='P';
                path[len].i=2;
                path[len].j=1;
                path[len].from=temp;
                q.push(make_pair(make_pair(x1+y1,0),len));

            }
        }
        else    ///把2的倒入1中,2中有剩餘
        {
            if(vis[a][x1+y1-a]==0)
            {
                len++;
                vis[a][x1+y1-a]=1;
                path[len].c='P';
                path[len].i=2;
                path[len].j=1;
                path[len].from=temp;
                q.push(make_pair(make_pair(a,x1+y1-a),len));
            }
        }
        if(x1+y1<=b)///把1全倒入2中,1無剩餘
        {
            if(vis[0][x1+y1]==0)
            {
                len++;
                vis[0][x1+y1]=1;
                path[len].c='P';
                path[len].i=1;
                path[len].j=2;
                path[len].from=temp;
                q.push(make_pair(make_pair(0,x1+y1),len));
            }
        }

        else  ///把1全倒入2中,1中有剩餘
        {
            if(vis[x1+y1-b][b]==0)
            {
                len++;
                vis[x1+y1-b][b]=1;
                path[len].c='P';
                path[len].i=1;
                path[len].j=2;
                path[len].from=temp;
                q.push(make_pair(make_pair(x1+y1-b,b),len));
            }
        }

    }
    return -1;
}

void prin(int s)
{
    int len2=0;
    int temp=s;
    int num[maxn];
    num[len2++]=temp;
    while(path[temp].from!=0)
    {
        num[len2++]=path[temp].from;
        temp=path[temp].from;
    }
    printf("%d\n",len2);
    for(int i=len2-1;i>=0;i--)
    {

        if(path[num[i]].c=='F')
        {
            printf("FILL(%d)\n",path[num[i]].i);
        }
        else if(path[num[i]].c=='P')
        {
             printf("POUR(%d,%d)\n",path[num[i]].i,path[num[i]].j);
        }
        else if(path[num[i]].c=='D')
        {
            printf("DROP(%d)\n",path[num[i]].i);
        }
    }


}
void init()
{

    memset(vis,0,sizeof(vis));
    memset(path,0,sizeof(path));
    while(!q.empty())
        q.pop();
        int len=0;
        path[len].from=0;
}
int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {
         init();
         int ans=bfs(0,0);
         if(ans==-1)
             printf("impossible\n");
         else
            prin(ans);
    }
   return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章