POJ 3414 Pots (经典bfs )

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

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

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[N][N];

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;
}

POJ 3414 Pots (经典bfs )

时间: 2024-07-31 02:33:39

POJ 3414 Pots (经典bfs )的相关文章

poj 3414 Pots【bfs+回溯路径 正向输出】

题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12080   Accepted: 5104   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operation

POJ - 3414 Pots (BFS+路径记录)

题目链接:http://poj.org/problem?id=3414 题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a. 题解:模拟一下操作,就好了. 1 #include <queue> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7

POJ 3414 Pots 暴力,bfs 难度:1

http://poj.org/problem?id=3414 记录瓶子状态,广度优先搜索即可 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn=101; int n,m; typedef unsigned long long ull; int A,B,C; int vis[maxn][maxn]; int ans[maxn][maxn][ma

(简单) POJ 3414 Pots,BFS+记录路径。

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

poj 3414 Pots(BFS+递归打印)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10255   Accepted: 4333   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ 3414 Pots(BFS+回溯)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

poj 3414 Pots (bfs+线索)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

poj 3414 pots (bfs 倒水问题)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int vis[120][120]; int vl,vr; int ok; int key; struct node { int l,r; int fa; int op; }; node str[100000]; int ans[100000]; void bfs() {

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom