poj 3414 Pots(BSF + 记录路径)

本文介绍了一种利用广度优先搜索(BFS)算法解决水量调配问题的方法,具体包括两个容器间的倒水操作,旨在通过最少步骤获得目标水量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pots

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15972 Accepted: 6730 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 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)

题意:
有两个水杯,可以倒水,有三种操作
1. fill(i)加满i号杯子
2. drop(i)倒掉i号杯子里的水
3. pour(i,j)把i号杯子里的水倒向j号杯子
问最后能不能配出c体积的水,如果能配出,则配出c体积水需要最少多少步,并输入步骤
- 分析:
该题目是个BFS搜索题目,搜索过程不难,难在如何记录路径,刚刚学习了一下。用一个数组,记录整棵遍历的树,每个结点记录父结点,找到符合要求的点,则顺着树枝找到根节点,并把途中所有的结点放入到栈中,最后输出栈。其中用了一个type遍历,记录了该结点是由什么操作变过来的,这样便可以输出路径了。
- 代码如下:

#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
using namespace std;

typedef struct{
    int a,b;
    int i,pre;
    int type;
    int t;
}node;

int a,b,c;
int n;
int vis[105][105];
queue <node>q;
stack <node>s;
node ans[10005];
int num=0;

void init()
{
    while(!q.empty())
        q.pop();
}

node bfs()
{
    int i,j;
    vis[0][0]=1;
    node temp;
    temp.a = 0;
    temp.b = 0;
    temp.i = 0;
    temp.t = 0;
    temp.pre = -1;
    q.push(temp);

    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        //printf("<%d %d %d>\n",temp.a,temp.b,temp.t);
        if(temp.a == c || temp.b == c)
            return temp;
        node peace;
        peace.pre = temp.i;
        peace.t = temp.t+1;
        if(temp.a!=a)
        {
            //a加满 type = 1
            peace.a = a;
            peace.b = temp.b;
            if(!vis[peace.a][peace.b])
            {
                vis[peace.a][peace.b]=1;
                peace.type = 1;
                peace.i = ++num;
                ans[peace.i] = peace;
                q.push(peace);
            }

            //b倒向a type = 2
            peace.a = (temp.a + temp.b) > a ? a : (temp.a + temp.b);
            peace.b = temp.b - (peace.a - temp.a);
            if(!vis[peace.a][peace.b])
            {
                vis[peace.a][peace.b]=1;
                peace.type = 2;
                peace.i = ++num;
                ans[peace.i] = peace;
                q.push(peace);
            }
        }

        if(temp.b!=b)
        {
            //b加满 type = 3
            peace.a = temp.a;
            peace.b = b;
            if(!vis[peace.a][peace.b])
            {
                vis[peace.a][peace.b]=1;
                peace.type = 3;
                peace.i = ++num;
                ans[peace.i] = peace;
                q.push(peace);
            }
            //a倒向b type = 4
            peace.b = (temp.a + temp.b) > b ? b : (temp.a + temp.b);
            peace.a = temp.a - (peace.b - temp.b);
            if(!vis[peace.a][peace.b])
            {
                vis[peace.a][peace.b]=1;
                peace.type = 4;
                peace.i = ++num;
                ans[peace.i] = peace;
                q.push(peace);
            }
        }

        if(temp.a!=0)
        {
            //a清空 type = 5
            peace.a = 0;
            peace.b = temp.b;
            if(!vis[peace.a][peace.b])
            {
                vis[peace.a][peace.b]=1;
                peace.type = 5;
                peace.i = ++num;
                ans[peace.i] = peace;
                q.push(peace);
            }
        }

        if(temp.b!=0)
        {
            //清空b type = 6 
            peace.a = temp.a;
            peace.b = 0;
            if(!vis[peace.a][peace.b])
            {
                vis[peace.a][peace.b]=1;
                peace.type = 6;
                peace.i = ++num;
                ans[peace.i] = peace;
                q.push(peace);
            }
        }
    }
    temp.t = -1;
    return temp;
}

int main()
{
    while(scanf("%d %d %d",&a,&b,&c)!=EOF)
    {
        node temp;
        memset(vis,0,sizeof(vis));
        init();
        temp = bfs();

        if(temp.t==-1)
        {
            printf("impossible\n");
        }
        else
        {
            printf("%d\n",temp.t);
            while(temp.i!=0)
            {
                s.push(temp);
                temp = ans[temp.pre];
            }
            while(!s.empty())
            {
                temp = s.top();
                s.pop();
                if(temp.type == 1)
                    printf("FILL(1)\n");
                else if(temp.type == 2)
                    printf("POUR(2,1)\n");
                else if(temp.type == 3)
                    printf("FILL(2)\n");
                else if(temp.type == 4)
                    printf("POUR(1,2)\n");
                else if(temp.type == 5)
                    printf("DROP(1)\n");
                else
                    printf("DROP(2)\n");
            }
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值