Pots(POJ 3414)解题报告(虫前,有两个痛)

本文介绍了一道关于两个容器操作的题目,通过BFS算法寻找最短步骤来达到指定水量的目标,并提供了完整的代码实现。

Pots(POJ 3414)解题报告(虫前,有两个痛)

//代码参考自http://blog.youkuaiyun.com/sunacmer/article/details/3945312

一、原题

E - Pots

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)

 

二、题目大意:

虫前,有两个痛,A痛和B痛,还有一痛水,容量我们叫它C。。。

你要想办法在两个痛之间倒腾来倒腾去最后整出来C。。。

规则如下:

1、你可以把任何一个痛装满水。

2、你可以把任何一个痛里的水倒干净。

3、你可以把一个痛里的水倒进另一个痛里。如果有剩余,留在那个痛里。。。

事实就是,一共有六种情况。

1:FILL(1)

2:FILL(2)

3:DROP(1)

4:DROP(2)

5:POUR(1,2)

6:POUR(2,1)

然后现在依次输入ABC,要求计算出最少步数以及打印出每一步。

 

三、基本思路:BFS

1、用一个结构体存相关数据(当前状态)

typedef struct

{

    int a;

    int b;

    int step;

    int pre;

    int flag;//标记当前步数step,之前的一步preflag表示六种情况中的一种

}Pot;

这里为了可以实现打印数据,必须在结构里面加上preflag

Queue在经历BFS后结束在front+1处,故而为了得到之前被遍历的结果,必须加一个pre用于最后的查找。

 

2、主要用到的几个关键变量

Pot que[N];//模拟队列

bool visit[101][101];//记录当前的A状态和B状态的过程是否被访问过

int path[N],index;//记录全部路径在que中的下标

这里第一个使用了数组来模拟队列而不是直接用queue<int>que,主要在于无法打印路径,由于que并不记录前一个节点位置,所以只好用数组进行模拟,便于通过下标进行回查。

BFS结束后,用path数组记录que下标。

 

3BFS关键代码部分:

这部分的嵌套循环实质是对当前的一个状态(存储在temp里)向下查找下一个状态。

过程依旧是:

头元素入队列,标记头元素已被查找;

While循环判断队列非空,取头元素(标记),弹出头元素,判断结束条件(找到C),遍历查找下一状态。

    while(front!=rear)

    {

        temp=que[front];//取头元素

        front++;//弹出头元素

        if(temp.a==C||temp.b==C)

        {

            flag_finish=true;

            break;

        }

        for(int i=1;i<=6;i++)//6种情况

        {

            switch(i)

            {

                case 1:next.a=A;next.b=temp.b;next.flag=1;break;//fill(1)

……

                case 5://drop(1,2)

                {

                    if(B-temp.b<temp.a)//如果A倒完后有剩余

                    {

                        next.a=temp.a-(B-temp.b);

                        next.b=B;

                        next.flag=5;

                    }

                    else//如果A能全倒进B

                    {

                        next.a=0;

                        next.b=temp.b+temp.a;

                        next.flag=5;

                    }

                }break;

……

            }

            if(!visit[next.a][next.b])

            {

               next.step=temp.step+1;

               next.pre=front-1;//front++

               que[rear++]=next;//next压入队列

               visit[next.a][next.b]=true;//该状态被访问过

//             cout<<front<<":"<<next.flag<<endl;

            }

        }

    }

4、记录被遍历过的元素下标并打印结果    

index=que[front-1].step-1;//que[front-1].step步,但后面到0,所以-1

    for(int i=front-1;i>=0;)//将之前已经弹出队列的元素从最后一位到第一位的下标存进path

    {

        path[index--]=i;

        i=que[i].pre;

    }

    for(int i=0;i<que[front-1].step;i++)

    {

        switch(que[path[i]].flag)

        {

            case 1:cout<<"FILL(1)"<<endl;break;

    ……

        }

}

 

四、源码完整版

#include <iostream>

#include <cstring>

#define N 10010

using namespace std;

typedef struct

{

    int a;

    int b;

    int step;

    int pre;

    int flag;//标记当前步数step,之前的一步preflag表示六种情况中的一种

}Pot;

Pot que[N];//模拟队列

bool visit[101][101];//记录当前的A状态和B状态的过程是否被访问过

int path[N],index;//记录全部路径在que中的下标

int main()

