(简单) 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了.然后第一次 ...
随机推荐
- php composer 安装
- git简单常用的命令
git status --查看文件状态 git add+文件路径 --上传到缓存区 git add --all --全部传到缓存区 git commit -m '描述' --对上传文件做描述 git ...
- Polycarp's problems
Polycarp's problems time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- php 10.2总
注意事项 获取表单信息 <?php if($_POST["submit"]=="登录"){ echo "您输入的用户名为:".$_PO ...
- Flask architecture
论文The Flask Security Architecture: System Support for Diverse Security Policies 介绍了Flask architectur ...
- Swift 与 JSON 数据
转载自: http://www.cnblogs.com/theswiftworld/p/4660177.html 我们大家平时在开发 App 的时候,相信接触最多的就是 JSON 数据了.只要你的 A ...
- 好的 小图标 html
只需引用样式,加入字体文件到项目中就可实现 <link href="css/font-awesome.min.css" rel="stylesheet"& ...
- centos dmesg
linux dmesg命令详解 功能说明:显示开机信息. 语 法:dmesg [-cn][-s ] 补充说明:kernel会将开机信息存储在ring buffer,若是开机时来不及查看信息,可利用 ...
- cap
http://blog.javachen.com/2014/05/30/note-about-brewers-cap-theorem.html
- oracle有三个默认的用户名和密码,但是都无法登录的解决方法
system/change_on_install, system/manager是较旧版的预设密码, 在安装较新版时会提示你设定密码, 若没有或忘了设定, 请参考以下重设: sqlplus / as ...