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年图灵奖获得者. ...
随机推荐
- 2018-8-10-WPF-可获得焦点属性
title author date CreateTime categories WPF 可获得焦点属性 lindexi 2018-08-10 19:16:53 +0800 2018-05-15 15: ...
- Vue基础练习之Todo List
<body> <div id = "app"> <input type="text" v-model="inputVal ...
- POJ 3311 Hie with the Pie 兼 Codevs 2800 送外卖(动态规划->TSP问题)
Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possi ...
- Loj2604开车旅行
Loj2604开车旅行 我完全没有看出这道题哪里是DP 首先,一个位置向后的最近和第二近我们可以通过set去简单实现 通过维护最大和次大即可 至于高度相同的情况我们可以通过先在set中查询小的来实现 ...
- git 上传当前分支
因为我现在的分支是的名很长,每次需要上次当前分支需要写很多代码,是不是有很简单方法上传当前分支. 如果要上传一个分支到仓库 origin 那么就需要使用下面的命令 git push origin 分支 ...
- 一个APP从启动到主页面显示经历了哪些过程?
①点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求: ②system_server进程接收到请求后,向zygote进程 ...
- Visual Studio Team Services使用教程【5】:Readers tfs组成员添加
2017.4.23之后建议朋友看下面的帖子 TFS2017 & VSTS 实战(繁体中文视频) Visual Studio Team Services(VSTS)与敏捷开发ALM实战关键报告( ...
- Maven工程
maven:父子工程 1,父工程并没有实质性的内容,所有的jar包都在里面,也就是说只需要管理jar包即可,不参与任务方法逻辑 2,在父工程中拥有很多的子模块,每一个子模块都代表了不用的包如(pack ...
- python 可变参数示例
练习1:请使用**kw的方式,把可变的所有参数, # 算一字典的value乘积 def fun(**kw): product =1 for k ,v in kw.items(): if isinsta ...
- Elasticsearch慢查询故障诊断
最近在做ES搜索调优,看了一些lucene搜索的文档和代码,本文用于总结调优过程中学到的知识和自己的思考. 在抓到ES慢查询之后,会通过profile或者kibana的Search Profiler ...