HDU1495 非常可乐(BFS/数论)
Input三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。Output如果能平分的话请输出最少要倒的次数,否则输出"NO"。Sample Input
- 7 4 3
- 4 1 3
- 0 0 0
Sample Output
- NO
- 3
BFS暴力搜索可解。倒水只可能有六种情况,s->m s->n m->s m-->n n->s n->m 每种情况里分为能把待倒入的瓶子倒满和不能倒满。这里可以用一个结构体记录信息:s代表原瓶里的可乐容量,m代表m瓶里的,n代表n瓶里的,nxt代表下一次要往外倒可乐的瓶号(0代表s瓶,1代表m瓶,2代表n瓶),cnt代表倒可乐累积的次数。再建立一个vis数组判断是否访问过。要注意的是这里最好建四维数组,三维存s,m,n,第四维存nxt。BFS即可,注释写得很详细。
最后附上大佬的数论解法https://www.cnblogs.com/ECJTUACM-873284962/p/6750320.html
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <queue>
- using namespace std;
- int s,n,m;
- struct node
- {
- int s;
- int m;
- int n;
- int cnt;
- int nxt;//下一次该哪个杯子往外倒水 0 s 1 m 2 n
- };
- bool vis[][][][]={};//四维vis数组存是否访问过
- int bfs()
- {
- node start;
- start.s=s;
- start.n=;//初始化第一层信息
- start.m=;
- start.cnt=;
- start.nxt=;
- queue<node>q;
- q.push(start);
- memset(vis,,sizeof(vis));
- while(q.size())
- {
- node pre=q.front();
- q.pop();
- if((pre.s==pre.n&&pre.s==s/)||(pre.s==pre.m&&pre.s==s/)||(pre.n==pre.m&&pre.n==s/))//是否搜到
- {
- return pre.cnt;
- }
- if(vis[pre.s][pre.m][pre.n][pre.nxt])//访问过的话直接continue
- {
- continue;
- }
- vis[pre.s][pre.m][pre.n][pre.nxt]=;
- int pour=pre.nxt;
- node next;
- if(pour==)//s往n m倒水
- {
- //s往n
- if(n-pre.n<pre.s) //能倒满
- {
- next.s=pre.s-(n-pre.n);//更新下一层的信息
- next.m=pre.m;
- next.n=n;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);//不为0的话再添加 要不然瓶子里都没可乐了也就倒不出来了
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);//如果只是三维vis数组的话 加进去三个s m n一样但nxt不一样的 后两个都被vis判定卡掉了 所以要换成四维
- }
- else if(n-pre.n>=s)//不能倒满
- {
- next.s=;
- next.m=pre.m;
- next.n=pre.n+pre.s;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- //s往m
- if(m-pre.m<pre.s)
- {
- next.s=pre.s-(m-pre.m);
- next.n=pre.n;
- next.m=m;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- else if(m-pre.m>=pre.s)
- {
- next.s=;
- next.n=pre.n;
- next.m=pre.m+pre.s;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- }
- else if(pour==)//m往 s n
- {
- //m往s
- if(s-pre.s<pre.m)
- {
- next.m=pre.m-(s-pre.s);
- next.n=pre.n;
- next.s=s;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- else if(s-pre.s>=pre.m)
- {
- next.m=;
- next.n=pre.n;
- next.s=pre.s+pre.m;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- //m往n
- if(n-pre.n<pre.m)
- {
- next.m=pre.m-(n-pre.n);
- next.s=pre.s;
- next.n=n;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- else if(n-pre.n>=pre.m)
- {
- next.m=;
- next.s=pre.s;
- next.n=pre.n+pre.m;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- }
- else if(pour==)//n往s m
- {
- //n往s
- if(s-pre.s<pre.n)
- {
- next.n=pre.n-(s-pre.s);
- next.m=pre.m;
- next.s=s;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- else if(s-pre.s>=pre.n)
- {
- next.n=;
- next.m=pre.m;
- next.s=pre.s+pre.n;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- //n往m
- if(m-pre.m<pre.n)
- {
- next.n=pre.n-(m-pre.m);
- next.s=pre.s;
- next.m=m;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- else if(m-pre.m>=pre.n)
- {
- next.n=;
- next.s=pre.s;
- next.m=pre.m+pre.n;
- next.cnt=pre.cnt+;
- next.nxt=;
- if(next.s!=)q.push(next);
- next.nxt=;
- if(next.m!=)q.push(next);
- next.nxt=;
- if(next.n!=)q.push(next);
- }
- }
- }
- return ;
- }
- int main()
- {
- while(scanf("%d%d%d",&s,&m,&n)&&s&&n&&m)
- {
- if(s%!=)
- {
- cout<<"NO"<<endl;
- continue;
- }
- int ans=bfs();
- if(ans)
- {
- cout<<ans<<endl;
- continue;
- }
- else
- {
- cout<<"NO"<<endl;
- continue;
- }
- }
- return ;
- }
HDU1495 非常可乐(BFS/数论)的更多相关文章
- HDU1495 非常可乐 —— BFS + 模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 非常可乐 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU-1495 非常可乐(BFS)
广搜的灵活应用题: 非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 非常可乐(杭电hdu1495)bfs
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1495 非常可乐 BFS
题目大意:中文题不说了. 题目思路:我有同学用GCD数论写出来的代码很简洁,但是很抱歉,数论蒟蒻,我觉得比赛的时候我没办法推出.如果用BFS的话思路很简单的,就是6方向广搜,只不过稍微麻烦点.具体看代 ...
- HDU-1495 非常可乐 (嵌套结构体-广搜 对比 一般广搜)
题意 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但 ...
- HDU1459 非常可乐(BFS) 2016-07-24 15:00 165人阅读 评论(0) 收藏
非常可乐 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶 ...
- HDU1664 BFS + 数论 + 剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1664 , 一道比较蛋疼的搜索题. 这道题有很多坑点,一点处理不好就要TLE. 题意很简单,就是找到一个 ...
- (step4.2.5)hdu 1495(非常可乐——BFS)
题目大意:输入三个整数 a,b,c. a : 可乐瓶的容量,b: 甲杯的容量 ,c: 乙杯的容量.问能否用这三个被来实现饮料的平分???如果可以输出倒饮料的次数, 否则输出NO 解题思路:BFS ...
- HDU1495 非常可乐
解题思路:简单的宽搜,见代码: #include<cstdio> #include<cstring> #include<algorithm> #include< ...
随机推荐
- selenium的元素定位方法-By
如果在定位元素属性中包含了如ID等元素属性,那么在一个测试中,定位方法具体有哪几种,可以参考by模块中的By类,By的代码如下: class By(object): """ ...
- 每天进步一点点------Allegro PCB命名规则
PCB命名规则-allegro 一.焊盘命名规则 1. 贴片矩形焊盘 命名规则:SMD+长(L)+宽(W)(mil) 举例:SMD90X60 2. 贴片圆焊盘 命名规则:SMDC+焊盘直径(D) ...
- 大白话Web三大组件之一Servlet
很多学习到Servlet这里的童鞋,听到那么多专业名词解释这个Servlet,相信都是很蒙圈的,在这里我先不跟大家扯Servlet的大概念,先跟大家探讨一下关于Servlet的作用 相信MVC这个概念 ...
- Codeforces A. Serval and Bus
inputstandard inputoutputstandard outputIt is raining heavily. But this is the first day for Serval, ...
- CSS中的z-index属性如何使用
z-index属性介绍 只有设置了定位我们才会使用到该z-index属性,如:固定定位.相对定位.绝对定位. 定位元素默认的z-index属性值是0. 如果2个定位的元素都没有设置z-index属性, ...
- IntelliJ IDEA 2017.3尚硅谷-----安装
选择路径 安装目录 bin目录下的文件 启动文件 虚拟机的配置信息 -Xms128m 初始内存 -Xmx750m 最大内存-XX:ReservedCodeCacheSize=240m 可保留代码缓存的 ...
- JSON--WEB SERVICE
Query ajax webservice:get 和 post 一.GET 方式 客户端 复制代码代码如下: var data = { classCode: "0001"}; / ...
- SQL Server 函数大全
本文链接:https://blog.csdn.net/qq_15028299/article/details/81330854SQL2008 表达式:是常量.变量.列或函数等与运算符的任意组合.htt ...
- 【想见你】剧情解析byZlc
花两天时间刷完了想见你,精神有点恍惚. 要是刷题也能有这个尽头就好了... 下面给大家带来个人的剧(hu)情(bian)解(luan)析(zao) 穿越条件:一台老式随身听,一首last dance, ...
- Flask的基本使用
最基本的一个Flask模板,实现本地9898端口访问,页面返回hello world from flask import Flask app = Flask(__name__) @app.route( ...