您的位置:首页 > 其它

POJ 3414 Pots (经典bfs )

2014-08-06 14:14 344 查看
Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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 A, B, 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

两个壶,可以清空,可以加满,可以相互倒水,不过每次到要么这个壶水倒完,要么那个壶满了,要求一个壶有一定的水结束,输出步骤

bfs具体在代码中

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 105

struct stud{
int a,b;
int time;
string mv;
};

int a,b,c;

int vis

;

int judge(int x,int y)
{
    if(x==c||y==c)
        return 1;
    return 0;
}

stud bfs()
{
    struct stud next,cur;
    queue<stud>q;

    while(!q.empty())
        q.pop();

    cur.a=cur.b=0;
    cur.mv="";
    cur.time=0;

    if(cur.a==c||cur.b==c)
        return cur;

    q.push(cur);

    memset(vis,0,sizeof(vis));
    vis[0][0]=1;

    while(!q.empty())
    {
        cur=q.front();
        q.pop();

        if(cur.a<a&&!vis[a][cur.b]) //装满a
        {
            next=cur;
            next.a=a;
            next.time++;
            next.mv+="F1";
            vis[next.a][next.b];

            if(judge(next.a,next.b)) return next;

            q.push(next);
        }

        if(cur.b<b&&!vis[cur.a][b])   //装满b
        {

            next=cur;
            next.b=b;
            next.time++;
            next.mv+="F2";
            vis[next.a][next.b]=1;

            if(judge(next.a,next.b)) return next;
            q.push(next);
        }

        if(cur.a>0&&!vis[0][cur.b])   //倒出a
        {
            next.a=0;
            next.b=cur.b;
            next.mv=cur.mv;
            next.mv+="D1";
            next.time=cur.time+1;

            vis[next.a][next.b]=1;

            if(judge(next.a,next.b)) return next;
            q.push(next);
        }

        if(cur.b>0&&!vis[cur.a][0])    //倒出b
        {
            next.b=0;
            next.a=cur.a;
            next.mv=cur.mv;
            next.mv+="D2";
            next.time=cur.time+1;

            vis[next.a][next.b]=1;

            if(judge(next.a,next.b)) return next;
            q.push(next);
        }

        //a给b倒水

        if(cur.b<b&&cur.a>0&&cur.a+cur.b<=b&&!vis[0][cur.a+cur.b])
        {
            next.a=0;
            next.b=cur.a+cur.b;
            next.time=cur.time+1;
            next.mv=cur.mv+"P12";

            vis[next.a][next.b]=1;

             if(judge(next.a,next.b)) return next;
            q.push(next);
        }
        else
          if(cur.b<b&&cur.a>0&&cur.a+cur.b>b&&!vis[cur.a+cur.b-b][b])
          {
              next.a=cur.a+cur.b-b;
              next.b=b;
              next.time=cur.time+1;
              next.mv=cur.mv+"P12";

              vis[next.a][next.b]=1;

              if(judge(next.a,next.b)) return next;
              q.push(next);
          }

          //b给a倒水

          if(cur.b>0&&cur.a!=a&&cur.a+cur.b<=a&&!vis[cur.a+cur.b][0])
          {
              next.a=cur.a+cur.b;
              next.b=0;
              next.time=cur.time+1;
              next.mv=cur.mv+"P21";

              vis[next.a][next.b]=1;
               if(judge(next.a,next.b)) return next;
              q.push(next);
          }
          else
            if(cur.b>0&&cur.a!=a&&cur.a+cur.b>a&&!vis[a][cur.a+cur.b-a])
          {
              next.a=a;
              next.b=cur.a+cur.b-a;
              next.time=cur.time+1;
              next.mv=cur.mv+"P21";

              vis[next.a][next.b]=1;
               if(judge(next.a,next.b)) return next;
              q.push(next);

          }

    }

   struct stud ans;
    ans.a=-1;
    return ans;
}

int main()
{
        int i;
        scanf("%d%d%d",&a,&b,&c);
        struct stud cur;

        cur=bfs();

        if(cur.a==-1)
        {
            printf("impossible\n");
            return 0;
        }

        printf("%d\n",cur.time);

        int len=cur.mv.size();

        for(i=0;i<len;i++)
        {
            if(cur.mv[i]=='D')
            {
                printf("DROP(");
                i++;
                printf("%c)\n",cur.mv.at(i));
            }
            else
            if(cur.mv[i]=='F')
            {
                printf("FILL(");
                i++;
                printf("%c)\n",cur.mv.at(i));
            }
            else
            {
                printf("POUR(");
                i++;
                printf("%c",cur.mv.at(i));
                i++;
                printf(",%c)\n",cur.mv.at(i));
            }
        }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: