//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了。然后第一次用对拍去找特殊数据折腾到十二点半终于AC,最后只想感叹没有仔细读题,没办法啊看着英语略烦躁,不扯了,那个题题解不想写了,回到这题。。。今天中午还是花了四十分钟写了这题(好慢啊orz),感觉,啊为什么别人可以一下子就写出来,我却想不到怎么写呢!!!

poj 3414 Pots  【BFS】

题意:两个给定容量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+记录路径 】的更多相关文章

  1. poj 3414 Pots(bfs+输出路径)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  2. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  3. Pots POJ - 3414 (搜索+记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special J ...

  4. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  5. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  6. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  7. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  8. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  9. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

随机推荐

  1. 九度oj题目1555:重复子串

    题目1555:重复子串 时间限制:3 秒 内存限制:256 兆 特殊判题:否 提交:738 解决:125 题目描述: 给定一个由小写字母组成的字符串,求它的所有连续子串中,出现过至少两次,且至少有一对 ...

  2. ExtJs6内嵌iframe,nginx部署本地前台文件

    /** * Created by Wwei on 2016/9/1. */ Ext.define('Admin.view.photo.CADMultiUploadForm', { extend: 'E ...

  3. YII框架一个请求的生命周期

    用户向入口脚本 web/index.php 发起请求. 入口脚本加载应用配置并创建一个应用实例去处理请求. 应用通过请求组件解析请求的路由. 应用创建一个控制器实例去处理请求. 控制器创建一个操作实例 ...

  4. Firebird with lock

    Firebird 锁,默认是行级锁,即记录锁. 通常最常用的是 with lock ,即:将查出的所有记录都锁定,但允许其他事务读取,不允许其他事务更新.删除.本事务允许更新. 另一种 for upd ...

  5. [转].NET Core dotnet 命令大全

    本文转自:http://www.cnblogs.com/linezero/p/dotnet.html https://docs.microsoft.com/en-us/dotnet/articles/ ...

  6. vue中echarts随窗体变化

    <div id="myChart" :style="{width: '100%', height: '345px'}"></div> & ...

  7. Swift函数_inout参数

    //无inout参数的函数 func changeName(var name:String){ name = "Hello" println(name) } let payerNa ...

  8. 怎样在ado.net中存取excel和word呢?

    办公软件指可以进行文字处理.表格制作.幻灯片制作.图形图像处理.简单数据库的处理等方面工作的软件.当然啦,这也包括了word.Excel以及PPT等.现在我们就一起来学习一下:怎样在ado.net中存 ...

  9. MySQL的四种事务隔离级别【转】

    本文实验的测试环境:Windows 10+cmd+MySQL5.6.36+InnoDB 一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做 ...

  10. DOM Tree

    DOM Tree   什么是DOM树:网页的所有内容在内存当中,其实是以树形结构存储的 何时创建:当浏览器,读取html中内容的时候,会马上开始创建DOM树. 如何创建: 1.读到HTML的时候还没有 ...