POJ 3414 Pots【BFS水】
POJ 3414 Pots
大意:有两只桶,容量分别为a,b,对每只桶,有以下三种操作:
1.Fill(i)装满第i只桶
2.DROP(i)将桶i的水倒掉
3.POUR(i,j),将桶i中的水倒入桶j,若桶j满,剩余的水遗留于桶i中
问能否通过这些操作使得某只桶恰好装有容量为c的水?若能,输出最少操作次数及过程
PS:c<max(a,b);
算法:BFS即可
#include<string.h>
#include<queue>
usingnamespace std;
constint N =101;
struct pn
{
int prex;
int prey;
}pre[N][N];
struct node
{
int a,b;
int step;
booloperator<(const node &A)const
{
return A.step<step;
}
};
bool visited[N][N];
int fa,fb;
void DFS(int a,int b)
{
if(pre[a][b].prex+pre[a][b].prey!=0)
DFS(pre[a][b].prex,pre[a][b].prey);
int pa=pre[a][b].prex;
int pb=pre[a][b].prey;
//A清空
if(pa!=0&&a==0&&pb==b)
{
printf("DROP(1)\n");
return;
}
//装满A
if(pa!=fa&&a==fa&&pb==b)
{
printf("FILL(1)\n");
return;
}
//从A倒入B或B倒入A
if(pa+pb==a+b)
{
//A->B
if(a==0||b==fb)
{
printf("POUR(1,2)\n");
}
else
printf("POUR(2,1)\n");
return;
}
//装满B
if(pa==a&&b==fb)
{
printf("FILL(2)\n");
return;
}
printf("DROP(2)\n");
}
void BFS(int a,int b,int c)
{
fa=a;fb=b;
priority_queue<node>Q;
memset(visited,false,sizeof(visited));
node cur,next;
cur.a =0;
cur.b =0;
cur.step =0;
Q.push(cur);
while(!Q.empty())
{
cur=Q.top();
Q.pop();
if(visited[cur.a][cur.b]==true)continue;
visited[cur.a][cur.b]=true;
if(cur.a==c||cur.b==c)break;
next.step=cur.step+1;
//将A倒满
next.a=a;
next.b=cur.b;
if(visited[next.a][next.b]==false)
{
pre[next.a][next.b].prex=cur.a;
pre[next.a][next.b].prey=cur.b;
Q.push(next);
}
//将A清空
next.a =0;
next.b = cur.b;
if(visited[next.a][next.b]==false)
{
pre[next.a][next.b].prex=cur.a;
pre[next.a][next.b].prey=cur.b;
Q.push(next);
}
//将A倒入B
next.a =0;
next.b = cur.b+cur.a;
if(next.b>b)
{
next.a = next.b-b;
next.b=b;
}
if(visited[next.a][next.b]==false)
{
pre[next.a][next.b].prex=cur.a;
pre[next.a][next.b].prey=cur.b;
Q.push(next);
}
//将b清空
next.a=cur.a;
next.b =0;
if(visited[next.a][next.b]==false)
{
pre[next.a][next.b].prex=cur.a;
pre[next.a][next.b].prey=cur.b;
Q.push(next);
}
//将b装满
next.a=cur.a;
next.b=b;
if(visited[next.a][next.b]==false)
{
pre[next.a][next.b].prex=cur.a;
pre[next.a][next.b].prey=cur.b;
Q.push(next);
}
//将b倒入A
next.a=cur.a+cur.b;
next.b=0;
if(next.a>a)
{
next.b=next.a-a;
next.a=a;
}
if(visited[next.a][next.b]==false)
{
pre[next.a][next.b].prex=cur.a;
pre[next.a][next.b].prey=cur.b;
Q.push(next);
}
}
if(cur.a==c||cur.b==c)
{
printf("%d\n",cur.step);
DFS(cur.a,cur.b);
}
else
printf("impossible\n");
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
BFS(a,b,c);
}
return0;
}
转载于:https://www.cnblogs.com/AndreMouche/archive/2011/02/12/1952820.html
POJ 3414 Pots【BFS水】相关推荐
- poj 3414 Pots BFS
参考文章:思路公式 代码思路 原来的代码有点瑕疵,这是最新的AC代码 #pragma warning(disable:4996) #include<iostream> #include&l ...
- POJ 3414 Pots【BFS】+ Python
原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- POJ 3414 Pots(深搜并打印路径)
POJ 3414 Pots(深搜并打印路径) You are given two pots, having the volume of A and B liters respectively. The ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj ...
- Pots POJ - 3414(bfs)
You are given two pots, having the volume of A and B liters respectively. The following operations c ...
- POJ 3414 Pots
题目链接 Description You are given two pots, having the volume of A and B liters respectively. The follo ...
- POJ 3414 Pots【广搜】
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- H - Pots POJ - 3414(两个锅互相倒水)
H - Pots POJ - 3414 题意: (两个锅互相倒水)希望能通过题目中的几种操作使一个锅中的水量为目标水量 bfs 搜索每一步的每种操作,直到所有操作用完,则impossible,否则输出 ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
最新文章
- Linux之rpm包管理
- st 串口烧写工具 芯片_STM32芯片的几种烧写方式简介
- python从零基础到项目实战怎么样-Python 3.x网络爬虫从零基础到项目实战
- Python基础-XML模块
- 一,彻底理解第一个C语言程序 Hello World
- Spring Boot EasyUI datagrid
- Nginx报错:upstream timed out (110: Connection timed out)和client intended to send too large body【转】...
- Robot Framework连接MySQL数据库
- Sqlloader导数据进数据库时间过长或卡住原因探讨及解决办法
- java随机点名器_基于JavaScript实现随机点名器
- python查询12306余票_Python之12306余票查询
- cocos creator切换场景闪退_#Cocos Creator# 为什么音乐音效在场景切换的时候自动停止了?...
- 概率DP——BZOJ4008 [HNOI2015]亚瑟王
- 根轨迹图、Bode图、Nyquist图的Matlab仿真
- 初识:神经网络(Neural Networks)
- vc设备工程师_设备工程师考核.doc
- Linux分区命令-parted
- 基于PHP的旅游资讯管理系统
- pixhawk飞控板的硬件构成
- 汽车材料QC/T 942-2013 ELV中六价铬的检测
热门文章
- SqlServer自增长字段归零
- Redis 配置文件详解
- EsayExcel简单的读和写
- python from import 和 import 区别_python import和from import的区别
- python字典长度可变吗_为什么Python中字典的key必须是不可变的?
- 昆明职高计算机学校,昆明职高学校,昆明职高学校前十强,昆明职高学校哪些比较好一点 - IT教育频道...
- python制作gif动图_Python几行代码制作Gif动图
- 华为算法精英赛(题2:水仙花数判断)
- 使用File I/O类实现文件的读写操作
- opencv的Mat与Eigen的Matrix相互转换