POJ3414 Pots
题目:
输入:
有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))
输出:
样例:

分析:简单的BFS,难点在于回溯,给每个状态用数组记录路径
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cctype>
#define PI acos(-1.0)
const int INF = 0x3f3f3f3f;
const int NINF = -INF - ;
typedef long long ll;
using namespace std;
int a, b, c;
int used[][];
struct node
{
int x, y;
int flag;
int path[];//数组中0-5分别表示6种不同操作
}st;
string print[] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
void bfs()
{
queue<node> q;
for (int i = ; i <= a; ++i)
{
for (int j = ; j <= b; ++j)
used[i][j] = INF;
}
memset(used, , sizeof(used));
st.x = , st.y = ;
st.flag = ;
memset(st.path, -, sizeof(st.path));
q.push(st);
used[st.x][st.y] = ;
while (q.size())
{
node temp = q.front();
q.pop();
if (temp.x == c || temp.y == c)
{
cout << temp.flag << endl;
for (int i = ; i < temp.flag; ++i)
cout << print[temp.path[i]] << endl;
return;
}
for (int i = ; i < ; ++i)
{
node now = temp;
now.flag++;
if (i == && now.x != a)
{
now.x = a;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == && now.y != b)
{
now.y = b;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == && now.x != )
{
now.x = ;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == && now.y != )
{
now.y = ;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == )
{
if (now.x + now.y > b)
{
now.x -= b - now.y;
now.y = b;
}
else
{
now.y += now.x;
now.x = ;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == )
{
if (now.x + now.y > a)
{
now.y -= a - now.x;
now.x = a;
}
else
{
now.x += now.y;
now.y = ;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
}
}
cout << "impossible" << endl;
}
int main()
{
cin >> a >> b >> c;
bfs();
return ;
}
POJ3414 Pots的更多相关文章
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 快速切题 poj3414 Pots
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10042 Accepted: 4221 Special J ...
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ3414 Pots BFS搜素
题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...
- POJ-3414.Pots.(BFS + 路径打印)
这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...
- poj3414 Pots(BFS)
题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...
- 【BFS】Pots
[poj3414]Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16925 Accepted: 7168 ...
随机推荐
- 分类(Category)的本质 及其与类扩展(Extension) /继承(Inherit)的区别
1.分类的概念 分类是为了扩展系统类的方法而产生的一种方式,其作用就是在不修改原有类的基础上,为一个类扩展方法,最主要的是可以给系统类扩展我们自己定义的方法. 如何创建一个分类?↓↓ ()Cmd+N, ...
- Scala类型系统——高级类类型(higher-kinded types)
高级类类型就是使用其他类型构造成为一个新的类型,因此也称为 类型构造器(type constructors).它的语法和高阶函数(higher-order functions)相似,高阶函数就是将其它 ...
- GET 请求控制器 返回绑定后HTML
//$.get("/Home/index/" + $("#S_BookName").val(), function (data) { //MVC控制器返回Vie ...
- Object.assign和序列/反序列
Object.assign let testObj = { a:[1,2,4], b:{ name:'ls', school:['huf','yelu'], parent:{ father:'lili ...
- python tips:dict的key顺序
python3.6+版本中,dict的键值保持插入有序. t = list(range(10)) b = t[:] d = dict(zip(t, b)) print(list(d.items())) ...
- hibernate与spring整合
Spring与Hibernate整合关键点: 1) Hibernate的SessionFactory对象交给Spring创建: 2) hibernate事务交给spring的声明式事务管理. 1. D ...
- spotlight on mysql 监控
. 安装 下载地址:https://pan.baidu.com/s/1qYi3lec 官网地址——https://www.quest.com/common/registration.aspx?requ ...
- 【剑指Offer】55、链表中环的入口结点
题目描述: 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 解题思路: 本题是一个比较典型的链表题目,难度适中.首先,对于大多人来说,看到这道题是比较开心的 ...
- Vue push() pop() shift() unshift() splice() sort() reverse() ...
Vue 变异方法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. pop() 方法用于删除并返回数组的最后一个元素. shift() 方法用于把数组的第一个元素从其中删除,并返回 ...
- 转载:Java中的Checked Exception——美丽世界中潜藏的恶魔?
转自 Amber-Garden 的 博客 https://www.cnblogs.com/loveis715/p/4596551.html 在使用Java编写应用的时候,我们常常需要通过第三方类库来帮 ...