poj 3414 Pots bfs+模拟
#include<iostream>
#include<cstring>
#define fillA 1
#define pourAB 2
#define dropA 3
#define fillB 4
#define pourBA 5
#define dropB 6 #define N 10000 using namespace std;
int vis[][];
class node
{
public:
int A, B;
int dir;
node()
{
A=;
B=;
}
}; int a, b, c, tot;
node q[N];
int pre[N]; void printPath(int index)
{
if(index==)
{
cout<<tot<<endl;
return ;
}
++tot;
printPath(pre[index]);
switch(q[index].dir)
{
case :
cout<<"FILL(1)";
break;
case :
cout<<"POUR(1,2)";
break;
case :
cout<<"DROP(1)";
break;
case :
cout<<"FILL(2)";
break;
case :
cout<<"POUR(2,1)";
break;
case :
cout<<"DROP(2)";
break;
}
cout<<endl;
} int bfs()
{
int head=, rear=, r;
node cur, next;
memset(vis, , sizeof(vis));
pre[]=;
vis[][]=;
while(head<rear)
{
cur=q[head];
if(cur.A==c || cur.B==c)
{
printPath(head);
return ;
}
next=cur;
if(!vis[a][cur.B])//将 A 装满
{
next.A=a;
next.dir=fillA;
pre[rear]=head;
vis[a][cur.B]=;
q[rear++]=next;
}
if(!vis[][cur.B])//将 A 倒掉
{
next.A=;
next.dir=dropA;
pre[rear]=head;
vis[][cur.B]=;
q[rear++]=next;
}
r=b-cur.B;
next.dir=pourAB;
if(r>cur.A && !vis[][cur.B+cur.A])//将A 倒向 B
{
next.A=;
next.B=cur.B+cur.A;
pre[rear]=head;
q[rear++]=next;
vis[][cur.B+cur.A]=;
}
else if(r<=cur.A && !vis[cur.A-r][cur.B+r])
{
next.A=cur.A-r;
next.B=cur.B+r;
pre[rear]=head;
q[rear++]=next;
vis[cur.A-r][cur.B+r]=;
} next=cur;//开始错在这里了, 忘记了从新给next赋值了
if(!vis[cur.A][b])//将 B 装满
{
next.B=b;
next.dir=fillB;
pre[rear]=head;
q[rear++]=next;
vis[cur.A][b]=;
}
if(!vis[cur.A][])//将 B 倒掉
{
next.B=;
next.dir=dropB;
pre[rear]=head;
q[rear++]=next;
vis[cur.A][]=;
}
r=a-cur.A;
next.dir=pourBA;
if(r>cur.B && !vis[cur.B+cur.A][])//将B 倒向 A
{
next.B=;
next.A=cur.B+cur.A;
pre[rear]=head;
q[rear++]=next;
vis[cur.B+cur.A][]=;
}
else if(r<=cur.B && !vis[cur.A+r][cur.B-r])
{
next.A=cur.A+r;
next.B=cur.B-r;
pre[rear]=head;
q[rear++]=next;
vis[cur.A+r][cur.B-r]=;
}
++head;
}
return ;
} int main()
{
while(cin>>a>>b>>c)
{
tot=;
if(c>a && c>b)
{
cout<<"impossible"<<endl;
continue;
}
if(!bfs())
cout<<"impossible"<<endl;
}
return ;
}
poj 3414 Pots bfs+模拟的更多相关文章
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- 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 You are given two pots, having the volume of A and B liters respectively. The following operati ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- C语言_第五章__实践(密码转换)
1. 要求 输入China 输出 Glmre #include <stdio.h> #include <stdlib.h> int main() { char c ; c ...
- 转载:Chrome调试折腾记_(1)调试控制中心快捷键详解!!!
转载:http://blog.csdn.net/crper/article/details/48098625 大多浏览器的调试功能的启用快捷键都一致…按下F12;还是熟悉的味道; 或者直接 Ctrl ...
- JavaScript数组去重的几种方法
这个老问题,网上普遍都有了,为什么要再写一遍呢,主要是为了记个笔记... 遍历时搜索结果数组 思路很明确,如下 新建一个数组存放结果 循环遍历原数组,对于每一个元素,在结果数组中搜索是否存在 若不存在 ...
- 用Node.js发送邮件
本文讲的是用Node.js通过一个开启smtp的已有的邮箱账号发送邮件,而不是如何创建一个邮件服务器 开启smtp服务 首先要去要使用的邮箱中设置开启smtp,才能正常发送邮件 这边以163邮箱为例 ...
- linux shell中不显示路径了,显示为-bash-4.1#的两种解决办法
出现这个问题的原因是因为没有配置.bash_profile的问题,或者是我们不小心清空或删除了.bash_profile文件. 办法一:修改 ~/.bash_profile文件 步骤如下: vim ~ ...
- php清理当前目录下的指定文件和空目录(源码),建议服务器端执行
<?php /** * @desc 解析当前目录并递归删除目录下的指定文件 * @author mengdj<mengdj@outlook.com> 2014.10.02 1530 ...
- JS中常遇到的浏览器兼容问题和解决方法
今天整理了一下浏览器对JS的兼容问题,希望能给你们带来帮助,我没想到的地方请留言给我,我再加上: 常遇到的关于浏览器的宽高问题: //以下均可console.log()实验 var winW=docu ...
- git学习笔记一
一.概念理解 1.理解工作区和暂存区以及版本库 工作区我理解就是我们创建的程序所在的文件夹,比如test文件夹.其中有个.git文件,这个就是版本库,其中版本库中有个区域叫暂存区或叫索引. 截自廖雪峰 ...
- jq仿淘宝放大镜插件
html部分 //小图 <div id="photoBox"> <img src="图片路径" width="400" h ...
- myeclipse tomcat内存溢出解决方法
Tomcat直接启动正常,通过myeclipse启动tomcat内存溢出.MyEclipse启动Tomcat无视catalina.bat中设置内存大小的问题.在 tomcat的catalina.bat ...