POJ 3414 Pots 记录路径的广搜

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 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)

题意:有两个空瓶 a,b是它们的容量,c是容量目标, 可以有三种操作 充满任意一瓶,倒空任意一瓶,将任意一瓶倒入另一瓶(能剩下但不能溢出);求任意一瓶的体积
达到目标体积所需要的最小操作数,并依此输出该操作。
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int m,n,k;
int vis[105][105];
char opr[20][20]= {" " , "FILL(1)" , "FILL(2)" , "DROP(1)" , "DROP(2)" , "POUR(1,2)" , "POUR(2,1)" };     //共6种操作
struct node
{
    int x,y,step;
    int w[200];                    //用来记录路径的数组数组
};
void bfs( )
{
    int i,j;
    int kx,ky;
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node now,next;
    now.x=0,now.y=0,now.step=0;
    q.push(now);
    vis[0][0]=1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==k||now.y==k)
        {
            cout<<now.step<<endl;
            for( i=1; i<=now.step; i++)
            {
                cout<<opr[now.w[i]]<<endl;
            }
            return;
        }
        for(i=0; i<6; i++)        // 共六种操作
        {
            if(i==0)
            {
                next.y=now.y;
                next.x=m;
                now.w[now.step+1]=1;           //随时更新
            }
            else if(i==1)
            {
                next.x=now.x;
                next.y=n;
                now.w[now.step+1]=2;
            }
            else if(i==2)
            {
                next.y=now.y;
                next.x=0;
                now.w[now.step+1]=3;
            }
            else if(i==3)
            {
                next.x=now.x;
                next.y=0;
                now.w[now.step+1]=4;
            }
            else if(i==4)
            {
                if(n-now.y>now.x)          //每种pour 应有两种情况
                {
                    next.x=0;
                    next.y=now.x+now.y;
                }
                else
                {
                    next.y=n;
                    next.x=now.x-n+now.y;
                }
                now.w[now.step+1]=5;
            }
            else if(i==5)
            {
                if(m-now.x>now.y)
                {
                    next.y=0;
                    next.x=now.x+now.y;
                }
                else
                {
                    next.x=m;
                    next.y=now.y-m+now.x;
                }
                now.w[now.step+1]=6;
            }
            if(vis[next.x][next.y]==1)
                continue;
            vis[next.x][next.y]=1;
            next.step=now.step+1;
            for(j=1; j<=next.step; j++)            //记录之前的行动
            {
                next.w[j]=now.w[j];
            }
            q.push(next);
        }
    }
    cout<<"impossible"<<endl;             //不要忘了这个情况
    return;
}
int main()
{
    int i;
    while(cin>>m>>n>>k)
    {
        bfs();
    }
    return 0;
}

通过自己对队列的理解成功写出了代码 还是挺开心的。。。

时间: 2024-11-05 21:45:21

POJ 3414 Pots 记录路径的广搜的相关文章

poj 1166 The Clocks 记录路径的广搜

题意: 给9个时钟的初始状态,和一些对某几个钟的操作,求最少经过几步能到目标状态(全指向12点). 分析: 明显的广搜,但实现起来的细节要注意:1.因为要记录路径,所以要在整个程序执行过程中扩展出的节点在输出路径前不能销毁, 故采用静态内存分配的方法(开node[600000],用get_node()创建节点.2.queue<node>比queue<int>要多花1别的时间. //poj 1166 //sep9 #include <iostream> #include

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: FILL(i)        f

广搜+输出路径 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(罐子)

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

poj 3278 Catch That Cow(广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

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 (输出路径的广搜)

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

(简单) 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打印路径)

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