Pots BFS 记忆路径

来源:互联网 发布:jquery post json解析 编辑:程序博客网 时间:2024/06/05 16:52

PotsTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

SubmitStatusPracticePOJ 3414

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 potj; after this operation either the pot j is full (and there may be some water left in the poti), or the pot i 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, and C. 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 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

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

给出两个容器,容量为A,B,问通过三种操作,最后某个容器中能否出现一个题目给出的水量。

题目中给出三种操作:

  • FILL(i)
  • DROP(i)   
  • POUR(i,j)


    但具体化之后,应该有六种操作,因为i,j可以取1和2,而且题目还要求操作次数最少,当然是BFS,每一步枚举六种操作即可,但麻烦的地方在于记忆路径,这里提供一种不需要二维数组存储的方法

    让每个节点自己保存从起点到达自己的路径。

    于是,为了操作方便事先将六种操作存储为字符串,然后给每一种操作编号,从0到5,然后在BFS的时候,让每个节点都记录操作序列,这里,可以使用string,因为在添加字符的时候比较简单,最后,如果找到了结果,就可以按照这个操作序列,依次输出对应的操作字符串,从而完成题目的要求。


    #include <iostream>#include <cmath>#include <stdio.h>#include <string>#include <cstring>#include <map>#include <set>#include <vector>#include <stack>#include <queue>#include <iomanip>#include <algorithm>#include <memory.h>using namespace std;string op[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};//一共只有六种操作,事先将字符串存好struct S{    string op;    int A,B;//两个容器中装入的水量    int step;//经过了多少步    S(int a,int b,int Step,string path)//构造函数    {        A=a;        B=b;        step=Step;        op=path;//核心部分,让每一个节点记录到达自己的路径,从而实现BFS的路径记忆    }    S(){}};bool visit[200][200];S bfs(int a,int b,int c)//返回值为S类型的BFS{    queue<S> q;    S temp;    int i,A,B,left;    q.push(S(0,0,0,""));//一开始两个容器都没有水,而且没有进行任何操作    visit[0][0]=1;    while(!q.empty())    {       temp=q.front();       q.pop();       if(temp.A==c||temp.B==c)//如果找到满足条件的,返回       {           return temp;       }       for(i=0;i<6;i++)//枚举六种操作       {           A=temp.A;           B=temp.B;           if(i==0)//FILL(1)           {               A=a;           }           else if(i==1)//FILL(2)            {                B=b;            }            else if(i==2)//DROP(1)            {                A=0;            }            else if(i==3)//DROP(2)            {                B=0;            }            else if(i==4)//POUR(1,2)            {                left=b-B;//B桶还有多少空间                if(A>left)//A中的水量大于B的剩余空间                {                    B=b;                    A=A-left;                }                else//A中的水量小于B的剩余空间                {                    B=B+A;                    A=0;                }            }            else if(i==5)//POUR(2,1)            {                left=a-A;                if(B>left)                {                    A=a;                    B=B-left;                }                else                {                    A=A+B;                    B=0;                }            }            if(visit[A][B]==0)//如果这个状态没有被访问过,入队列            {                q.push(S(A,B,temp.step+1,temp.op+char(i+'0')));                visit[A][B]=1;            }       }    }    temp.step=-1;//如果最后没有找到结果,就返回一个step为-1的节点    return temp;}int main(){    int A,B,C;    int i;    cin>>A>>B>>C;    memset(visit,0,sizeof(visit));    S result=bfs(A,B,C);    if(result.step!=-1)    {        cout<<result.step<<endl;        for(i=0;i<result.step;i++)        {            cout<<op[result.op[i]-'0']<<endl;//op中存储的是操作序列        }    }    else        cout<<"impossible"<<endl;    return 0;}








  • 0 0