POJ 3414 Pots 暴力,bfs 难度:1
http://poj.org/problem?id=3414
记录瓶子状态,广度优先搜索即可
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=101;
int n,m;
typedef unsigned long long ull;
int A,B,C;
int vis[maxn][maxn];
int ans[maxn][maxn][maxn*maxn];
struct node{
int a,b;
node (int ta,int tb):a(ta),b(tb){}
};
void printop(int op){
switch(op){
case 0:
puts("FILL(1)");
break;
case 1:
puts("FILL(2)");
break;
case 2:
puts("POUR(1,2)");
break;
case 3:
puts("POUR(2,1)");
break;
case 4:
puts("DROP(1)");
break;
case 5:
puts("DROP(2)");
break;
}
}
void op(int &a,int &b,int op){
switch(op){
case 0:
a=A;
break;
case 1:
b=B;
break;
case 2:
if(b+a<=B){
b+=a;
a=0;
}
else {
a-=B-b;
b=B;
}
break;
case 3:
if(b+a<=A){
a+=b;
b=0;
}
else {
b-=A-a;
a=A;
}
break;
case 4:
a=0;
break;
case 5:
b=0;
break;
}
}
void bfs(){
queue <node> que;
que.push(node(0,0));
vis[0][0]=0;
while(!que.empty()){
node tp=que.front();que.pop();
int ta=tp.a;
int tb=tp.b;
if(tp.a==C||tp.b==C){
printf("%d\n",vis[tp.a][tp.b]);
for(int i=0;i<vis[ta][tb];i++){
int op=ans[ta][tb][i];
printop(op);
}
return ;
}
for(int i=0;i<6;i++){
int ta=tp.a;
int tb=tp.b;
op(ta,tb,i);
if(vis[ta][tb]==-1){
vis[ta][tb]=vis[tp.a][tp.b]+1;
for(int j=0;j<vis[tp.a][tp.b];j++){
ans[ta][tb][j]=ans[tp.a][tp.b][j];
}
ans[ta][tb][vis[tp.a][tp.b]]=i;
que.push(node(ta,tb));
}
}
}
puts("impossible");
}
int main(){
scanf("%d%d%d",&A,&B,&C);
memset(vis,-1,sizeof(vis));
bfs();
return 0;
}
POJ 3414 Pots 暴力,bfs 难度:1的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ - 3414 Pots 【BFS】
题目链接 http://poj.org/problem?id=3414 题意 给出两个杯子 容量分别为 A B 然后给出C 是目标容量 有三种操作 1 将一个杯子装满 2.将一个杯子全都倒掉 3.将一 ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- (简单) 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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
随机推荐
- centos 搭建php环境
安装Apache1.安装yum -y install httpd2.开启apache服务systemctl start httpd.service3.设置apache服务开机启动systemctl e ...
- Http协议中Cookie详细介绍(转)
原文:http://www.169it.com/article/3217120921.html Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存C ...
- hdu1286(找新朋友)&&POJ2407Relatives(欧拉函数模版题)
http://acm.hdu.edu.cn/showproblem.php?pid=1286 没什么好说的,模板题,主要是弄懂欧拉函数的思想. #include <iostream> #i ...
- ZW团队:IN_OUT传播模型简介
传统媒体,网络媒体的整合推广,我曾经提出过一个:Tn-Out模式 In-Out是NBA的篮球术语,你自己百度下 传统媒体承担"IN"的角色,负责传播的深度和建立公信力 网络媒体充当 ...
- 20145202马超 2016-2017-2 《Java程序设计》第8周学习总结
20145202马超 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 NIO使用频道(channel)来衔接数据节点,对数据区的标 ...
- RPC和REST
什么是RPC? 是指远程过程调用,就是两个服务A.B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网络来表达调用的语义和传达调用的数据 ...
- openwrt编译系统生成ubi镜像的各变量解析
1.MKUBIFS_OPTS的作用 传递参数给mkfs.ubifs 2.MKUBIFS_OPTS传递了哪些参数? 传递了最小输入输出单元大小.逻辑擦除块大小.最大物理擦除块的个数,分别由选项-m.-e ...
- Asynchronous Programming Patterns
Asynchronous Programming Patterns The .NET Framework provides three patterns for performing asynchro ...
- SDN前瞻 软件定义网络的一些概念
SDN的核心:可编程性 SDN的思想:SOA面向服务 面向服务的体系结构(service-oriented architecture SOA) 使网络连接的大量计算机易于合作,以 服务 而不是人工交互 ...
- Binary Tree Zigzag Level Order Traversal,z字形遍历二叉树,得到每层访问的节点值。
问题描述: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from l ...