poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了。然后第一次用对拍去找特殊数据折腾到十二点半终于AC,最后只想感叹没有仔细读题,没办法啊看着英语略烦躁,不扯了,那个题题解不想写了,回到这题。。。今天中午还是花了四十分钟写了这题(好慢啊orz),感觉,啊为什么别人可以一下子就写出来,我却想不到怎么写呢!!!
题意:两个给定容量a, b的杯子,有倒满水,倒光水,互相倒水三个操作,直到任意一个杯子中的水达到指定容量c为止。输出操作步骤。(a,b,c≤100)
题解:六个方向的bfs,加上100*100的记录路径。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = ;
string s[] = {"impossible","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
int a, b, c;
typedef struct Node {
int x, y;
Node(int _x = , int _y = ):x(_x),y(_y){}
}node;
node g[N][N];//存储上一步的状态
int Do[N][N];//记录操作
bool vis[N][N];//标记
node bfs() {
CLR(g, -); CLR(vis, );
node t;
int i = , j = ;
queue<node> q;
q.push(Node(, ));
while(!q.empty()) {
t = q.front(); q.pop();
if(t.x == c || t.y == c) return t;
vis[t.x][t.y] = ;
if(t.x != a && !vis[a][t.y]) { q.push(Node(a, t.y)); Do[a][t.y] = ; g[a][t.y] = t; }
if(t.y != b && !vis[t.x][b]) { q.push(Node(t.x, b)); Do[t.x][b] = ; g[t.x][b] = t; }
if(t.x && !vis[][t.y]) { q.push(Node(, t.y)); Do[][t.y] = ; g[][t.y] = t; }
if(t.y && !vis[t.x][]) { q.push(Node(t.x, )); Do[t.x][] = ; g[t.x][] = t; }
if(!vis[i = max(, t.x + t.y - b)][j = min(b, t.x + t.y)]) { q.push(Node(i, j)); Do[i][j] = ; g[i][j] = t; }
if(!vis[i = min(a, t.x + t.y)][j = max(, t.x + t.y - a)]) { q.push(Node(i, j)); Do[i][j] = ; g[i][j] = t; }
}
return Node(, );
}
void dfs(const node & d, const int & p) {
if(d.x + d.y == ) {
if(p) { cout << p << endl; } else { cout << s[] << endl; }
return;
}
dfs(g[d.x][d.y], p+);
cout << s[Do[d.x][d.y]] << endl;
}
int main() {
scanf("%d%d%d", &a, &b, &c);
node ans = bfs();
dfs(ans, );
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
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- fetch将替代ajax?
原谅我做一次标题党,Ajax 不会死,传统 Ajax 指的是 XMLHttpRequest(XHR),未来现在已被 Fetch 替代. 最近把阿里一个千万级 PV 的数据产品全部由 jQuery 的 ...
- HTML页面中嵌入SVG
HTML页面中嵌入SVG的几种方式 你有N种理由使用SVG在页面中展示图像,如它的矢量特性.广泛的浏览器支持.比JPEG和PNG更小的体积.可用CSS设置外观.使用DOM API操作以及各种可用的SV ...
- 安装并开启ssh服务
sudo yum install openssh* 安装 2. 设置 sudo vi /etc/ssh/sshd_config 首先先把port改掉port 52222 限制用户AllowUse ...
- Golang教程:循环语句
循环语句用于重复执行一段代码. for 语句是 Go 中唯一的循环语句.Go 没有提供其他语言(如 C)中的 while 和 do while 语句. for 语句语法 for 语句的语法如下: fo ...
- [转]如何在 .Net Framework 4.0 项目上使用 OData?
本文转自:http://www.cnblogs.com/fiozhao/p/3536469.html 最新的 Microsoft ASP.NET Web API 2.1 OData 5.1.0 已只能 ...
- Asp.Net MVC4通过id更新表单
用户需求是:一个表单一旦创建完,其中大部分的字段便不可再编辑.只能编辑其中部分字段. 而不可编辑是通过对input输入框设置disabled属性实现的,那么这时候直接向数据库中submit表单中的内容 ...
- [android] 切换界面的问题
1. 界面重复创建的问题 2. 中间容器每次切换,都会清空容器中的子对象问题 3. 点击返回键的处理 解决重复创建的问题: 传递Class字节码对象,利用泛型来规定对象 判断界面是否存在,如果存在重复 ...
- mysql5.7忘记密码修改方法
mysql5.7忘记密码修改方法 mysql是开发中最常用的关系数据库之一.一般在安装数据库到时候会自定义root密码,有时候会忘记该密码,这时候需要对数据库进行密码修改. 一.windows下更改m ...
- 固态硬盘SSD与闪存(Flash Memory)
转自:http://qiaodahai.com/solid-state-drives-ssd-and-flash-memory.html 固态硬盘SSD(Solid State Drive)泛指使用N ...
- jQuery中的pushStack
在学习jquery源码的时候,学到了其中的pushStack方法,在这里记录一下 源码为 // Take an array of elements and push it onto the stack ...