POJ - 3414 Pots 【BFS】
题目链接
http://poj.org/problem?id=3414
题意
给出两个杯子 容量分别为 A B 然后给出C 是目标容量
有三种操作
1 将一个杯子装满
2.将一个杯子全都倒掉
3.将一个杯子的水倒到另一个杯子里面
如果某个杯子里面的水 能够达到 目标容量 那么就输出步骤
思路
BFS 并且要存储步骤
每一步一共有六步操作 记得标记
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
const int MOD = 1e9 + 7;
/*
0 FILL 1
1 FILL 2
2 DROP 1
3 DROP 2
4 POUR 1 2
5 POUR 2 1
*/
int visit[maxn][maxn];
vector <int> ans;
int flag;
int a, b, c;
struct node
{
int a, b;
vector <int> v;
};
void bfs()
{
queue <node> q;
node tmp;
tmp.a = 0;
tmp.b = 0;
tmp.v.clear();
q.push(tmp);
visit[0][0] = 1;
while (!q.empty())
{
node u = q.front(), v;
q.pop();
if (u.a == c || u.b == c)
{
flag = 1;
ans = u.v;
return;
}
if (u.a < a)
{
v.a = a;
v.b = u.b;
if (visit[v.a][v.b] == 0)
{
v.v = u.v;
v.v.pb(0);
q.push(v);
visit[v.a][v.b] = 1;
}
}
if (u.b < b)
{
v.a = u.a;
v.b = b;
if (visit[v.a][v.b] == 0)
{
v.v = u.v;
v.v.pb(1);
q.push(v);
visit[v.a][v.b] = 1;
}
}
if (u.a > 0)
{
v.a = 0;
v.b = u.b;
if (visit[v.a][v.b] == 0)
{
v.v = u.v;
v.v.pb(2);
q.push(v);
visit[v.a][v.b] = 1;
}
}
if (u.b > 0)
{
v.a = u.a;
v.b = 0;
if (visit[v.a][v.b] == 0)
{
v.v = u.v;
v.v.pb(3);
q.push(v);
visit[v.a][v.b] = 1;
}
}
if (u.a < a)
{
int c = a - u.a;
if (u.b >= c)
{
v.b = u.b - c;
v.a = a;
}
else
{
v.b = 0;
v.a = u.a + u.b;
}
if (visit[v.a][v.b] == 0)
{
v.v = u.v;
v.v.pb(5);
q.push(v);
visit[v.a][v.b] = 1;
}
}
if (u.b < b)
{
int c = b - u.b;
if (u.a >= c)
{
v.a = u.a - c;
v.b = b;
}
else
{
v.a = 0;
v.b = u.a + u.b;
}
if (visit[v.a][v.b] == 0)
{
v.v = u.v;
v.v.pb(4);
q.push(v);
visit[v.a][v.b] = 1;
}
}
}
}
int main()
{
map <int, string> M;
M[0] = "FILL(1)";
M[1] = "FILL(2)";
M[2] = "DROP(1)";
M[3] = "DROP(2)";
M[4] = "POUR(1,2)";
M[5] = "POUR(2,1)";
CLR(visit, 0);
scanf("%d%d%d", &a, &b, &c);
flag = 0;
bfs();
if (flag == 0)
printf("impossible\n");
else
{
int len = ans.size();
cout << len << endl;
for (int i = 0; i < len; i++)
cout << M[ans[i]] << endl;
}
}
POJ - 3414 Pots 【BFS】的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ 3414 Pots 暴力,bfs 难度:1
http://poj.org/problem?id=3414 记录瓶子状态,广度优先搜索即可 #include <cstdio> #include <cstring> #inc ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- (简单) 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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- 【BFS】POJ 3414
直达 -> POJ 3414 Pots 相似题联动–>HDU 1495 非常可乐 题意:两个壶倒水,三种操作,两个桶其中一个满足等于C的最少操作,输出路径.注意a,b互倒的时候能不能倒满, ...
随机推荐
- 【Excle数据透视表】如何得到数据透视表中某个汇总行的明细数据
例如: 现在想得到"北京 汇总"的明细数据,该怎么处理呢? 步骤 右键数据透视表任意单元格→数据透视表选项→启用显示明细数据→确定→单击"北京 汇总"行最后一个 ...
- mongo 增
mongodb存储的是文档,文档是json格式的对象,我们的增删改查,都要传输json对象 json是一个对象,js里有数组这个概念,只需要把多个对象放到一个数组里,即可 use test //首先选 ...
- Loadrunner 关于参数赋值取值的操作
1.参数的赋值和取值 lr_save_string("hello world","param"); lr_eval_string("{param}&q ...
- Android模拟屏幕点击input tap替代解决方案
动机解释 本来直接使用 adb shell -> input 即可模拟 键盘事件,触屏事件keyevent ,text,tap 但是手上的这台目标Android机4.0.3系统的input只支持 ...
- unity3d WebPlayer版本号音效无声音问题
unity web player,其是一款浏览器执行unity3d游戏引擎公布的游戏的插件,和Flash Player非常像,安全无毒应该是你玩某款网页游戏安装的.假设以后不玩了就能够卸载 Unity ...
- atitit.基于bat cli的插件管理系统.doc
atitit.基于bat cli的插件管理系统.doc /AtiPlatf/src_atibrow/com/attilax/cmd/CmdX.java pathx.isWebPathMode=true ...
- Eclipse Plugin Installation and Windows User Access Control
I make Eclipse Plugins and I sell them to developers using Eclipse. Most of the visitors to my web s ...
- Sphinx之配置文件
# # Sphinx configuration file sample # # WARNING! While this sample file mentions all available opti ...
- iOS iPhoneX/iPhoneXS/iPhoneXR/iPhoneXS Max系列适配
以前异性屏只有一款iPhoneX,所以在适配的时候直接判断高度是否等于812即可判断是否是iPhoneX #define IS_IPHONE_X (IS_IPHONE && SCREE ...
- PL/0编译程序
Pl/0语言文法的BNF表示: 〈程序〉→〈分程序>. 〈分程序〉→ [<常量说明部分>][<变量说明部分>][<过程说明部分>]〈语句〉 <常量说明部 ...