N - 非常可乐

================================================================================================================================
     不用想的太复杂,其实就是一道广搜题,
     三个瓶子有六种操作
     s瓶→n瓶  s瓶→m瓶    
     n瓶→m瓶  n瓶→s瓶  
     m瓶→n瓶  m瓶→s瓶
     只是比迷宫题的 上下左右 4个操作多了2种操作
     然后判定条件变成   两个瓶子里的液体相同&&另一个瓶子里为空 
     再按照一般广搜的套路来,定义好列表,head ,tail 开始进行广搜
     tail[head] 的初始状态 即三个瓶子的初始状态
     s瓶 满的 n瓶 空的 m瓶 空的  step = 0
================================================================================================================================
     代码:
 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int s,n,m;
struct Node{
int s,n,m;
int step;
void in(int a,int b,int c,int d)
{
s=a,n=b,m=c;step=d;
}
}tail[];
bool book[][];
bool flag;
int head,top;
void caozuo(int i,int& ts,int& tn,int& tm)
{
if(i==) // s瓶→n瓶
{
tn+=ts;ts=; //s瓶全部倒入 n瓶
//tn表示当前的液体 , n表示最多可容纳多少
if(tn>n) ts = tn-n,tn=n; //如果n瓶溢出了,则溢出的返回给s瓶
}
//下面的操作原理跟上面一样
if(i==) // s瓶→m瓶
{
tm+=ts;ts=;
if(tm>m) ts = tm-m,tm=m;
}
if(i==) // n瓶→s瓶
{
ts+=tn;tn=;
if(ts>s) tn = ts-s,ts=s;
}
if(i==) // n瓶→m瓶
{
tm+=tn;tn=;
if(tm>m) tn = tm-m,tm=m;
}
if(i==) // m瓶→s瓶
{
ts+=tm;tm=;
if(ts>s) tm = ts-s,ts=s;
}
if(i==) // m瓶→n瓶
{
tn+=tm;tm=;
if(tn>n) tm = tn-n,tn=n;
}
}
int main()
{
while(~scanf("%d %d %d",&s,&n,&m)&&s+n+m)
{
flag=;memset(book,,sizeof(book)); //初始化
if(n == m) printf("1\n"); //如果n==m,只需一步就可以达到目的
else
{
head = ;top = ;
tail[].in(s,,,); //队列【1】初始化
book[][]=;
while(head<top)
{
for(int i = ;i<=;++i)
{
int ts = tail[head].s;
int tn = tail[head].n;
int tm = tail[head].m;
caozuo(i,ts,tn,tm); //执行六个步骤
if(book[tn][tm]==) continue;
if(book[tn][tm]==)
{
book[tn][tm]=;
tail[top].in(ts,tn,tm,tail[head].step+);
++top;
}
//printf("ts : %d tn: %d tm: %d %d\n",ts,tn,tm,tail[top-1].step);
//↑可以直观的看到每一步三个瓶子的状况
if(tn==tm&&ts==||tn==ts&&tm==||ts==tm&&tn==) {flag=;break;}
}
head++;
if((flag==)) break;
}
if(flag) printf("%d\n",tail[top-].step);
else printf("NO\n"); }
}
}

HDU 1495 非常可乐 (只是转了个弯的广搜题)的更多相关文章

  1. BFS(倒水问题) HDU 1495 非常可乐

    题目传送门 /* BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都 ...

  2. HDU 1495 非常可乐(数论,BFS)

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. HDU 1495 非常可乐

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103711#problem/M /*BFS简单题 链接地址: http://acm.hdu ...

  4. HDU 1495 非常可乐 BFS 搜索

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...

  5. HDU 1495 非常可乐 bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 第三个杯子的盛水量可由前两个杯子得到,而前两个杯子状态总数在100*100以内,穷举可实现 #includ ...

  6. HDU 1495 非常可乐(BFS倒水问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101) ...

  7. HDU - 1495 非常可乐 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1495 思路 首先 如果可乐的体积 是奇数 那么是无解的 然后 如果能够得到两杯 都是一般容量的可乐 那 ...

  8. HDU 1495 非常可乐【BFS/倒水问题】

    非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  9. HDU - 1495 非常可乐 bfs互倒三杯水

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Windows快捷操作技巧

    隐藏技能 在当前路径打开命令行 shift + 右键点击文件夹内的空白处,你会看到右键弹出菜单多了个选项 "在此处打开命令窗口",省去了打开命令行再cd到当前路径的麻烦. 快捷键 ...

  2. 深入Python(1): 字典排序 关于sort()、reversed()、sorted()

    http://www.cnblogs.com/BeginMan/p/3193081.html 一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠 ...

  3. OFFICE_EXCEL_Combine text from two or more cells into one cell.

    Excel   Enter and format data   Layout   Combine text from two or more cells into one cell Combine t ...

  4. mysql便于管理的几个shell脚本

    [mysql@clark scripts]$ lsmysql_db_shutdown.sh  mysql_db_startup.sh  mysql_env.ini  mysqlplus.sh[mysq ...

  5. vue+node+mongoose踩过的坑

    1.当你在cmd中输入npm run dev的时候,出现这种错误 很有可能是目前的端口被占用了,可以把所有可能用到这个端口号的应用关闭或者你直接改一个新的端口号 修改端口的方法:新打开一个cmd,然后 ...

  6. RF脚本中的坑2: pip下载python库时报certificate verify failed

    用pip命令下载第三方library时,报错certificate verify failed,截图如下: 在网上找了各种教程,包括修改了pip下载源地址也无效果,最后祭出了杀手锏——FQ,FQ成功后 ...

  7. pymongo 常用操作函数

    pymongo 是 mongodb 的 python Driver Editor. 记录下学习过程中感觉以后会常用多一些部分,以做参考. 1. 连接数据库 要使用pymongo最先应该做的事就是先连上 ...

  8. BZOJ1718:[USACO]Redundant Paths 分离的路径(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  9. 过拟合是什么?如何解决过拟合?l1、l2怎么解决过拟合

    1. 过拟合是什么? https://www.zhihu.com/question/264909622    那个英文回答就是说h1.h2属于同一个集合,实际情况是h2比h1错误率低,你用h1来训练, ...

  10. 学习换脸:Switching Eds: Face swapping with Python, dlib, and OpenCV

    学习GitHub上比较火换脸博客,原英文版:https://matthewearl.github.io/2015/07/28/switching-eds-with-python/ 系统win10,x6 ...