//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. 我该不该学习C语言

    这几天把c语言过了一遍,基本上算是入门了,常用语法.函数的使用.c语言是比较古老的语言了,很多系统的底层.工业控制软件都是使用C语言编写,过一遍之后觉得c语言屹立不倒是有原因.c程序员有一句话:使用c ...

  2. HDU 5701 ——中位数计数——————【思维题】

    中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  3. 获取IP相关信息和文件上传

    获取IP相关信息 要获取用户访问者的IP地址相关信息,可以利用依赖注入,获取IHttpConnectionFeature的实例,从该实例上可以获取IP地址的相关信息,实例如下: var connect ...

  4. C#基础笔记 转自wojiushigelg

    笔记如下: 概念:.net与c# .net/dontnet:一般指.net framework框架,一种平台,一种技术 c#(charp):一种编程语言,可以开发基于.net的应用. *java既是一 ...

  5. 告别Flash——那些年我们追过的FusionCharts

    随着FusionCharts最终放弃Flash这块蛋糕,不.或者已经不能叫做蛋糕了,现在Flash图表控件就只剩下AnyChart这一个独苗了,到底Flash还能走多远?这是Flash的末路吗? 众说 ...

  6. spring-boot配置log4j日志

    spring boot默认使用logback日志记录工具,修改为log4j: <dependency> <groupId>org.springframework.boot< ...

  7. spring的事务管理配置

    spring有两种事务配置器,可以使用spring的jdbc事务管理器,也可以使用对hibernate的事务管理器 第一种 使用Spring JDBC或IBatis进行事务配置(配置文件方式): &l ...

  8. centos下如何停止ping命令

    ctrl + c 或者 Ctrl + d(好像不行) man ping

  9. Thread 的join方法

    package com.cn.test.thread; public class TestJoin extends Thread{ private String name; public TestJo ...

  10. 最近使用日期控件时,用到了My97DatePicker控件,单日期控件,记录一下

    以上是使用时的效果,可以自己设定日期有效区间,如下图: 对于起始日期和终止日期的控制如下: <td> <label >起始日期:</label> <input ...