PKU3414 Pots

题目链接:http://poj.org/problem?id=3414

这题以前碰到,没做,感觉好麻烦的样子。

今天突然想起这题,反应过来,这不就是一个BFS吗。

用一个二维数组vis[a][b]来记录a,b是否出现过,然后6个操作搜索一遍,能入队的入队。

我一开始没发现内存限制这么大,所以写的时候比较考虑内存。。

用了一个char数组来存6个操作的名字

然后在一个路径数组中存了所有操作,每一个节点都记录了这个操作在数组里的下标

然后再用一个结构体来读取路径,最后用了一个栈也是因为写起来比较方便。。。

a,b的范围都是<=100,所以vis数组要开101*101。

写的时候没有多想,一路想一路写下来,有些地方写得不太好,代码显得很长,4000多B,大概写了40分钟,还是需要进步啊!

不过能1次AC也是比较惊讶的,毕竟只测了样例就交了。。。

题目:Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11017 Accepted: 4674 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 ≤ 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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).

Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. These are all integers in the range from 1 to 100 andC≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operationsK. The followingK 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)
代码如下:

#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cctype>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<set>
#include<map>
using namespace std;
int sa,sb,target;
bool vis[101][101];
char output[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"};
//                     0         1         2         3         4           5
typedef struct node{
    int index;
    int a,b;
    int step;
};
typedef struct pp{
    int now,pre;
};
int px;
pp path[10001];
int bfs(node &sourse)
{
    queue<node> Q;
    node start,use;
    start.step=0;
    start.index=0;
    start.a=0;
    start.b=0;
    vis[0][0]=1;
    path[px].pre=-1;
    path[px++].now=-1;
    Q.push(start);
    while(!Q.empty())
    {
        start=Q.front();
        Q.pop();
        if(start.a==target||start.b==target)
        {
            sourse=start;
            return start.step;
        }
        if(vis[sa][start.b]==false)
        {//fill 1
            vis[sa][start.b]=true;
            use.a=sa;
            use.b=start.b;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=0;
            Q.push(use);
        }
        if(vis[start.a][sb]==false)
        {//fill 2
            vis[start.a][sb]=true;
            use.a=start.a;
            use.b=sb;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=1;
            Q.push(use);
        }
        if(vis[0][start.b]==false)
        {//drop 1
            vis[0][start.b]=true;
            use.a=0;
            use.b=start.b;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=2;
            Q.push(use);
        }
        if(vis[start.a][0]==false)
        {//drop 2
            vis[start.a][0]=true;
            use.a=start.a;
            use.b=0;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=3;
            Q.push(use);
        }
        if(start.a<sa&&start.b>0)
        {//pour 2 1
            int tempa,tempb;
            tempa=(start.a+start.b>=sa)?sa:start.a+start.b;
            tempb=(sa-start.a>=start.b)?0:start.b-sa+start.a;
            if(vis[tempa][tempb]==false)
            {
                vis[tempa][tempb]=true;
                use.a=tempa;
                use.b=tempb;
                use.step=start.step+1;
                path[px].pre=start.index;
                use.index=px;
                path[px++].now=4;
                Q.push(use);
            }
        }
        if(start.a>0&&start.b<sb)
        {
            int tempa,tempb;
            tempa=(sb-start.b>=start.a)?0:start.a-sb+start.b;
            tempb=(start.a+start.b>=sb)?sb:start.a+start.b;
            if(vis[tempa][tempb]==false)
            {
                vis[tempa][tempb]=true;
                use.a=tempa;
                use.b=tempb;
                use.step=start.step+1;
                path[px].pre=start.index;
                use.index=px;
                path[px++].now=5;
                Q.push(use);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d",&sa,&sb,&target)!=EOF)
    {
        getchar();
        memset(vis,false,sizeof(vis));
        memset(path,0,sizeof(path));
        px=0;
        node sourse;
        sourse.a=-1;
        sourse.b=-1;
        int ans=bfs(sourse);
        if(ans<0){
            printf("impossible\n");
            continue;
        }
        printf("%d",ans);
        stack<int> st;
        while(sourse.index>=0)
        {
            st.push(path[sourse.index].now);
            sourse.index=path[sourse.index].pre;
        }
        while(!st.empty())
        {
            printf("%s\n",output[st.top()]);
            st.pop();
        }
    }
    return 0;
}




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
学校大创竞赛管理系统,学生上报项目内容,学院、教务处、评审专家评审。SpringBoot、SpringCloud、SpringSecurity、redis、Mysql、swagger、fastdfs、maven、vue、webpack.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值