题目描述 Description

有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水。设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒。已知 x 升壶为空 壶, y 升壶为空壶。问如何通过倒水或灌水操作, 用最少步数能在x或y升的壶中量出 z ( z ≤ 100 )升的水 来。

输入描述 Input Description

一行,三个数据,分别表示 x,y 和 z;

输出描述 Output Description

一行,输出最小步数 ,如果无法达到目标,则输出"impossible"

样例输入 Sample Input

3 22 1

样例输出 Sample Output

14

思路:

普通广搜

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define maxn 100000
using namespace std;
struct sta{
int x;
int y;
int step;
int frm;
};
int mx,my,z,j[][],solved = ;
sta temp;
void input(){
cin>>mx>>my>>z;
temp.x = temp.y = temp.step = ;
temp.frm = -;
}
sta expand(sta a,int sign){
if(sign == ) a.x = ;
if(sign == ) a.y = ;
if(sign == ) a.x = mx;
if(sign == ) a.y = my;
if(sign == ){
int d;
if(mx - a.x < a.y) d = mx - a.x;
else d = a.y;
a.y -= d;
a.x += d;
}
if(sign == ){
int d;
if(my - a.y < a.x) d = my - a.y;
else d = a.x;
a.y += d;
a.x -= d;
}
a.frm = sign;
a.step++;
if(j[a.x][a.y]) a.step = -;
j[a.x][a.y] = ;
return a;
}
void bfs(){
sta q[maxn];
int h = ,t = ;
q[h] = temp;
while(h != t){
if(q[h%maxn].x == z || q[h%maxn].y == z) {
cout<<q[h%maxn].step<<endl;
solved = ;
break;
}
for(int i = ;i <= ;i++){
if(i == && (q[h%maxn].x == ||q[h%maxn].frm == )) continue;
if(i == && (q[h%maxn].y == ||q[h%maxn].frm == )) continue;
if(i == && (q[h%maxn].x == mx ||q[h%maxn].frm == )) continue;
if(i == && (q[h%maxn].y == my ||q[h%maxn].frm == )) continue;
if(i == && (q[h%maxn].y <= || q[h%maxn].x >= mx || q[h%maxn].frm == )) continue;
if(i == && (q[h%maxn].x <= || q[h%maxn].y >= my || q[h%maxn].frm == )) continue;
temp = expand(q[h%maxn],i); if(temp.step != -) {
t++;
q[t%maxn] = temp; }
}
h++;
}
}
int main(){
input();
if(z > mx || z > my || !mx || !my || (mx == my && mx != z)){
cout<<"impossible"<<endl;
return ;
}
bfs();
if(!solved) cout<<"impossible"<<endl;
return ;
}

codevs1226 倒水问题的更多相关文章

  1. codevs1226倒水问题(Bfs)

    /* 首先建立模型 可以看成是三个水杯 第三个无穷大 (这里看成是201足够了) 最少步数 想到Bfs 维护队列里的状态:要有个步数 还要有v :此时刻三个杯子有多少水 然后倒水:因为没有刻度 所以有 ...

  2. 广度优先搜索 cdoevs 1226 倒水问题

    cdoevs 1226 倒水问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升 ...

  3. 倒水问题 (codevs 1226) 题解

    [问题描述] 有两个无刻度标志的水壶,分别可装x升和y升 ( x,y 为整数且均不大于100)的水.设另有一水缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知x升壶为空壶, ...

  4. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  5. yzoi1777倒水问题的详细解法

    Description - 问题描述 x.y.z三个容器,其最大容量分别是xMAX升.yMAX升.zMAX升,这里规定100>xMAX>yMAX>zMAX.一开始x是装满了水的,现在 ...

  6. 倒水问题-->经典面试题目

    题目详细: 有两个容器,容积分别为A升和B升,有无限多的水,现在需要C升水.我们还有一个足够大的水缸,足够容纳C升水.起初它是空的,我们只能往水缸里倒入水,而不能倒出.可以进行的操作是:把一个容器灌满 ...

  7. 倒水问题 (FillUVa 10603) 隐式图

    题意:本题的题意是给你三个杯子,第一二个杯子是空的,第三个杯子装满水,要求是量出一定容量d升的水.若是得不到d升的水,那就让某一个杯子里面的水达到d',使得d'尽量接近d升. 解题思路:本题是给出初始 ...

  8. uva10603 倒水问题

    状态搜索.类似八数码问题 AC代码 #include<cstdio> #include<queue> #include<cstring> #include<a ...

  9. 美团codeM预赛A轮 倒水

    [编程题] 倒水 时间限制:1秒 空间限制:32768K 有一个大水缸,里面水的温度为T单位,体积为C升.另有n杯水(假设每个杯子的容量是无限的),每杯水的温度为t[i]单位,体积为c[i]升. 现在 ...

随机推荐

  1. 数据据操作 tp5

    数据库操作-DB类 学习手册 数据库配置 注意1:在TP里面,可以在模块下面单独的建立一个database.php配置文件,代表这个模块就使用配置的这个数据库 注意2:我们可以在config.php里 ...

  2. spring 简单实现BeanFactory(转)

    原文地址: http://blog.csdn.net/mlc1218559742/article/details/52776160 有没有发现上面的代码与利用反射实现工厂模式的代码很相似.对,你没有看 ...

  3. [Usaco2005 oct]Flying Right 飞行航班

    Description 为了表示不能输给人类,农场的奶牛们决定成立一家航空公司.她们计划每天早晨,从密歇根湖湖岸的最北端飞向最南端,晚上从最南端飞往最北端.在旅途中,航空公司可以安排飞机停在某些机场. ...

  4. hdu2031

    http://acm.hdu.edu.cn/showproblem.php?pid=2031 #include<stdio.h> #include<math.h> #inclu ...

  5. E - Easy Dijkstra Problem(求最短路)

    Description Determine the shortest path between the specified vertices in the graph given in the inp ...

  6. Canvas入门笔记-实现极简画笔

    今天学习了Html5 Canvas入门,已经有大神写得很详细了http://www.cnblogs.com/tim-li/archive/2012/08/06/2580252.html#8 在学习过后 ...

  7. MySQL与Sqlserver数据获取

    由于项目要求,一个.net mvc登录注册的东西网站必须放弃sqlserver数据去使用MySQL数据库,因此我遇到了一些问题,并找出相应的解决方法, 因为sqlserver跟MySQL的数据引擎不同 ...

  8. Smarty的应用

    smarty模板的核心是一个类,下载好的模板中有这么几个重要的文件夹 (1)libs核心文件夹(2)int.inc.php这是入口文件(3)plugins:自己写的插件文件夹(4)templates_ ...

  9. IBatis的分页研究

    IBatis的分页研究 博客分类: Ibatis学习   摘自: http://cpu.iteye.com/blog/311395 yangtingkun   Oracle分页查询语句 ibaits. ...

  10. 【sqli-labs】 less64 GET -Challenge -Blind -130 queries allowed -Variation3 (GET型 挑战 盲注 只允许130次查询 变化3)

    双括号整型 http://192.168.136.128/sqli-labs-master/Less-64/?id=1)) or ((1