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互倒的时候能不能倒满, ...
随机推荐
- Nginx:HTTP框架是如何介入请求
参考资料 <深入理解Nginx>(陶辉) Nginx事件模块博客地址:http://www.cnblogs.com/runnyu/p/4914698.html Nginx是一个事件驱动构架 ...
- 获取web应用路径 // "/" 表示class 根目录
/** * 获取web应用路径 * @Description : 方法描述 * @Method_Name : getRootPath * @return * @return : String * @C ...
- Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding
Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding 深度学 ...
- mngoDB 常用语法
http://topmanopensource.iteye.com/blog/1278812### 连接写法:[IP地址:端口号] mongo 192.168.1.161:27017; show db ...
- 结合jquery的前后端加密解密 适用于WebApi的SQL注入过滤器 Web.config中customErrors异常信息配置 ife2018 零基础学院 day 4 ife2018 零基础学院 day 3 ife 零基础学院 day 2 ife 零基础学院 day 1 - 我为什么想学前端
在一个正常的项目中,登录注册的密码是密文传输到后台服务端的,也就是说,首先前端js对密码做处理,随后再传递到服务端,服务端解密再加密传出到数据库里面.Dotnet已经提供了RSA算法的加解密类库,我们 ...
- 在caffe中用训练好的 caffemodel 来分类新的图片所遇到的问题
结合之前的博客: http://www.cnblogs.com/Allen-rg/p/5834551.html#3949333 用caffemodel去测试单通道的图像(mnist数据集)时,出现了问 ...
- 捕获网络数据包并进行分析的开源库-WinPcap
什么是WinPcap WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库. 大多数网络应用程序通过被广泛使用的操作系统元件来访问网络,比如sockets. 这是一种简单的 ...
- 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...
- iOS tableView高度缓存
tableView计算完高度后,把高度缓存起来,避免下次重复计算,以减少不必要的消耗 // declare cellHeightsDictionary NSMutableDictionary *cel ...
- 篇三、开发前知识补充:Android的长度单位和屏幕分辨率,这个也是转载~~
这篇文章有点早,不过很实用.单位的实用看最后的红色标注的部分. 屏幕分辨率基础 1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3 ...