(简单) POJ 3414 Pots,BFS+记录路径。
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.
就是对两个杯子进行操作了,装满,倒空,倒过去。两个水杯里的水表示一个状态,然后BFS就好,要记录路径的话,记住每一个的父亲,然后最后反着来一遍就好了。
代码如下:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue> using namespace std; int A,B,C;
int rem[][];
int shorem[]; struct state
{
int a,b;
int type;
int faa,fab;
int num; state() {}
}; state sta[][]; void showans(state *e)
{
int cou=; while(e->a||e->b)
{
shorem[cou++]=e->type; e=&sta[e->faa][e->fab];
} cout<<cou<<endl;
for(int i=cou-;i>=;--i)
switch(shorem[i])
{
case :
cout<<"FILL(1)\n";
break;
case :
cout<<"FILL(2)\n";
break;
case :
cout<<"DROP(1)\n";
break;
case :
cout<<"DROP(2)\n";
break;
case :
cout<<"POUR(1,2)\n";
break;
case :
cout<<"POUR(2,1)\n";
break;
} } void slove()
{
queue <state *> que;
state *temp;
int t1,t2,t3; sta[][].faa=sta[][].fab=-;
sta[][].type=;
sta[][].num=;
que.push(&sta[][]); while(!que.empty())
{
temp=que.front();
que.pop(); if(temp->a==C||temp->b==C)
{
showans(temp);
return;
} t1=temp->a;
t2=temp->b; if(sta[A][t2].num==-)
{
sta[A][t2].num=temp->num+;
sta[A][t2].faa=t1;
sta[A][t2].fab=t2;
sta[A][t2].type=; que.push(&sta[A][t2]);
}
if(sta[t1][B].num==-)
{
sta[t1][B].num=temp->num+;
sta[t1][B].faa=t1;
sta[t1][B].fab=t2;
sta[t1][B].type=; que.push(&sta[t1][B]);
}
if(sta[][t2].num==-)
{
sta[][t2].num=temp->num+;
sta[][t2].faa=t1;
sta[][t2].fab=t2;
sta[][t2].type=; que.push(&sta[][t2]);
}
if(sta[t1][].num==-)
{
sta[t1][].num=temp->num+;
sta[t1][].faa=t1;
sta[t1][].fab=t2;
sta[t1][].type=; que.push(&sta[t1][]);
}
t3=min(t1,B-t2);
if(sta[t1-t3][t2+t3].num==-)
{
sta[t1-t3][t2+t3].num=temp->num+;
sta[t1-t3][t2+t3].faa=t1;
sta[t1-t3][t2+t3].fab=t2;
sta[t1-t3][t2+t3].type=; que.push(&sta[t1-t3][t2+t3]);
}
t3=min(t2,A-t1);
if(sta[t1+t3][t2-t3].num==-)
{
sta[t1+t3][t2-t3].num=temp->num+;
sta[t1+t3][t2-t3].faa=t1;
sta[t1+t3][t2-t3].fab=t2;
sta[t1+t3][t2-t3].type=; que.push(&sta[t1+t3][t2-t3]);
}
} cout<<"impossible\n";
} int main()
{
ios::sync_with_stdio(false); for(int i=;i<=;++i)
for(int j=;j<=;++j)
{
sta[i][j].a=i;
sta[i][j].b=j;
} while(cin>>A>>B>>C)
{
for(int i=;i<=;++i)
for(int j=;j<=;++j)
sta[i][j].num=-; slove();
} return ;
}
(简单) POJ 3414 Pots,BFS+记录路径。的更多相关文章
- 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 POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- 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)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- 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+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
随机推荐
- table详解
1.tr 元素定义表格行,th 元素定义表头,td 元素定义表格单元. tr内是th还是td可由自己定义,th,td可存在于任一行,th与td的区别在与th字体更粗 2.定义一个table默认有bor ...
- log4j2.xml配置及例子
1.使用log4j2需要下载包,如下: 2.配置文件可以有三种格式(文件名必须规范,否则系统无法找到配置文件): classpath下名为 log4j-test.json 或者log4j-test.j ...
- Tomcat服务器
常见的web服务器 1. WebLogic是BEA公司的产品,是目前应用最广泛的web服务器,支持J2EE规范(J2EE里有13种技术),商业产品,收费的.银行,证券等对并发,安全要求高的时候用,一般 ...
- 转:loadrunner ---循环输出关联数组
web_reg_save_param,将Ord参数值设定为ALL,则关联函数将自动把符合条件的关联值保存到参数数组里.在本例中,假设关联值返回三条记录,则LR分别将值保存到sor_1,sor_2,so ...
- linux下安装rabbitmq
1.安装erlang虚拟机 Rabbitmq基于erlang语言开发,所有需要安装erlang虚拟机.安装erlang有两种方式: 第一种:使用yum安装: wget -O /etc/yum.repo ...
- ormlite 多表联合查询
ormlite 多表联合查询 QueryBuilder shopBrandQueryBuilder = shopBrandDao.queryBuilder(); QueryBuilder shopQu ...
- zepto学习之路--核心函数$()的实现
$()可以说是jquery的精华了,为dom操作带来了极大的灵活和方便.zepto号称“移动版的jquery”,那么它是怎么来实现这个核心函数呢?我们来详细探讨下. 1.首先,我们看下zepto中它是 ...
- codeforces div2 677 D
http://codeforces.com/problemset/problem/677/D 题目大意: 给你一个n*m的图,上面有p种钥匙(p<=n*m),每种钥匙至少有一个,期初所有为1的钥 ...
- HDU 1934 树状数组 也可以用线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...
- SQL查询重复记录
假设现有一张人员表(表名:Person),若想将姓名.身份证号.住址这三个字段完全相同的记录查找出来,使用 1: SELECT p1.* 2: FROM persons p1,persons p2 3 ...