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 ...
随机推荐
- Android自定义折线图
老师布置了个作业:http://www.cnblogs.com/qingxu/p/5316897.html 作业中提到的 “玩了几天以后,大家发现了一些很有意思的现象,比如黄金点在逐渐地往下移动.” ...
- dvb标准
一.概念 DVB, 数字视频广播Digital Video Broadcasting的缩写, 是由DVB项目维护的一系列国际承认的数字电视公开标准.(欧标)二.分类DVB系统传输方式有如下几种: 卫星 ...
- eclipse的SVN插件去除无效的文件
- 【SQL语句】update ... ... from ......
要求:修改vaj表中的vaj02字段的值,vaj02字段的值=cag.cag03的值,vaj 表与 cag 表无直接关联 实现: update vaj set vaj02=c.cag03 from l ...
- GOOD MEETINGS CREATE SHARED UNDERSTANDING, NOT BRDS!
Deliverables and artifacts were a focal point of BA work during the early part of my career. If I ...
- iOS开发之记录用户登录状态
iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...
- Python实战 :2017国考职业表excel转数据库,再查询生成excel
最近看2017年国考的职业表,多而杂,不好过滤我想要的信息,特此把它转成Sqlite3数据库,再从数据库里把查询结果导出成excel,方便找职业. (后附上整套代码) 环境:python2.7 x ...
- linux开启FTP以及添加用户配置权限,只允许访问自身目录,不能跳转根目录
1.切换到root用户 2.查看是否安装vsftp,我这个是已经安装的. [root@localhost vsftpd]# rpm -qa |grep vsftpd vsftpd--.el7_2.x8 ...
- 新手 gulp+ seajs 小demo
首先,不说废话,它的介绍和作者就不在多说了,网上一百度一大堆: 我在这里只是来写写我这2天抽空对seajs的了解并爬过的坑,和实现的一个小demo(纯属为了实现,高手请绕道); 一.环境工具及安装 1 ...
- Ubuntu 14.04--php的安装和配置
更新源列表 打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入root用户的密码"--& ...