题目链接

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 ;
}

 相似题目

1、poj1606 Jugs

poj3414 Pots(BFS)的更多相关文章

  1. poj3414 Pots (BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special J ...

  2. POJ-3414 Pots (BFS)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  4. Pots(BFS)

    Pots Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submiss ...

  5. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  6. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. Nginx修改access.log日志时间格式

    一.修改原因 因为要获取nginx访问信息,作为开发的数据使用,但是nginx的access.log文件中的默认的时间格式是这样的: [02/Nov/2017:20:48:25 +0800] 而要求的 ...

  2. docker操作mysql

    Docker操作mysql 查找docker hub上的mysql镜像 Docker search.mysql 拉取官方的镜像标签为5.6 Docker pull mysql:5.6 在本地镜像列表里 ...

  3. js绑定事件和解绑事件

    在js中绑定多个事件用到的是两个方法:attachEvent和addEventListener,但是这两个方法又存在差异性 attachEvent方法  只支持IE678,不兼容其他浏览器 addEv ...

  4. 说说ASP.NET的IsPostBack

    学习asp.net,就不能不谈IsPostBack属性,顾名思义,这是是否回发的意思,多用于判断是不是第一次打开 一.IsPostBack介绍 Page.IsPostBack是一个标志:当前请求是否第 ...

  5. HDU 2191 珍惜现在,感恩生活 (dp)

    题目链接 Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都 ...

  6. HDU 2593 Pirates’ Code (STL容器)

    题目链接 Problem Description Davy Jones has captured another ship and is smiling contently under the sun ...

  7. three.js_camera相机

    https://blog.csdn.net/yangnianbing110/article/details/51275927 文章地址

  8. Linux下文件目录权限和对应命令的总结

    Linux下的权限有rwx三种,分别对应读,写,执行三种,在对文件和目录时,分别是下列含义: 对应权限的命令为: 文件: r-- cat, more, head, tail w-- echo, vi ...

  9. 【Windows使用笔记】神舟笔记本的control center

    首先,神船大法好. 然后,因为我的船风扇声音有点大啊,在实验室感觉就很吵,但是它的背板温度又不是很高,所以想设置下风扇的启动. 所以需要用到神船自带的control center软件. 长这样. 应该 ...

  10. Xcode 9安装

    Xcode 9 Xcode 9 Compatibility Xcode 9 requires a Mac running macOS 10.13.2 or later. Xcode 9 include ...