poj3414 Pots(BFS)
题目链接
http://poj.org/problem?id=3414
题意
有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其中的一个杯子里的水恰为C升,输出最少步数和操作;如果不能倒到C升,输出“impossible”。
思路
这题与poj1606基本相同,在poj1606的基础上添加了输出最少步数,修改了操作的表示,以及不可能达到目标时输出impossible。将poj1606的代码略作修改即可。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std; struct Node
{
int a, b;
int steps;
int flag;
Node* pre;
}; const int N = ;
int ca, cb, n;
int visit[N][N];
stack<int> s; void print()
{
while (!s.empty())
{
switch (s.top())
{
case :
cout << "FILL(1)" << endl;
break;
case :
cout << "FILL(2)" << endl;
break;
case :
cout << "DROP(1)" << endl;
break;
case :
cout << "DROP(2)" << endl;
break;
case :
cout << "POUR(1,2)" << endl;
break;
case :
cout << "POUR(2,1)" << endl;
break;
}
s.pop();
}
} void bfs(int a, int b)
{
Node state[N];
int cnt = -;
memset(visit, , sizeof(visit));
Node node;
node.a = node.b = ;
node.steps = ;
node.pre = NULL;
queue<Node> q;
q.push(node);
visit[node.a][node.b] = ;
while (!q.empty())
{
Node node = q.front();
q.pop();
state[++cnt] = node;
Node next = node;
for (int i = ; i < ; i++)
{
next = node;
int amount;
switch (i)
{
case : //FILL(1)
next.a = ca;
next.flag = ;
break;
case : //FILL(2)
next.b = cb;
next.flag = ;
break;
case : // DROP(1)
next.a = ;
next.flag = ;
break;
case : //DROP(2)
next.b = ;
next.flag = ;
break;
case : //POUR(1,2)
amount = cb - node.b;
if (node.a > amount)
{
next.a -= amount;
next.b = cb;
}
else {
next.a = ;
next.b = node.a + node.b;
}
next.flag = ;
break;
case : //POUR(2,1)
amount = ca - node.a;
if (node.b > amount)
{
next.a = ca;
next.b -= amount;
}
else {
next.a = node.a + node.b;
next.b = ;
}
next.flag = ;
break;
} if (!visit[next.a][next.b])
{
visit[next.a][next.b] = ;
next.pre = &state[cnt];
next.steps = node.steps + ;
if (next.a == n || next.b == n)
{
cout << next.steps << endl;
while (next.pre)
{
s.push(next.flag);
next = *next.pre;
}
print();
return;
}
q.push(next);
}
}
}
cout << "impossible" << endl;
} int main()
{
cin >> ca >> cb >> n;
bfs(, );
return ;
}
相似题目
poj3414 Pots(BFS)的更多相关文章
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 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 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- Pots(BFS)
Pots Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submiss ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- PHP函数方法
补充一个P可以HP的特点函数:动态调用 function t(){ echo "welcome"; } function t2(){ echo "beatch" ...
- JAVA多线程提高十三:同步集合类的应用
1.引言 在多线程的环境中,如果想要使用容器类,就需要注意所使用的容器类是否是线程安全的.在最早开始,人们一般都在使用同步容器(Vector,HashTable),其基本的原理,就是针对容器的每一个操 ...
- bootstrap模态框 内部input无法手动获取焦点
//重写enforceFocus方法$(document).ready(function(){ $.fn.modal.Constructor.prototype.enforceFocus = func ...
- GridControl详解(七)事件
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventA ...
- 详解ASP.NET4 GridView的四种排序样式
与ASP.NET 的其他Web控件一能够,Gridview控件拥有很多不同的CSS样式属性设置,包括象CssClass,Font字体,ForeColor,BackColor,BackColor, Wi ...
- 数据库-Core Data
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong 数据库-Core Data 技术博客htt ...
- 【洛谷 P2553】 [AHOI2001]多项式乘法(FFT)
题目链接 简单处理一下输入,\(fft\)模板题. #include <cstdio> #include <cmath> #include <algorithm> ...
- 【leetcode 简单】第十三题 最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...
- 2017ACM暑期多校联合训练 - Team 6 1008 HDU 6103 Kirinriki (模拟 尺取法)
题目链接 Problem Description We define the distance of two strings A and B with same length n is disA,B= ...
- Big O
评价一个计算机算法的效率时用到的方法我们称之为Big O(Order of [大约是]). 有序二分查找算法: 比如我们通过二分查找算法查找容器里的10个数据. 那么,我们需要查找一个数据时正常应该是 ...