poj3414 倒水并输出过程
2017-02-07 16:08
239 查看
Description
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)
思路:bfs爆搜。首先确定状态:两个桶中水的体积,采取行动的次数,当前状态是由6种操作中那种得来的。
关键:
1.实现6种操作。
2.要用vis记录数组,否则最后的结果不一定是采取行动次数最少。
3.如何回溯输出路径。(借助二维结构体)
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)
思路:bfs爆搜。首先确定状态:两个桶中水的体积,采取行动的次数,当前状态是由6种操作中那种得来的。
关键:
1.实现6种操作。
2.要用vis记录数组,否则最后的结果不一定是采取行动次数最少。
3.如何回溯输出路径。(借助二维结构体)
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define N 105 using namespace std; typedef struct note { int v1, v2;//桶1,2中的水的体积 int step; int type;//1到6分别表示FILL(1),FILL(2),DROP(1),DROP(2),POUR(1,2),POUR(2,1) }cup; cup pre ;//二维结构体记录路径 cup cur, tmp;//用来记录队列,队首元素和下一个可能的元素 int a, b, c;//输入 int vis ;//两维分别表示,两个桶的状态 void choose(int i) { int sum; switch (i) { case 1: tmp.v1 = a; break; case 2: tmp.v2 = b; break; case 3: tmp.v1 = 0; break; case 4: tmp.v2 = 0; break; case 5: sum = tmp.v1 + tmp.v2; if (sum <= b)//倒完 { tmp.v1 = 0; tmp.v2 = sum; } else { tmp.v1 -= b - tmp.v2; tmp.v2 = b; } break; case 6: sum = tmp.v1 + tmp.v2; if (sum <= a)//倒完 { tmp.v1 = sum; tmp.v2 = 0; } else { tmp.v2 -= a - tmp.v1; tmp.v1 = a; } break; } } void print(cup t)//回溯输出路径 { if (pre[t.v1][t.v2].v1 != 0 || pre[t.v1][t.v2].v2 != 0) print(pre[t.v1][t.v2]); switch (t.type) { 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; default : break; } } int bfs() { //初始化队列 queue<cup> q; while (q.size()) q.pop(); //初始化初状态并入队 cup t; t.v1 = 0; t.v2 = 0; t.step = 0;// t.type = -1; q.push(t); vis[0][0] = 1; pre[0][0].v1 = 0; pre[0][0].v2 = 0;// pre[0][0].type = -1; while (q.size()) { cur = q.front(); q.pop(); if (cur.v1 == c || cur.v2 == c)//两桶中有一个水的体积达到c { printf("%d\n", cur.step); return 1; } for (int i = 1; i <= 6; i++)//共6种情况 { tmp = cur; choose(i);//采取行动 if (!vis[tmp.v1][tmp.v2]) { tmp.step++; tmp.type = i; q.push(tmp); vis[tmp.v1][tmp.v2] = 1; pre[tmp.v1][tmp.v2] = cur;//记录路径 } } } return 0; } int main() { int ans; while (~scanf("%d%d%d", &a, &b, &c)) { memset(vis, 0, sizeof(vis)); ans = bfs(); if (ans) print(cur); else printf("impossible\n"); } return 0; }
相关文章推荐
- poj 3414 Pots [bfs - 倒水问题]
- POJ 3414 Pots【bfs模拟倒水问题】
- POJ - 3414 Pots解题报告(输出路径的bfs)
- poj 3414 路径输出的bfs(pre)
- poj3414 倒水问题
- POJ 3414 Pots(BFS倒水问题)
- POJ-3414-BFS输出路径
- POJ 3414 - Easy BFS - 六入口倒水问题
- 广搜+输出路径 POJ 3414 Pots
- POJ3414 Pots【倒水问题+BFS】
- POJ 3414 Pots(bfs + 判重 + 输出路径)
- poj 3414 pots (bfs 倒水问题)
- POJ 3414 Pots【bfs模拟倒水问题】
- POJ 3414-Pots(BFS-模拟倒水)
- POJ 3414 dfs倒水问题
- POJ 3414 Pots【bfs模拟倒水问题】
- POJ-3414-两个杯子倒水问题(宽搜+回溯)
- POJ 3414 Pots(BFS 倒水)
- POJ 3414 Pots——bfs搜索+dfs输出
- poj 3414 Pots(BFS+路径输出)