Pots

来源:互联网 发布:大数据时代的小数据 编辑:程序博客网 时间:2024/05/01 15:19

题目链接:poj.org/problem?id=3414

Pots

Time Limit: 1000MS Memory Limit: 65536K

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 ≤ 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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).

Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. These are all integers in the range from 1 to 100 andC≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operationsK. The followingK 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

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)

题目描述:给定两个空容器,容量分别为A和B,有三种不同操作,问能否量出C公升水,若能,输出最短步骤。思路如下:广搜每一个可能的状态,代码如下:

#include<stdio.h>#include<iostream>#include<string.h>#include<queue>#define N 1000using namespace std;bool vis[N][N];struct nodes{    int fa,pos,op,a,b;                                            //fa记录父节点编号,pos记录当前节点位置} node[N];int A,B,C;void BFS(){    memset(vis,0,sizeof(vis));    queue<struct nodes>q;    int tn=0,ta,tb;    node[tn].fa=-1;    node[tn].pos=0;    node[tn].a=node[tn].b=0;    struct nodes tmp;    q.push(node[tn]);    vis[node[tn].a][node[tn].b]=true;    bool flag=false;    tn++;    while(!q.empty())    {        tmp=q.front();        q.pop();        if(tmp.a==C||tmp.b==C)        {            flag=true;            break;        }        struct nodes t;        t.fa=tmp.pos;        if(!vis[A][tmp.b])        {            vis[A][tmp.b]=1;            t.a=A;            t.b=tmp.b;            t.op=1; //FILL 1            t.pos=tn;            node[tn++]=t;            q.push(t);        }        if(!vis[tmp.a][B])        {            vis[tmp.a][B]=1;            t.a=tmp.a;            t.b=B;            t.op=2; //FILL 2            t.pos=tn;            node[tn++]=t;            q.push(t);        }        if(!vis[0][tmp.b])        {            vis[0][tmp.b]=1;            t.a=0;            t.b=tmp.b;            t.op=3;//DROP 1            t.pos=tn;            node[tn++]=t;            q.push(t);        }        if(!vis[tmp.a][0])        {            vis[tmp.a][0]=1;            t.a=tmp.a;            t.b=0;            t.op=4;//DROP 2            t.pos=tn;            node[tn++]=t;            q.push(t);        }        ta=tmp.a>=(B-tmp.b)?tmp.a-(B-tmp.b):0;        tb=tmp.a>=(B-tmp.b)?B:tmp.a+tmp.b;        if(!vis[ta][tb])        {            vis[ta][tb]=1;            t.a=ta;            t.b=tb;            t.op=5;//pour 1 2            t.pos=tn;            node[tn++]=t;            q.push(t);        }        ta=tmp.b>=(A-tmp.a)?A:tmp.a+tmp.b;        tb=tmp.b>=(A-tmp.a)?tmp.b-(A-tmp.a):0;        if(!vis[ta][tb])        {            vis[ta][tb]=1;            t.a=ta;            t.b=tb;            t.op=6;//pour 2 1            t.pos=tn;            node[tn++]=t;            q.push(t);        }    }    if(!flag)    {        cout<<"impossible"<<endl;        return ;    }    int op[N],i;                         /*从当前节点回到祖先节点,op数组用于保存该路径节点的编号*/    while(tmp.fa!=-1)    {        op[i++]=tmp.pos;        tmp=node[tmp.fa];    }    cout<<i<<endl;    for(int j=i-1;j>=0;j--)    {        if(node[op[j]].op==1)            cout<<"FILL(1)"<<endl;        else if(node[op[j]].op==2)            cout<<"FILL(2)"<<endl;        else if(node[op[j]].op==3)            cout<<"DROP(1)"<<endl;        else if(node[op[j]].op==4)            cout<<"DROP(2)"<<endl;        else if(node[op[j]].op==5)            cout<<"POUR(1,2)"<<endl;        else if(node[op[j]].op==6)            cout<<"POUR(2,1)"<<endl;    }}int main(){    cin>>A>>B>>C;    BFS();    return 0;}


0 0
原创粉丝点击