{

    int A,B,C;

    bool flag_finish=false;//初始化记录是否找到最终的结果

    cin>>A>>B>>C;

    memset(visit,0,sizeof(visit));

    memset(que,0,sizeof(que));

    int front=0,rear=0;//队列顶,尾

    Pot temp={0,0,0,-1,0},next;//初始化队列头

    que[rear++]=temp;//首元素压入队列

    visit[0][0]=true;

    while(front!=rear)

    {

        temp=que[front];//取头元素

        front++;//弹出头元素

        if(temp.a==C||temp.b==C)

        {

            flag_finish=true;

            break;

        }

        for(int i=1;i<=6;i++)//6种情况

        {

            switch(i)

            {

                case 1:next.a=A;next.b=temp.b;next.flag=1;break;//fill(1)

                case 2:next.a=temp.a;next.b=B;next.flag=2;break;//fill(2)

                case 3:next.a=0;next.b=temp.b;next.flag=3;break;//drop(1)

                case 4:next.a=temp.a;next.b=0;next.flag=4;break;//drop(2)

                case 5://drop(1,2)

                {

                    if(B-temp.b<temp.a)//如果A倒完后有剩余

                    {

                        next.a=temp.a-(B-temp.b);

                        next.b=B;

                        next.flag=5;

                    }

                    else//如果A能全倒进B

                    {

                        next.a=0;

                        next.b=temp.b+temp.a;

                        next.flag=5;

                    }

                }break;

                case 6://drop(2,1)

                {

                    if(A-temp.a<temp.b)//如果B倒完后有剩余

                    {

                        next.b=temp.b-(A-temp.a);

                        next.a=A;

                        next.flag=6;

                    }

                    else//如果B能全倒进A

                    {

                        next.b=0;

                        next.a=temp.b+temp.a;

                        next.flag=6;

                    }

                }break;

            }

            if(!visit[next.a][next.b])

            {

               next.step=temp.step+1;

               next.pre=front-1;//front++

               que[rear++]=next;//next压入队列

               visit[next.a][next.b]=true;//该状态被访问过

//             cout<<front<<":"<<next.flag<<endl;

            }

        }

    }

    if(!flag_finish)   {cout<<"impossible"<<endl;return 0;}//必须加上return否则继续执行后续部分

    else cout<<que[front-1].step<<endl;

    index=que[front-1].step-1;//que[front-1].step步,但后面到0,所以-1

    for(int i=front-1;i>=0;)//将之前已经弹出队列的元素从最后一位到第一位的下标存进path

    {

        path[index--]=i;

        i=que[i].pre;

    }

    for(int i=0;i<que[front-1].step;i++)

    {

        switch(que[path[i]].flag)

        {

            case 1:cout<<"FILL(1)"<<endl;break;

            case 2:cout<<"FILL(2)"<<endl;break;

            case 3:cout<<"DROP(1)"<<endl;break;

            case 4:cout<<"DROP(2)"<<endl;break;

            case 5:cout<<"POUR(1,2)"<<endl;break;

            case 6:cout<<"POUR(2,1)"<<endl;break;

        }

    }

    return 0;

}

 

 

PS:最后发现那个flag还是可以不用的。。。。囧,结束条件也可以是iffront==rearcout<<"impossible"<<endl;

另,附上链接:

http://poj.org/problem?id=3414

【最优潮流】直流最优潮流(OPF)课设(Matlab代码实现)内容概要:本文档主要围绕“直流最优潮流(OPF)课设”的Matlab代码实现展开,属于电力系统优化领域的教学与科研实践内容。文档介绍了通过Matlab进行电力系统最优潮流计算的基本原理与编程实现方法,重点聚焦于直流最优潮流模型的构建与求解过程,适用于课程设计或科研入门实践。文中提及使用YALMIP等优化工具包进行建模,并提供了相关资源下载链接,便于读者复现与学习。此外,文档还列举了大量与电力系统、智能优化算法、机器学习、路径规划等相关的Matlab仿真案例,体现出其服务于科研仿真辅导的综合性平台性质。; 适合人群:电气工程、自动化、电力系统及相关专业的本科生、研究生,以及从事电力系统优化、智能算法应用研究的科研人员。; 使用场景及目标:①掌握直流最优潮流的基本原理与Matlab实现方法;②完成课程设计或科研项目中的电力系统优化任务;③借助提供的丰富案例资源,拓展在智能优化、状态估计、微电网调度等方向的研究思路与技术手段。; 阅读建议:建议读者结合文档中提供的网盘资源,下载完整代码与工具包,边学习理论边动手实践。重点关注YALMIP工具的使用方法,并通过复现文中提到的多个案例,加深对电力系统优化问题建模与求解的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值