(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; 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<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include <math.h>
#include<queue>
#define ll long long
#define met(a,b) memset(a,b,sizeof(a));
#define N 405
using namespace std;
char str[6][50]={"FILL(2)","FILL(1)","POUR(2,1)","POUR(1,2)","DROP(1)","DROP(2)"};
int n,m,k,f;
int vis[N][N];
struct node
{
    int x,y,t;
    char s[500];///记录路径过程   s[i] i代表第i步做str[s[i]]这个操作
};
void dfs()
{
    queue<node> Q;
    node q,p;
    q.x=0;
    q.y=0;
    q.t=0;
    strcpy(q.s,"0");///记得初始化
    vis[0][0]=1;
    Q.push(q);
    while(Q.size())
    {
        p=Q.front();
        Q.pop();
        if(p.x==k || p.y==k)
        {
            f=1;
            printf("%d\n",p.t);
            for(int i=1; i<=p.t; i++)
                printf("%s\n",str[p.s[i]-‘0‘]);///根据p.s储存的输出路径
            break;
        }
        for(int i=0; i<6; i++)///每次一共就有六种操作可做,符合条件的才实行
        {
            q.t=p.t+1;
            q.x=0;
            q.y=0;
            strcpy(q.s,p.s);
            q.s[q.t]=i+‘0‘;
            if(i==0 && !p.y)///第二个杯子为空,装满
            {
                q.x=p.x;
                q.y=m;

            }
            else if(i==1 && !p.x)///第一个杯子为空,装满
            {
                q.x=n;
                q.y=p.y;
            }
            else if(i==2 && p.x<n&&p.y)///第一个杯子不满,第二个杯子不空,把第二个杯子的水倒入第一个杯子里
            {
                if(p.x+p.y<=n)
                {
                    q.x=p.x+p.y;
                    q.y=0;
                }
                else
                {
                    q.x=n;
                    q.y=p.x+p.y-n;
                }
            }
            else if(i==3 && p.y<m && p.x)///第二个杯子不满,第一个杯子不空,把第一个杯子的水倒入第二个杯子里
            {
                if(p.x+p.y<=m)
                {
                    q.x=0;
                    q.y=p.x+p.y;
                }
                else
                {
                    q.y=m;
                    q.x=p.x+p.y-m;

                }
            }
            else if(i==4 && p.x)///第一个杯子不空,倒完
            {
                q.x=0;
                q.y=p.y;
            }
            else if(i==5 && p.y)///第二个杯子不空,倒完
            {
                q.y=0;
                q.x=p.x;
            }
            if(!vis[q.x][q.y])///是否出现过,防止重复出现,死循环
            {
                vis[q.x][q.y]=1;
                Q.push(q);
            }
        }
    }
    return ;
}
int main()
{
    while(scanf("%d %d %d",&n,&m,&k)!=EOF)
    {
        met(vis,0);
        f=0;
        dfs();
        if(f==0)
            printf("impossible\n");
    }
    return 0;
}
时间: 2024-10-06 12:46:14

(poj)3414 Pots (输出路径的广搜)的相关文章

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

POJ 3414 Pot (输出路径)【BFS】

<题目链接> 题目大意: 有两个容量的空杯子,能够对这两个空杯子进行三种操作: 分别是fill(a),装满a杯子: drop(a),倒空a杯子: pour(a,b),将a杯子中的水倒入b杯子中: 现在问你,是否能够通过这三种操作,使得这两个杯子中至少有一个杯子中含有c体积的水,如果不行,输出"impossible",如果可以,输出操作的步数,以及每一步的具体操作. 解题分析: 此题与一道输出路径的很相像,只不过那道题是输出每一次操作对应的点的状态,而此题是要输出具体的操作.

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 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 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+回溯路径 正向输出】

题目地址: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 1166 The Clocks 记录路径的广搜

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