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 ...
随机推荐
- ormLite注解小记
注解是特殊的代码标志已在Java版本开始,要指定什么类和字段存储在数据库中,ORMLite支持其自己的注解(@ DatabaseTable @ DatabaseField)或更多的标准注解从javax ...
- Consul 遇到的坑
均衡负载时调用的地址 spring.cloud.consul.discovery.service-name= 当A服务调用B服务时,可以转发到注册中心进行转发调用, 应该使用这个地址,这一点和eure ...
- LinkedList封装
LinkedList简单的封装 package com.cn.test.jihe.LinkedList; import java.util.NoSuchElementException; public ...
- Effective C++ .08 别让异常逃离析构函数
异常不怎么用,C++能自己控制析构过程,也就有这个要求了.容器不能完全析构其中的元素真是太危险了
- Hibernate 注解 (Annotations 三)多对一双向注解
注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法.局部变量.方法参数等的前面,用来 ...
- java unsupported major.minor version 51.0 解决
1.概述 出现如题所述异常 是因为jdk高版本 编译后的class文件 运行在低版本的jre环境下(如jdk7编译 运行在jdk6环境下) 2. 解决方案 在eclipse等ide中重新编译 指定编译 ...
- div 旋转
-webkit-transform:rotate(120deg); -moz-transform:rotate(120deg); -o-transform:rotate(120deg); /* fil ...
- Web前端面试指导(二十):JavaScript中如何翻转一个字符串?
题目点评 字符串作在程序中是非常常见的,因为程序中绝大部分的数据都可以当作字符串来处理.需要对字符的处理方法比较熟悉,在回答的时候尽量能够说出多种解决方法更好! 字符串翻转的方法 1)使用字符串函数 ...
- VS code 自定义快捷输入
本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 位置 ctrl+shift+p 搜索: snippets 输入类型: 比如 ...
- React Native之React速学教程(中)
概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解 ...