Pots(BFS)

Pots
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit 
Status 
Practice 
POJ 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 ≤ ≤ 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)

和上面做的一道题十分相似(http://blog.csdn.net/u014665013/article/details/51334889),直接记录状态,只不过需要记录下路径,其实到现在为止还是不太清楚如何比较节省空间的记录路径,不过一般小的图直接用二维的前驱打表就够了。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define MAX 10005
#define INF 0x3f3f3f3f
#define LL long long
#define pii pair<int,int>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
///map<int,int>mmap;
///map<int,int >::iterator it;
using namespace std;
int vist[105][105],a,b,c;//a b c->S 可乐的体积 , N 和 M是两个杯子的容量
char opr[][10]={"","POUR(1,2)","POUR(2,1)","DROP(1)","DROP(2)","FILL(1)","FILL(2)"};
int lastOpr[105][105];
int lasta[105][105];
int lastb[105][105];

struct node
{
    int a,b;
    int step;//步数
};
int sum=0;

void myPrint(int a,int b){
  if(lastOpr[a][b]!=0){
    myPrint(lasta[a][b],lastb[a][b]);
    printf("%s\n",opr[ lastOpr[a][b] ]);
  }
}

void bfs()
{
    queue<node>que;
    memset(vist,0,sizeof(vist));
    node p1;
    p1.a=0 , p1.b=0 ,p1.step=0,lastOpr[0][0]=0;
    que.push(p1);
    vist[0][0]=1;

    while(!que.empty())
    {
        p1=que.front();
        que.pop();
       // printf("%d%d  ",p1.a,p1.b);
        if(p1.a==c||p1.b==c)
        {
            printf("%d\n",p1.step);
            myPrint(p1.a,p1.b);
            return;
        }
        //
        node p2;
        if(p1.a!=0)
        {//a倒入b
            if(p1.a>b-p1.b)
            {
                p2.a=p1.a-(b-p1.b);
                p2.b=b;
                p2.step=p1.step+1;
            }
            else
            {
                p2.a=0;
                p2.b=p1.b+p1.a;
                p2.step=p1.step+1;
            }
            if(!vist[p2.a][p2.b])
            {
                vist[p2.a][p2.b]=1;
                lastOpr[p2.a][p2.b]=1,lasta[p2.a][p2.b]=p1.a,lastb[p2.a][p2.b]=p1.b;
                que.push(p2);
            }
        }

        if(p1.b!=0)
        {//b倒入a
            if(p1.b>a-p1.a)
            {
                p2.b=p1.b-(a-p1.a);
                p2.a=a;
                p2.step=p1.step+1;
            }
            else
            {
                p2.b=0;
                p2.a=p1.a+p1.b;
                p2.step=p1.step+1;
            }
            if(!vist[p2.a][p2.b])
            {
                vist[p2.a][p2.b]=1;
                que.push(p2);
                lastOpr[p2.a][p2.b]=2,lasta[p2.a][p2.b]=p1.a,lastb[p2.a][p2.b]=p1.b;
            }
        }
        if(p1.a!=0){ ///a清空
          p2.a=0,p2.b=p1.b,p2.step=p1.step+1;
          if(!vist[p2.a][p2.b]){
              vist[p2.a][p2.b]=1;
              que.push(p2);
              lastOpr[p2.a][p2.b]=3,lasta[p2.a][p2.b]=p1.a,lastb[p2.a][p2.b]=p1.b;
          }
        }
        if(p1.b!=0){ ///b清空
          p2.a=p1.a,p2.b=0,p2.step=p1.step+1;
          if(!vist[p2.a][p2.b]){
              vist[p2.a][p2.b]=1;
              que.push(p2);
              lastOpr[p2.a][p2.b]=4,lasta[p2.a][p2.b]=p1.a,lastb[p2.a][p2.b]=p1.b;
          }
        }
        if(p1.a!=a){///a装满
          p2.a=a,p2.b=p1.b,p2.step=p1.step+1;
          if(!vist[p2.a][p2.b]){
              vist[p2.a][p2.b]=1;
              que.push(p2);
              lastOpr[p2.a][p2.b]=5,lasta[p2.a][p2.b]=p1.a,lastb[p2.a][p2.b]=p1.b;
          }
        }
        if(p1.b!=b){///b装满
          p2.a=p1.a,p2.b=b,p2.step=p1.step+1;
          if(!vist[p2.a][p2.b]){
              vist[p2.a][p2.b]=1;
              que.push(p2);
              lastOpr[p2.a][p2.b]=6,lasta[p2.a][p2.b]=p1.a,lastb[p2.a][p2.b]=p1.b;
          }
        }
    }
    printf("impossible\n");
}

int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {

        if(a==0&&b==0&&c==0)
            break;
        bfs();
    }
    return 0;
}

    原文作者:BFS
    原文地址: https://blog.csdn.net/u014665013/article/details/51336434
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