POJ3414 Pots( BFS搜索)
题目:
给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作
FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2);
DROP(i) 将第i个容器抽干
POUR(i,j) 将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)
现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程
输入:
有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))
输出:
第一行包含一个数表示最小操作数K
随后K行每行给出一次具体操作,如果有多种答案符合最小操作数,输出他们中的任意一种操作过程,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”
分析:
简单的BFS,难点在于回溯,给每个状态用数组记录路径
代码
#include<iostream>
#include<queue>
#include<string>
using namespace std;
const int INF = 0x3f3f3f3f;
int a, b, c;
int used[105][105];
struct node
{
int x, y;
int flag;
int path[1005];//数组中0-5分别表示6种不同操作
}st;
string print[6] = { "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)" };
void bfs()
{
queue<node> q;
for (int i = 0; i <= a; ++i)
{
for (int j = 0; j <= b; ++j)
used[i][j] = INF;
}
memset(used, 0, sizeof(used));
st.x = 0, st.y = 0;
st.flag = 0;
memset(st.path, -1, sizeof(st.path));
q.push(st);
used[st.x][st.y] = 1;
while (q.size())
{
node temp = q.front();
q.pop();
if (temp.x == c || temp.y == c)
{
cout << temp.flag << endl;
for (int i = 0; i < temp.flag; ++i)
cout << print[temp.path[i]] << endl;
return;
}
for (int i = 0; i < 6; ++i)
{
node now = temp;
now.flag++;
if (i == 0 && now.x != a)
{
now.x = a;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 0;
q.push(now);
}
}
else if (i == 1 && now.y != b)
{
now.y = b;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 1;
q.push(now);
}
}
else if (i == 2 && now.x != 0)
{
now.x = 0;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 2;
q.push(now);
}
}
else if (i == 3 && now.y != 0)
{
now.y = 0;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 3;
q.push(now);
}
}
else if (i == 4)
{
if (now.x + now.y > b)
{
now.x -= b - now.y;
now.y = b;
}
else
{
now.y += now.x;
now.x = 0;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 4;
q.push(now);
}
}
else if (i == 5)
{
if (now.x + now.y > a)
{
now.y -= a - now.x;
now.x = a;
}
else
{
now.x += now.y;
now.y = 0;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 5;
q.push(now);
}
}
}
}
cout << "impossible" << endl;
}
int main()
{
cin >> a >> b >> c;
bfs();
return 0;
}
POJ3414 Pots( BFS搜索)的更多相关文章
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ-3414.Pots.(BFS + 路径打印)
这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...
- POJ3414 Pots BFS搜素
题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hiho_1139_二分+bfs搜索
题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分 最小化最大值,考虑采用二分搜索.对所有的边长进 ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- BFS搜索
参考博客:[算法入门]广度/宽度优先搜索(BFS) 适用问题:一个解/最优解 重点:我们怎么运用队列?怎么记录路径? 假设我们要找寻一条从V0到V6的最短路径.(明显看出这条最短路径就是V0-> ...
- Horse Pro(带负坐标的bfs搜索)
Horse Pro bfs搜索,但图中存在负值坐标,两种方法解决. 用数组标记,将原点设为300,300 用map标记 http://oj.jxust.edu.cn/contest/Problem?i ...
随机推荐
- [编程]UML语言:理论之光与实践之惑
UML介绍及现状 UML(统一建模语言)是软件工程领域中具有悠久历史的一种模型化语言工具.它通过标准化的图形符号体系,使得软件系统的蓝图能够被更直观地表达出来.UML诞生于20世纪90年代,经过多年积 ...
- 解决IDEA加载maven工程缓慢
如图,哪里没有加哪里 -DarchetypeCatalog=internal
- [gym104542F] Interesting String Problem
Since you are a good friend of Jaber and Eyad, they are asking for your help to solve this problem. ...
- [USACO2007NOVS] Cow Hurdles S
题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...
- eclipse的问题
在我使用eclipse的时候出现的一个问题 就是因为汉化之后出现的问题 问题的解决办法 点击帮助 关于 安装细节 选中Babel Language Pack for rt.rap in Chinese ...
- MySQL运维5-Mycat配置
一.schema.xml 1.1 简介 schema.xml作为Mycat中最重要的配置文件之一,涵盖了Mycat的逻辑库.逻辑表.分片规则.分片节点即数据源的配置.主要包括一下三组标签 schema ...
- Harbor安装和镜像推送
安装前提: yum -y install docker-compose-plugin 安装: tar -zxvf harbor-offline-installer-v2.6.1.tgz cd harb ...
- 从零玩转设计模式之建造者模式-jianzaozhemoshi
title: 从零玩转设计模式之建造者模式 date: 2022-12-08 18:15:30.898 updated: 2022-12-23 15:35:58.428 url: https://ww ...
- aspnetcore使用websocket实时更新商品信息
先演示一下效果,再展示代码逻辑. 中间几次调用过程省略... 暂时只用到了下面四个项目 1.产品展示页面中第一次通过接口去获取数据库的列表数据 /// <summary> /// 获取指定 ...
- 关于helloworld
我们的helloworld是从一个源程序开始的,该源程序由程序员通过编译器创建并保存的文件,文件名就是hello.c.这个hello.c的源程序,实际上是有0和1组成的序列.每一个0和1都成为一位,这 ...