poj3414 Pots (BFS)
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 12198 | Accepted: 5147 | 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)
Source
空杯子倒水问题,这题比较简单,只有两个杯子,最初的时候都是空的,要倒出指定量的水有三种操作:
1、FILL(i) 把第i个杯子装满(i=0,1)
2、DROP(i) 把第i个杯子倒空
3、POUR(i,j) 把i的水倒入到j中,直到j满或i倒完
我的想法:把a->b,b->c,。。。。共6种倒水方法一个一个列出来,而每种都是一样的讨论方法,
虽然很好做,但对于我这样的入门级水手来说还是写不出太好看的代码,所以。。。。
看看吧,看不懂再去看看网上别人的==||
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=;
bool vis[maxn][maxn];
int fa[maxn][maxn];
int op[maxn][maxn];
int sa,sb,sc;
struct node{
int a,b,step;
};
char ans[][]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
void print(int a ,int b){
if(op[a][b]==-)
return;
else{
print(fa[a][b]/,fa[a][b]%);
printf("%s\n",ans[op[a][b]]);
}
} bool bfs(){
node u,v;
u.a=,u.b=;
u.step=;
queue<node>q;
q.push(u);
memset(op,,sizeof(op));
memset(vis,false,sizeof(vis));
memset(fa,,sizeof(fa));
op[][]=-;
fa[][]=;
vis[][]=true;
while(!q.empty()){
u=q.front();
q.pop();
if(u.a==sc||u.b==sc){
printf("%d\n",u.step);
print(u.a,u.b);
return true;
}
v=u; if(u.a!=sa){
v.a=sa;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b!=sb){v.b=sb;
if(!vis[v.a][v.b]){
v.step=u.step+;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.a){v.a=;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b){v.b=;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.a){
if(v.a>=sb-u.b&&u.b!=sb){v.a-=(sb-u.b);v.b=sb;}
else if(v.a<sb-u.b){v.a=;v.b+=u.a;}
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b)
{
if(v.b>=sa-u.a&&u.a!=sa){v.b-=(sa-u.a);v.a=sa;}
else if(v.b<sa-u.a){v.b=;v.a+=u.b;}
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
} }
return false;
}
int main(){
while(scanf("%d%d%d",&sa,&sb,&sc)!=EOF){
bool flag=bfs(); if(!flag)
printf("impossible\n");
}
return ;
}
poj3414 Pots (BFS)的更多相关文章
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj3414 Pots(BFS)
题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- Pots(BFS)
Pots Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submiss ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- arr.forEach()与for...in的用法举例
1.forEach() 将给定的数字转换成罗马数字. 所有返回的 罗马数字 都应该是大写形式. function convert(num) { var str = ""; var ...
- 对mysql快速批量修改,查重
更新UPDATE mytable SET myfield = CASE id WHEN 1 THEN 'value' WHEN 2 THEN 'value' WHEN 3 THEN 'value' E ...
- react组件间的传值方法
关于react的几个网站: http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.re ...
- 《转载》ASP动态iframe
原文:[ASP.NET]关于iframe的两个技巧 最近在给朋友写个网站,虽然不大,但是也碰到了一些问题.这篇就为解决ASP.NET中关于IFRAME的两个很现实的问题提供解决方法.PS:呵呵,又做了 ...
- Mysql 查询出某列字段 被包含于 条件数据中
我们通常是使用 某条件 是否包含于 某列中 ,简单点 就是:select * from 表名 where 字段名 like '%条件数据%'; 现在说下 某列 被包含于 条件数据中 接下 ...
- Http请求 GET和POST,405错误
我就简单说吧,在用SringMVC时,我们通常会用到 @RequestMapping(value="/test",method=RequestMethod.GET) public ...
- 对比传统方式访问数据库和SpringData访问数据库
我们在写代码的时候应该一边写一边测试,这样的话可以尽快的找到错误,在代码写多了之后去找错误的话不容易给错误定位 传统方式访问数据库 1:创建一个Maven web项目 2:修改pom.xml为以下内容 ...
- vue之神奇的动态按钮
今天我们将利用vue的条件指令来完成一个简易的动态变色功能按钮 首先我们还是要对vue进行配置,在上篇随笔中有相关下载教学. 然后我们要在html页面上搭建三个简易的按钮,颜色分别为紫,绿和蓝(颜色随 ...
- django_mysql_配置
配置 1. 安装Pymysql pip install PyMySQL 然后在项目同名_init__添加 from pymysql import install_as_MySQLdb install_ ...
- Linux系统软件包之---Apache
当前互联网主流web服务说明 静态服务: apache 中小型静态web服务的主流,web服务器中的老大哥 nginx 大型新兴网站静态web服务主流,web服务器中的初生牛犊 lighttpd 静态 ...