POJ - 3414 Pots(记录步数的广搜)

本文介绍了一种解决特定水量转移问题的方法,通过一系列操作(如填充、倾倒和转移水),使得其中一个容器恰好包含目标容量的水量。文章提供了一个算法实现示例,详细解释了如何寻找最短的操作路径。

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

题目链接:http://poj.org/problem?id=3414点击打开链接


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

Source

Northeastern Europe 2002, Western Subregion

跟hdu的非常可乐差不多 主要是记录比较麻烦

每一步都记录之前的步数 然后递推往后

也可以只记录每个步数的前驱 然后最后放到一个栈倒序输出

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
int n,m,k;
struct xjy
{
    int nv;
    int mv;
    int type;
    char num;
    int step;
    int pre;
};
struct book
{
    int x;
    int y;
};
pair <int ,int >check;
set<pair<int ,int > > s;
vector <xjy> q;
vector <xjy> ::iterator it;
int iit;
vector<xjy> aans;
xjy ans;
void bfs()
{
    xjy beg;
    beg.nv=0;
    beg.mv=0;
    beg.step=0;
    beg.num='k';
    beg.pre=-1;
    check.first=beg.nv;
    check.second=beg.mv;
    q.push_back(beg);
    s.insert(check);
    it=q.begin();
    while(iit!=q.size())
    {
        xjy mid;
        mid=q[iit];

        iit++;
        if(mid.mv==k||mid.nv==k)
            {
                ans=mid;
                break;
            }
        xjy midmid;
        midmid=mid;

        //1.n
        if(midmid.nv!=n)
        {

            midmid.nv=n;
            midmid.type=1;
            midmid.num='n';
            midmid.step++;
            midmid.pre=iit-1;
            check.first=midmid.nv;
            check.second=midmid.mv;
            if(!s.count(check))
            {
                q.push_back(midmid);
                s.insert(check);
            }
        }
        //1.m
        midmid=mid;
        if(midmid.mv!=m)
        {

            midmid.mv=m;
            midmid.type=1;
            midmid.num='m';
            midmid.step++;
            midmid.pre=iit-1;
            check.first=midmid.nv;
            check.second=midmid.mv;
            if(!s.count(check))
            {
                q.push_back(midmid);
                s.insert(check);
            }
        }
        //2.m
        midmid=mid;
        if(midmid.nv!=n&&midmid.mv!=0)
        {

            if(midmid.mv>(n-midmid.nv))
            {
                midmid.mv=midmid.mv-(n-midmid.nv);
                midmid.nv=n;
                midmid.type=2;
                midmid.num='m';
                midmid.step++;
                midmid.pre=iit-1;
                check.first=midmid.nv;
                check.second=midmid.mv;
                if(!s.count(check))
                {
                    q.push_back(midmid);
                    s.insert(check);
                }
            }
            else
            {

                midmid.nv+=midmid.mv;
                midmid.mv=0;
                midmid.type=2;
                midmid.num='m';
                midmid.step++;
                midmid.pre=iit-1;
                check.first=midmid.nv;
                check.second=midmid.mv;
                if(!s.count(check))
                {
                    q.push_back(midmid);
                    s.insert(check);
                }
            }
        }
        //2.n
        midmid=mid;
        if(midmid.mv!=m&&midmid.nv!=0)
        {

            if(midmid.nv>(m-midmid.mv))
            {

                midmid.nv-=(m-midmid.mv);
                midmid.mv=m;
                midmid.type=2;
                midmid.num='n';
                midmid.step++;
                midmid.pre=iit-1;
                check.first=midmid.nv;
                check.second=midmid.mv;
                if(!s.count(check))
                {
                    q.push_back(midmid);
                    s.insert(check);
                }
            }
            else
            {
                midmid.mv+=midmid.nv;
                midmid.nv=0;
                midmid.type=2;
                midmid.num='n';
                midmid.step++;
                midmid.pre=iit-1;
                check.first=midmid.nv;
                check.second=midmid.mv;
                if(!s.count(check))
                {
                    q.push_back(midmid);
                    s.insert(check);
                }
            }
        }
        //3.m
        midmid=mid;
        if(midmid.mv!=0)
        {

            midmid.mv=0;
            midmid.type=3;
            midmid.num='m';
            midmid.step++;
            midmid.pre=iit-1;
            check.first=midmid.nv;
            check.second=midmid.mv;
            if(!s.count(check))
            {
                q.push_back(midmid);
                s.insert(check);
            }
        }
        //3.n
        midmid=mid;
        if(midmid.nv!=0)
        {

            midmid.nv=0;
            midmid.type=3;
            midmid.num='n';
            midmid.step++;
            midmid.pre=iit-1;
            check.first=midmid.nv;
            check.second=midmid.mv;
            if(!s.count(check))
            {
                q.push_back(midmid);
                s.insert(check);
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    //if((k&1)&&!(n&1)&&!(m&1))
        //printf("impossible\n");
    //else
    {
        bfs();
        if(ans.step)
        {
            printf("%d\n",ans.step);
            int ppre=ans.pre;
            aans.push_back(ans);
            while(ppre!=0)
            {
                aans.push_back(q[ppre]);
                ppre=q[ppre].pre;
            }
            for(it=aans.end()-1;it>=aans.begin();it--)
            {
                if((*it).type==1)
                {
                    printf("FILL");
                    printf("(");
                    if((*it).num=='m')
                        printf("2");
                    else if((*it).num=='n')
                        printf("1");
                    printf(")\n");
                }
                else if((*it).type==2)
                {
                    printf("POUR");
                    printf("(");
                    if((*it).num=='m')
                        printf("2,1");
                    else if((*it).num=='n')
                        printf("1,2");
                    printf(")\n");
                }
                else if((*it).type==3)
                {
                    printf("DROP");
                    printf("(");
                    if((*it).num=='m')
                        printf("2");
                    else if((*it).num=='n')
                        printf("1");
                    printf(")\n");
                }
            }
        }
        else
        {
            printf("impossible\n");
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值