首页 > 代码库 > poj 3414 Pots(BFS+递归打印)

poj 3414 Pots(BFS+递归打印)

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


题意:
给你两个已知容量的杯A,B,和一个C,求最少步数得出C容量的水。FILL(i)倒满i   DROP(i):倒光i    POUR(i,j):倒i到j

题解:
BFS,共6种情况:FILL(A),  FILL(B),  DROP(A),   DROP(B),   POUR(A,B),   POUR(B,A).
  其中POUR(i,j)又分倒满和倒不满两种情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;

const int INF = 1000010;

using namespace std;

int n,m,x;

string s[3]= {"FILL","DROP","POUR"};
bool vis[110][110];

struct node
{
    int num,nex;///num==:1(FILL),2(DROP),3(POUR)
    int t;///倒满,guang的瓶子
    int x1,x2;///num=3时,由x1->x2
    int A,B;///a,b状态
    int step;///步数
} mp[1010];

queue<node>que;
int fro,nex;

bool bfs()
{
    while(que.size())
        que.pop();
    memset(vis,0,sizeof vis);
    node a;
    fro=0,nex=1;
    mp[fro].A=0,mp[fro].B=0;
    mp[fro].nex=-1;
    mp[fro].step=0;
    que.push(mp[fro]);
    vis[0][0]=1;
    while(que.size())
    {
        a=que.front();
        que.pop();
        if(a.A==x||a.B==x)
        {
            printf("%d\n",a.step);
            return 1;
        }
        mp[nex].nex=-1;
        if(!vis[n][a.B])///倒满A
        {
            mp[nex].nex=fro;
            mp[nex].A=n,mp[nex].B=a.B;
            mp[nex].num=1;
            mp[nex].t=1;
            mp[nex].step=a.step+1;
            que.push(mp[nex]);
            vis[n][a.B]=1;
            nex++;
        }
        if(!vis[a.A][m])///倒满B
        {
            mp[nex].nex=fro;
            mp[nex].A=a.A,mp[nex].B=m;
            mp[nex].num=1;
            mp[nex].t=2;
            mp[nex].step=a.step+1;
            que.push(mp[nex]);
            vis[a.A][m]=1;
            nex++;
        }
        if(!vis[0][a.B])///倒光A
        {
            mp[nex].nex=fro;
            mp[nex].A=0,mp[nex].B=a.B;
            mp[nex].num=2;
            mp[nex].t=1;
            mp[nex].step=a.step+1;
            que.push(mp[nex]);
            vis[0][a.B]=1;
            nex++;
        }
        if(!vis[a.A][0])///倒光B
        {
            mp[nex].nex=fro;
            mp[nex].A=a.A,mp[nex].B=0;
            mp[nex].num=2;
            mp[nex].t=2;
            mp[nex].step=a.step+1;
            que.push(mp[nex]);
            vis[a.A][m]=1;
            nex++;
        }
        if(a.A>0)///A->B
        {
            int t=m-a.B;
            if(a.A>t)//
            {
                mp[nex].A=a.A-t;
                mp[nex].B=m;
            }
            else    //
            {
                mp[nex].A=0;
                mp[nex].B=a.A+a.B;
            }
            if(!vis[mp[nex].A][mp[nex].B])
            {
                mp[nex].num=3;
                mp[nex].x1=1,mp[nex].x2=2;
                mp[nex].nex=fro;
                mp[nex].step=a.step+1;
                que.push(mp[nex]);
                vis[mp[nex].A][mp[nex].B]=1;
                nex++;
            }
        }
        if(a.B>0)///B->A
        {
            int t=n-a.A;
            if(a.B>t)//
            {
                mp[nex].A=n;
                mp[nex].B=a.B-t;
            }
            else    //
            {
                mp[nex].A=a.A+a.B;
                mp[nex].B=0;
            }
            if(!vis[mp[nex].A][mp[nex].B])
            {
                mp[nex].num=3;
                mp[nex].x1=2,mp[nex].x2=1;
                mp[nex].nex=fro;
                mp[nex].step=a.step+1;
                que.push(mp[nex]);
                vis[mp[nex].A][mp[nex].B]=1;
                nex++;
            }
        }
        fro++;
    }
    return 0;
}

void print(int x)
{
    if(mp[x].nex!=-1)
    {
        print(mp[x].nex);
        if(mp[x].num==1)
            printf("FILL(%d)\n",mp[x].t);
        else if(mp[x].num==2)
            printf("DROP(%d)\n",mp[x].t);
        else
            printf("POUR(%d,%d)\n",mp[x].x1,mp[x].x2);
    }
}

int main()
{
    while(cin>>n>>m>>x)
    {
        if(!bfs())
            cout<<"impossible"<<endl;
        else
        {
            print(fro);
        }
    }
    return 0;
}


poj 3414 Pots(BFS+递归打印)