poj 3414 pots (bfs+路径记录)

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11703   Accepted: 4955   Special Judge

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)

好爽,一遍ac

/*************************************************************************
    > File Name: code/2015summer/searching/H.cpp
    > Author: 111qqz
    > Email: [email protected]
    > Created Time: 2015年07月27日 星期一 09时11分28秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int N=1E2+5;
int A,B,C;
int d[N][N];
bool flag;
struct node
{
    int d,opt,par,prea,preb;
}q[N][N];

void print(int x,int y)
{
 //    cout<<"x:"<<x<<"y:"<<y<<endl;
    if (q[x][y].prea!=-1&&q[x][y].preb!=-1)
    {
//       cout<<"who is 111qqz"<<endl;
      print(q[x][y].prea,q[x][y].preb);
      if (q[x][y].opt==1){
        printf("FILL(%d)\n",q[x][y].par);
      }
      if (q[x][y].opt==2)
      {
        printf("DROP(%d)\n",q[x][y].par);
      }
      if (q[x][y].opt==3)
      {
        printf("POUR(%d,%d)\n",q[x][y].par,3-q[x][y].par);
      }
    }

}
void bfs()
{
    memset(q,-1,sizeof(q));
    queue<int>a;
    queue<int>b;
    a.push(0);
    b.push(0);
    q[0][0].d=0;
    while (!a.empty()&&!b.empty())
    {
      int av = a.front();a.pop();
      int bv = b.front();b.pop();
//       cout<<"av:"<<av<<"bv:"<<bv<<endl;
      if (av==C||bv==C)
      {
        flag = true;
        //cout<<"yeah~~~~~~~~~~~~~~~"<<endl;
        cout<<q[av][bv].d<<endl;
//         cout<<"prea:"<<q[av][bv].prea<<"preb:"<<q[av][bv].preb<<endl;
        print(av,bv);
          return;
      }
      if (av<A&&q[A][bv].d==-1)
      {
        q[A][bv].d=q[av][bv].d+1;
        q[A][bv].opt=1;
        q[A][bv].par=1;
        q[A][bv].prea=av;
        q[A][bv].preb=bv;
        a.push(A);
        b.push(bv);
      }
      if (av>0&&q[0][bv].d==-1)
      {
        q[0][bv].d=q[av][bv].d+1;
        q[0][bv].opt=2;
        q[0][bv].par=1;
        q[0][bv].prea=av;
        q[0][bv].preb=bv;
        a.push(0);
        b.push(bv);

      }
      if (bv<B&&q[av][B].d==-1)
      {
        q[av][B].d=q[av][bv].d+1;
        q[av][B].opt=1;
        q[av][B].par=2;
        q[av][B].prea = av;
        q[av][B].preb = bv;
        a.push(av);
        b.push(B);

      }
      if (bv>0&&q[av][0].d==-1)
      {
        q[av][0].d=q[av][bv].d+1;
        q[av][0].opt=2;
        q[av][0].par=2;
        q[av][0].prea=av;
        q[av][0].preb=bv;
        a.push(av);
        b.push(0);
      }

      if (av+bv<=B&&q[0][av+bv].d==-1)
      {
        q[0][av+bv].d=q[av][bv].d+1;
        q[0][av+bv].opt=3;
        q[0][av+bv].par=1;
        q[0][av+bv].prea=av;
        q[0][av+bv].preb=bv;
        a.push(0);
        b.push(av+bv);
      }
      if (av+bv>B&&q[av-(B-bv)][B].d==-1)   //把1往2里倒入的两种情况
      {

        int tmp = av-(B-bv);
        q[tmp][B].d=q[av][bv].d+1;
        q[tmp][B].opt=3;
        q[tmp][B].par=1;
        q[tmp][B].prea=av;
        q[tmp][B].preb=bv;
        a.push(tmp);
        b.push(B);
      }

      if (bv+av<=A&&q[av+bv][0].d==-1)
      {
        q[av+bv][0].d=q[av][bv].d+1;
        q[av+bv][0].opt=3;
        q[av+bv][0].par=2;
        q[av+bv][0].prea=av;
        q[av+bv][0].preb=bv;
        a.push(av+bv);
        b.push(0);
      }
      if (bv+av>A&&q[A][bv-(A-av)].d==-1)
      {
        int tmp = bv-(A-av);
        q[A][tmp].d=q[av][bv].d+1;
        q[A][tmp].opt=3;
        q[A][tmp].par=2;
        q[A][tmp].prea=av;
        q[A][tmp].preb=bv;
        a.push(A);
        b.push(tmp);
      }
    }
}
int main()
{

    flag = false;
    cin>>A>>B>>C;
    bfs();
    if (!flag)
    {
      cout<<"impossible"<<endl;
    }

    return 0;
}
时间: 2024-08-06 20:04:46

poj 3414 pots (bfs+路径记录)的相关文章

POJ 3414 Pots (BFS + 记录路径)

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10300 Accepted: 4362 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)        fill th

POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A.B,经过6种操作使获得C升水,求最少操作数: 思路:广搜.最少操作数很简单,练习一下打印路径:打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int A,B,C; int shortest[150][150];//更新最短步数

POJ 3414 Pots(BFS 倒水)

题意  你有两个容积分别为a,b杯子  你每次可以将某个杯子中的水倒满或者倒掉或者倒到另一个杯子  问能否通过这两个杯子量出c容量的水 和上一个倒可乐问题类似  只是这个操作更多了点  将两个杯子中各含有的水作为状态  每出队列一个状态  将所有可能到达的状态入队  直到有一个杯子里面水的体积为c   打印路径直接递归就行了 #include <map> #include <cstdio> #include <cstring> using namespace std;

poj 3414 Pots(BFS)(简单题)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11551   Accepted: 4900   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/DFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   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)        fi

广搜+输出路径 POJ 3414 Pots

POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   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)

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(罐子)

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

迷宫bfs+路径记录

给定5*5的地图,1表示墙,0表示空地,从左上角走到左下角 是把所有的可走路径都记录下来了,但是 搜索有递归与非递归形式 本代码使用非递归形式 bfs+路径记录 对于num[i].pre=head的理解是他存在很多条路,每个点是从上一个点走过来的,但总存在一条路是到达终点的,所以,只需要得到到终点的一个head就可以顺着这个路径标记回去了 #include <iostream> #include <cstdio> using namespace std; char a[10][10