【POJ】3414Pots(单bfs)

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

Northeastern Europe 2002, Western Subregion

题目大意:

给出AB两个杯子通过三个操作让两个杯子其中之一调和出C升的水量,其中有三个操作,

1.把某杯子的水清空

2.把某杯子的水倒满

3.把某杯子的水倒入另一个杯子中

根据这三个操作(实际操作可 化为6个,毕竟两杯子不相同)求出得到C升水的最短序列,

最后要求输出操作的!

 思路:这个就是bfs 枚举每一层的所有状态到一个队列中,不过这个要求输出操作顺序,中间要每次都保存,后面用栈方便输出。

我们用一个结构node来表示到达(i,j)状态时,它的前一个状态是node.i和node.j且操作时node.k.其中用pre[i][j]来指示node中的节点。

其中用vis[i][j]和dist[i][j]来标记和记录最短路径长,用pre[i][j]=x来指示(i,j)状态的前驱信息在nodes[x]节点中。

之后就是枚举状态,0~5

注意:A杯和B杯的状态在任何时候都不可能是: A非空且不满 B非空且不满.所以我们可以通过A和B的当前状态推出他们的前驱.所以可以不用nodes记录前驱也行.

代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
const int maxa=100+5;
const int maxb=100+5;
const int maxn=110000;

int A,B,C;
int vis[maxa][maxb],dist[maxa][maxb],pre[maxa][maxb];

struct Cell
{
    int a,b;
    Cell(int a,int b):a(a),b(b){}
};

int cnt;

struct Node
{
    int a,b,t;
    Node(){};
    Node(int a,int b,int t):a(a),b(b),t(t){}
}node[maxn];

queue<Cell> Q;

Cell bfs()
{
    cnt=0;
    memset(vis,0,sizeof(vis));
    dist[0][0]=0;
    vis[0][0]=1;
    Q.push(Cell(0,0));

    while(!Q.empty())
    {
        Cell cell=Q.front();
        Q.pop();

        int a=cell.a;
        int b=cell.b;
        for(int d=0;d<6;d++)
        {
            int na,nb;
            if(d==0) na=A,nb=b;
            else if(d==1) na=a,nb=B;
            else if(d==2) na=0,nb=b;
            else if(d==3) na=a,nb=0;
            else if(d==4)
            {
                int all=a+b;
                na=all>=B?all-B:0;
                nb=all>=B?B:all;
            }
            else if(d==5)
            {
                int all=a+b;
                na=all>=A?A:all;
                nb=all>=A?all-A:0;
            }
            //选择条件完毕
            if(vis[na][nb]==0)
            {
                vis[na][nb]=1;
                dist[na][nb]=dist[a][b]+1;
                node[cnt++]=Node(a,b,d);
                pre[na][nb]=cnt-1;
                if(na==C||nb==C)//满足条件退出
                {
                    return Cell(na,nb);
                }
                Q.push(Cell(na,nb));
            }
        }
    }
    return Cell(-1,-1);
}

int main()
{
    scanf("%d%d%d",&A,&B,&C);
    Cell cell=bfs();
    if(cell.a==-1)printf("impossible\n");
    else
    {
        stack<int> S;
        int a=cell.a,b=cell.b;
        while(a!=0||b!=0)
        {
            int p=pre[a][b];
            S.push(node[p].t);
            a=node[p].a,b=node[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);
            else if(x==2||x==3) printf("DROP(%d)\n",x-1);
            else if(x==4) printf("POUR(1,2)\n");
            else if(x==5)printf("POUR(2,1)\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wentong_Xu/article/details/84207216