POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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)
- 怎么说呢,BFS,有六种状态
写起来还是有点烦的,要细心
- #include<cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- #include<queue>;
- using namespace std;
- struct node
- {
- int av,aw,bv,bw;
- int o[],pos;
- }s;
- bool vis[][];
- int f;
- node cur,next;
- void fill(int i)
- {
- if(i==)
- next.aw=next.av;
- else if(i==)
- next.bw=next.bv;
- }
- void drop(int i)
- {
- if(i==)
- next.aw=;
- else
- next.bw=;
- }
- void pour(int a,int b)
- {
- if(a==&&b==){
- if(next.aw>next.bv-next.bw)
- {
- next.aw-=(next.bv-next.bw);
- next.bw=next.bv;
- }
- else
- {
- next.bw+=next.aw;
- next.aw=;
- }}
- else
- {
- if(next.bw>next.av-next.aw)
- {
- next.bw-=(next.av-next.aw);
- next.aw=next.av;
- }
- else
- {
- next.aw+=next.bw;
- next.bw=;
- }
- }
- }
- void print(int i)
- {
- if(i==)
- printf("POUR(1,2)\n");
- else if(i==)
- printf("POUR(2,1)\n");
- else if(i==)
- printf("FILL(1)\n");
- else if(i==)
- printf("FILL(2)\n");
- else if(i==)
- printf("DROP(1)\n");
- else if(i==)
- printf("DROP(2)\n");
- }
- bool bfs()
- {
- memset(vis,false,sizeof(vis));
- s.aw=;s.bw=;
- s.pos=;
- vis[][]=true;
- queue<node>Q;
- Q.push(s);
- while(!Q.empty())
- {
- cur=Q.front();
- Q.pop();
- if(f==cur.aw||f==cur.bw)
- {
- printf("%d\n",cur.pos);
- for(int i=;i<cur.pos;i++)
- {
- print(cur.o[i]);
- }
- return true;
- break;
- }
- //
- if(cur.aw!=&&cur.bw!=cur.bv){
- next=cur;
- pour(,);
- next.o[next.pos]=;
- next.pos++;
- if(!vis[next.aw][next.bw])
- {
- vis[next.aw][next.bw]=true;
- Q.push(next);
- }}
- //
- if(cur.bw!=&&cur.aw!=cur.av){
- next=cur;
- pour(,);
- next.o[next.pos]=;
- next.pos++;
- if(!vis[next.aw][next.bw])
- {
- vis[next.aw][next.bw]=true;
- Q.push(next);
- }}
- //
- if(cur.aw!=cur.av){
- next=cur;
- fill();
- next.o[next.pos]=;
- next.pos++;
- if(!vis[next.aw][next.bw])
- {
- vis[next.aw][next.bw]=true;
- Q.push(next);
- }}
- //
- if(cur.bw!=cur.bv){
- next=cur;
- fill();
- next.o[next.pos]=;
- next.pos++;
- if(!vis[next.aw][next.bw])
- {
- vis[next.aw][next.bw]=true;
- Q.push(next);
- }}
- //
- if(cur.aw!=){
- next=cur;
- drop();
- next.o[next.pos]=;
- next.pos++;
- if(!vis[next.aw][next.bw])
- {
- vis[next.aw][next.bw]=true;
- Q.push(next);
- }}
- //
- if(cur.bw!=){
- next=cur;
- drop();
- next.o[next.pos]=;
- next.pos++;
- if(!vis[next.aw][next.bw])
- {
- vis[next.aw][next.bw]=true;
- Q.push(next);
- }}
- }
- return false;
- }
- int main()
- {
- while(scanf("%d%d%d",&s.av,&s.bv,&f)!=EOF)
- {
- if(!bfs())printf("impossible\n");
- }
- return ;
- }
POJ 3414 Pots(BFS)的更多相关文章
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- poj 3414 Pots bfs+模拟
#include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- python 获取当前时间
我有的时候写程序要用到当前时间,我就想用python去取当前的时间,虽然不是很难,但是老是忘记,用一次丢一次,为了能够更好的记住,我今天特意写下python 当前时间这篇文章,如果你觉的对你有用的话, ...
- CloudFormation
亚马逊云服务之CloudFormation 亚马逊的Web Service其实包含了一套云服务.云服务主要分为三种: IaaS: Infrastructure as a service,基础设施即 ...
- 软件快速开发平台 WebBuilder 6.8
WebBuilder是一款开源的跨平台.数据库和浏览器的可视化Web应用快速开发平台.WebBuilder使用了多项最新的技术,使Web应用的开发更快捷和简单. 作为一款高效的Web开发工具,WebB ...
- Excel 开发概述
浅谈Excel开发:一 Excel 开发概述 做Office相关的开发工作快一年多了,在这一年多里,在插件的开发中遇到了各种各样的问题和困难,还好同事们都很厉害,在和他们的交流讨论中学到了很多的知识. ...
- Http的四种post方式
1.引言 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交 ...
- kivy Create an application
http://kivy.org/docs/guide/basic.html#quickstart I followed this tutorial about how to create basic ...
- 使用 javascript 来实现 观察者模式
以[猫叫.老鼠跑.主人醒]为例子,使用 javascript 来实现 观察者模式 (有在线演示) 2013-06-24 08:35 by 金色海洋(jyk)阳光男孩, 572 阅读, 4 评论, 收藏 ...
- POJ 1033 Defragment
根据http://hi.baidu.com/algorithm/item/d51b15f7a8ea1c0a84d278be这个开始练习ac,刚开始接触这道题时以为是道搜索题,读完之后深思了一下,感觉不 ...
- .Net程序员学用Oracle系列(5):三大数据类型
<.Net程序员学用Oracle系列:导航目录> 本文大纲 1.Oracle 数据类型概述 2.字符类型 2.1.字符集 & NLS 2.2.常见的两种字符串 2.3.NCHAR ...
- CCNA网络工程师学习进程(7)路由器的路由配置
前面一节已经介绍了路由器的端口配置,接着我们介绍路由器的路由配置:静态路由.默认路由和浮动路由的配置:动态路由协议的配置,包括RIP.IGRP.EIGRP和OSPF. (1)路由器的基 ...