poj3414Pots(倒水BFS)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 13231 | Accepted: 5553 | Special Judge |
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K 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"cstdio"
- #include"cstring"
- #include"queue"
- #include"algorithm"
- using namespace std;
- const int MAXN=;
- struct node{
- int a,b,op,pre;
- node(int ca,int cb,int co,int cp):a(ca),b(cb),op(co),pre(cp){}
- node(){}
- };
- const char* opit[]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
- int vis[MAXN][MAXN];
- int A,B,C;
- node step[MAXN*MAXN];
- int cnt;
- void print(int now,int ans)//??????
- {
- node no = step[now];
- if(no.pre==-)
- {
- printf("%d\n",ans);
- return ;
- }
- print(no.pre,ans+);
- printf("%s\n",opit[no.op]);
- }
- void bfs()
- {
- memset(vis,,sizeof(vis));
- cnt=;
- queue<node> que;
- que.push(node(,,,-));
- while(!que.empty())
- {
- node now = que.front();que.pop();
- step[cnt++]=now;
- if(now.a==C||now.b==C)
- {
- print(cnt-,);
- return ;
- }
- int ta,tb;
- //第一种操作 FILL(A)
- ta=A,tb=now.b;
- if(!vis[ta][tb])
- {
- vis[ta][tb]=;
- que.push(node(ta,tb,,cnt-));
- }
- //第二种操作 FILL(B)
- ta=now.a,tb=B;
- if(!vis[ta][tb])
- {
- vis[ta][tb]=;
- que.push(node(ta,tb,,cnt-));
- }
- //第三种操作 DROP(A)
- ta=,tb=now.b;
- if(!vis[ta][tb])
- {
- vis[ta][tb]=;
- que.push(node(ta,tb,,cnt-));
- }
- //第四种操作 DROP(B)
- ta=now.a,tb=;
- if(!vis[ta][tb])
- {
- vis[ta][tb]=;
- que.push(node(ta,tb,,cnt-));
- }
- //第五种操作 POUR(A,B)
- ta=now.a-min(B-now.b,now.a);
- tb=now.b+min(B-now.b,now.a);
- if(!vis[ta][tb])
- {
- vis[ta][tb]=;
- que.push(node(ta,tb,,cnt-));
- }
- //第六种操作 POUR(B,A)
- ta=now.a+min(A-now.a,now.b);
- tb=now.b-min(A-now.a,now.b);
- if(!vis[ta][tb])
- {
- vis[ta][tb]=;
- que.push(node(ta,tb,,cnt-));
- }
- }
- printf("impossible\n");
- }
- int main()
- {
- while(scanf("%d%d%d",&A,&B,&C)!=EOF)
- {
- bfs();
- }
- return ;
- }
poj3414Pots(倒水BFS)的更多相关文章
- hdu1495 倒水bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1495/ 题意:给定三个杯子S,M,N,满足S=M+N,现在要求用最短的次数将S杯中的饮倒平分到两个杯子中.我们首 ...
- HDOJ1495(倒水BFS)
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- codevs1226倒水问题(Bfs)
/* 首先建立模型 可以看成是三个水杯 第三个无穷大 (这里看成是201足够了) 最少步数 想到Bfs 维护队列里的状态:要有个步数 还要有v :此时刻三个杯子有多少水 然后倒水:因为没有刻度 所以有 ...
- HDU 1495 非常可乐(BFS倒水问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101) ...
- HDU 1495 非常可乐【BFS/倒水问题】
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- CodeVS 1226 倒水问题【DFS/BFS】
题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水 ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- BFS(倒水问题) HDU 1495 非常可乐
题目传送门 /* BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都 ...
随机推荐
- 我的设计模式学习笔记------>Java设计模式总概况
设计模式(Design Pattern)的概念最早起源于建筑设计大师Alexander的<建筑的永恒方法>一书,尽管Alexander的著作是针对建筑领域的,但是他的观点实际上用用于所有的 ...
- ros下单目相机校正
1. 安装对应的驱动与程序包. 图像对应包 http://wiki.ros.org/camera_calibration 在gitbub下载image_pipeline : ...
- linux c编程:进程控制(三)_exec函数
fork()函数通过系统调用创建一个与原来进程(父进程)几乎完全相同的进程(子进程是父进程的副本,它将获得父进程数据空间.堆.栈等资源的副本.注意,子进程持有的是上述存储空间的“副本”,这意味着父子进 ...
- CENTOS7 修改网卡名称为eth[012...],格式
具体操作是修改/etc/default/grub文件 在GRUB_CMDLINE_LINUX一行中添加net.ifnames=0 biosdevname=0 保存文件后然后运行 grub2-mkcon ...
- 用cocos2d-html5做的消除类游戏《英雄爱消除》(4)——游戏结束
游戏结束界面: 在前面几个教程中,这个界面的创作所需要的知识点基本我们都讲过了,这里就说下用户数据的缓存吧,也是先来看下源码 /** * Power by html5中文网(html5china.co ...
- 网络编程概述和IP地址的获取方法
java网络通信概述 一.网络通信步骤: 主机1 主机2 QQ-------QQ FEIQ-----FEIQ 1.找到对方IP. 2.找到对方端口号.数据要发送到对方的应用程序上.为了标识这些应用程序 ...
- [原创]关于设置linux中vim 显示行号
1.更改所有账户配置 直接更改/etc/vimrc vim /etc/vimrc 在vimrc文件的最后添加 set nu 即可 wq退出. 这样,不论使用哪个账号登陆,vim打开后都显示行号 2.为 ...
- 使用 sqoop 将mysql数据导入到hive表(import)
Sqoop将mysql数据导入到hive表中 先在mysql创建表 CREATE TABLE `sqoop_test` ( `id` ) DEFAULT NULL, `name` varchar() ...
- python练习_简单登录
python练习_简单登录 需求: 做一个登录的小程序,验证用户与密码 要求登录三次错误后锁定 以下代码实现的功能与思路: 功能: 1.除admin以外的用户累计登录失败次数超过三次则锁定,此时需要管 ...
- jQuery应用之eraser.js使用,实现擦除、刮刮卡效果
jquery.eraser是一款使用鼠标或触摸的动作来擦除画布显示真正图片的插件.jquery.eraser插件的原理是用一个画布遮住图片,然后根据触摸或鼠标输入来擦除画布显示图片,您可以在参数中指定 ...