bfs + 路径输出
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
题意 :
3 种操作,第一种将桶内装满,第二种将桶倒干净,第三种将一个桶内的倒入另一个桶。
思路 :
BFS做,每次将当前的状态打入队列,最后要输出路径,在结构体中存上一个点的位置。
代码 :
int a, b, c;
struct node
{
int x, y;
int pt;
int temp;
node *pre;
}arr[400]; int vis[105][105];
int ans;
stack<int>s;
void bfs(){
queue<node>que; // 每次将当前的状态打入队列,对于每个状态有 6 种处理方法
memset(vis, 0, sizeof(vis)); node v;
v.x = 0, v.y = 0;
v.pt = 0;
v.temp = 0;
v.pre = NULL;
while(!que.empty()) que.pop();
que.push(v);
int cnt = -1;
vis[0][0] = 1; while(!que.empty()){
v = que.front();
que.pop();
cnt++;
arr[cnt] = v; for(int i = 1; i <= 6; i++){
switch(i){
case 1:
v.x = a;
v.y = arr[cnt].y;
v.pt = 1;
break;
case 2:
v.x = arr[cnt].x;
v.y = b;
v.pt = 2;
break;
case 3:
v.x = 0;
v.y = arr[cnt].y;
v.pt = 3;
break;
case 4:
v.x = arr[cnt].x;
v.y = 0;
v.pt = 4;
break;
case 5:
if (arr[cnt].x > (b - arr[cnt].y)){
v.x = arr[cnt].x - (b - arr[cnt].y);
v.y = b;
}
else {
v.x = 0;
v.y = arr[cnt].x + arr[cnt].y;
}
v.pt = 5;
break;
case 6:
if (arr[cnt].y > (a - arr[cnt].x)){
v.x = a;
v.y = arr[cnt].y - (a - arr[cnt].x);
}
else {
v.x = arr[cnt].x + arr[cnt].y;
v.y = 0;
}
v.pt = 6;
}
if (vis[v.x][v.y]) continue;
vis[v.x][v.y] = 1;
v.temp = arr[cnt].temp + 1;
v.pre = &arr[cnt];
if (v.x == c || v.y == c){ while(!s.empty()) s.pop();
ans = v.temp;
while(v.pre){
s.push(v.pt);
v = *v.pre;
}
return;
}
que.push(v);
}
}
} void print(){
while(!s.empty()){
int f = s.top();
s.pop();
switch(f){
case 1: printf("FILL(1)\n");break;
case 2: printf("FILL(2)\n");break;
case 3: printf("DROP(1)\n");break;
case 4: printf("DROP(2)\n");break;
case 5: printf("POUR(1,2)\n");break;
case 6: printf("POUR(2,1)\n");break;
}
}
} int main() { while(~scanf("%d%d%d", &a, &b, &c)){
ans = 0;
bfs(); if (ans == 0) printf("impossible\n");
else {
printf("%d\n", ans);
print();
}
}
return 0;
}
bfs + 路径输出的更多相关文章
- [POJ] 1606 Jugs(BFS+路径输出)
题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...
- POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- Pots(POJ - 3414)【BFS 寻找最短路+路径输出】
Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...
- Poj3984 迷宫问题 (BFS + 路径还原)
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- POJ-3894 迷宫问题 (BFS+路径还原)
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- 洛谷 P2764 最小路径覆盖问题【最大流+拆点+路径输出】
题目链接:https://www.luogu.org/problemnew/show/P2764 题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V ...
- java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用
java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用 import java.io.BufferedReader; import java.io.BufferedWrite ...
- Floyd最短路(带路径输出)
摘要(以下内容来自百度) Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似. 该算法名称以创始人之一.1978年图灵奖获得者. ...
随机推荐
- JDBC 时间处理
Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...
- 关于后台动态模板添加内容的总结 Builder使用
1.后台控制器中ArticleController中加载 formBuilder,listBuilder类: 2.实例化FormBuilder对象,调用类中的方法: 3.初始化时加载Builder/f ...
- 2019-1-29-WPF-设置输入只能英文
title author date CreateTime categories WPF 设置输入只能英文 lindexi 2019-1-29 15:8:4 +0800 2018-2-13 17:23: ...
- 【codeforces 749B】Parallelogram is Back
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- C# 标准性能测试高级用法
本文告诉大家如何在项目使用性能测试测试自己写的方法 在 C# 标准性能测试 已经告诉大家如何使用 BenchmarkDotNet 测试性能,本文会告诉大家高级的用法. 建议是创建一个控制台项目用来做性 ...
- 文本框(代替input)输入长度限制、提示
<div class="inform_content_text"> <textarea name="name" placeholder=&qu ...
- CSS 高度居中方案
实现高度自适应并且上下居中 <div id="wrap"> <div class="box">DemoSeat</div> ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- cocos2dx Quaternion 四元数(1/2)
这篇文章只是我学完四元数之后的一些理解,其实是对别人理解的理解,有些地方我理解但是没有写下来,如果真的想深入的学习四元数,建议从学习复数开始. 这个知识点需要几何想象的天赋和学习的耐心,缺一不可,慢慢 ...
- VC windows 多网卡情况下 获取当前网卡ip地址
参考 代码如下 记录下以后用得到或者能帮到有需要的朋友 #include <iostream> #include <WinSock2.h> #include <Iphlp ...