题目大意:输入三个整数 a,b,c.   a : 可乐瓶的容量,b: 甲杯的容量 ,c: 乙杯的容量。问能否用这三个被来实现饮料的平分???如果可以输出倒饮料的次数,

否则输出NO

解题思路:BFS

1)本题的考点其实在于将标记数组由二维数组变为三维数组。遍历状态由使用for()循环变为手动枚举,一个一个的if()

代码如下:

/*
* 1495_2.cpp
*
* Created on: 2013年8月16日
* Author: Administrator
*/ #include <iostream>
#include <queue> using namespace std;
const int maxn = 102;
bool visited[maxn][maxn][maxn]; int a, b, c;
struct State {
int a;
int b;
int c;
int v;
}; bool checkState(State st) {
if (!visited[st.a][st.b][st.c]) {
return true;
} return false;
} void bfs() {
queue<State> q;
State st, now, next; st.a = a;
st.b = 0;
st.c = 0;
st.v = 0;
q.push(st);
memset(visited,0,sizeof(visited) );
visited[st.a][st.b][st.c] = 1;
while (!q.empty()) {
now = q.front(); //有2个等于a/2就结束
if ((now.a == a / 2 && now.b == a / 2)
|| (now.a == a / 2 && now.c == a / 2)
|| (now.c == a / 2 && now.b == a / 2)) {
printf("%d\n", now.v);
return ;
} /**
* 若a杯中的饮料的体积不为0,
* 则枚举出将a杯中的饮料倒到其他杯中....
*/
if (now.a != 0) {
/**
* 关键举得理解:now.a > b - now.b
* now.a : now状态下a杯中的饮料的体积
* b : b杯的体积
* now.b :now状态下b杯中的饮料的体积
*
*/
if (now.a > b - now.b) {//now.a > b - now.b。且倒不完
next.a = now.a - (b - now.b);
next.b = b;
next.c = now.c;
next.v = now.v + 1;
} else {//倒完了
next.a = 0;
next.b = now.b + now.a;
next.c = now.c;
next.v = now.v + 1;
} if (checkState(next)) {
q.push(next);
visited[next.a][next.b][next.c] = 1;
} if (now.a > c - now.c) {
next.a = now.a - (c - now.c);
next.b = now.b;
next.c = c;
next.v = now.v + 1;
} else {
next.a = 0;
next.b = now.b;
next.c = now.c + now.a;
next.v = now.v + 1;
} if (checkState(next)) {
q.push(next);
visited[next.a][next.b][next.c] = 1;
}
} if (now.b != 0) {
if (now.b > a - now.a) {
next.a = a;
next.b = now.b - (a - now.a);
next.c = now.c;
next.v = now.v + 1;
} else {
next.a = now.a + now.b;
next.b = 0;
next.c = now.c;
next.v = now.v + 1;
} if (checkState(next)) {
q.push(next);
visited[next.a][next.b][next.c] = 1;
} if (now.b > c - now.c) {
next.a = now.a ;
next.b = now.b - (c - now.c);
next.c = c;
next.v = now.v + 1;
} else {
next.a = now.a;
next.b = 0;
next.c = now.c + now.b;
next.v = now.v + 1;
} if (checkState(next)) {
q.push(next);
visited[next.a][next.b][next.c] = 1;
}
} if (now.c != 0) {
if (now.c > b - now.b) {
next.a = now.a ;
next.b = b;
next.c = now.c - (b - now.b);
next.v = now.v + 1;
} else {
next.a = now.a;
next.b = now.b + now.c;
next.c = 0;
next.v = now.v + 1;
} if (checkState(next)) {
q.push(next);
visited[next.a][next.b][next.c] = 1;
} if (now.c > a - now.a) {
next.a = a;
next.b = now.b;
next.c = now.c - (a - now.a);
next.v = now.v + 1;
} else {
next.a = now.a + now.c;
next.b = now.b;
next.c = 0;
next.v = now.v + 1;
} if (checkState(next)) {
q.push(next);
visited[next.a][next.b][next.c] = 1;
}
}
q.pop();
} printf("NO\n");
}
int main() { while(scanf("%d%d%d",&a,&b,&c)!=EOF,a+b+c){
if(a%2 == 1){
printf("NO\n");
}else{
bfs();
}
}
}

(step4.2.5)hdu 1495(非常可乐——BFS)的更多相关文章

  1. HDU 1495 非常可乐 BFS 搜索

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

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

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

  3. HDU 1495 非常可乐 BFS搜索

    题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n, 刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1; 分析:直接互相倒就完了,BFS模 ...

  4. HDU 1495 非常可乐 BFS

    题目大意:中文题不说了. 题目思路:我有同学用GCD数论写出来的代码很简洁,但是很抱歉,数论蒟蒻,我觉得比赛的时候我没办法推出.如果用BFS的话思路很简单的,就是6方向广搜,只不过稍微麻烦点.具体看代 ...

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

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

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

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

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

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

  8. 非常可乐---hdu 1495(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...

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

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

随机推荐

  1. 【Binary Tree Inorder Traversal】cpp

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  2. 解决jquery-easyui1.3.3 combobox 多选模式不兼容IE8问题

    扩展Array的原型对象,加入indexOf方法 if(!Array.prototype.indexOf){    Array.prototype.indexOf = function(target) ...

  3. Multi-catch

    It’s relatively common for a try block to be followed by several catch blocks to handle various type ...

  4. block extends include三者的差别跟用法

    block extends include三者的差别跟用法 一.定义基础模板,在html内容中定义多个block块,block由子模板引用同名block块,来决定是否替换这些部分{% block ti ...

  5. Posix线程编程指南(2) 线程私有数据

    概念及作用 在单线程程序中,我们经常要用到"全局变量"以实现多个函数间共享数据.在多线程环境下,由于数据空间是共享的,因此全局变量也为所有线程所共有.但有时应用程序设计中有必要提供 ...

  6. CkEditor 插件开发

    CKEditor的插件开发其实很简单只需要两步.1.通过CKEditor.plugins.add()方法编写插件的逻辑主体, 2.告诉CKEditor我们有一个自定义插件需要添加进来. //创建插件逻 ...

  7. hadoop开发环境-----eclipse

    1.eclipse下载 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/lunasr1 2.hadoo ...

  8. hdu 1800 Flying to the Mars(简单模拟,string,字符串)

    题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...

  9. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  10. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...