POJ 3414 Pots(bfs)
来源:互联网 发布:兄弟连nginx视频教程 编辑:程序博客网 时间:2024/06/05 16:15
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
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
Source
#include <iostream> #include <cstring>#include <cstdio> #include <algorithm> using namespace std; const int MAX = 110; int a, b, c, r, l, vis[MAX][MAX], step[MAX*MAX]; string opr[7] = {" ", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(2,1)", "POUR(1,2)"}; struct Info { int x; int y; int operation; int pre;//链表回溯找路径 }info[MAX*MAX]; void solve(int xx, int yy, int ss) { if (vis[xx][yy]) return ; vis[xx][yy] = 1; info[r].x = xx; info[r].y = yy; info[r].operation = ss; info[r].pre = l; r++; } void print() { int i, ans; ans = 0; while (l != 0){ step[ans++] = info[l].operation; l = info[l].pre; } cout << ans << endl; for (i = ans - 1; i >= 0; i--){ cout << opr[step[i]] << endl; } } void BFS() { info[0].x = 0; info[0].y = 0; vis[0][0] = 1; l = 0; r = 1; while (l != r){ if (info[l].y == c || info[l].x == c){ print(); return ; } int tmpx, tmpy; //fill(1) tmpx = a; tmpy = info[l].y; solve(tmpx, tmpy, 1); //fill(2) tmpx = info[l].x; tmpy = b; solve(tmpx, tmpy, 2); //drop(1); tmpx = 0; tmpy = info[l].y; solve(tmpx, tmpy, 3); //drop(2) tmpx = info[l].x; tmpy = 0; solve(tmpx, tmpy, 4); //fill(2, 1) tmpx = info[l].x + min(a - info[l].x, info[l].y); tmpy = info[l].y - min(a - info[l].x, info[l].y); solve(tmpx, tmpy, 5); //fill(1, 2) tmpx = info[l].x - min(b - info[l].y, info[l].x); tmpy = info[l].y + min(b - info[l].y, info[l].x); solve(tmpx, tmpy, 6); l++; } if (l >= r) cout << "impossible" << endl; } int main() { cin >> a >> b >> c; memset(vis, 0, sizeof(vis)); BFS(); system("pause"); }
- POJ 3414 Pots BFS
- POJ 3414 Pots(BFS)
- poj 3414 Pots bfs
- poj 3414 Pots BFS
- poj 3414 Pots BFS
- poj 3414 Pots (bfs)
- BFS-POJ-3414-Pots
- POJ 3414 Pots BFS
- POJ 3414-Pots(BFS)
- poj-3414 Pots BFS
- poj 3414 Pots bfs
- POJ 3414 POTS BFS
- poj 3414 (POTS) (BFS)
- POJ 3414 Pots (BFS)
- POJ - 3414 Pots(BFS)
- POJ 3414 Pots (BFS)
- 【Poj 3414】 Pots BFS
- POJ 3414 Pots bfs
- POJ 3176 Cow Bowling (数字三角形DP)
- java环境搭建
- Java_基础—统计字符串中每个字符出现的次数
- 最大连续子数组
- 搬砖中的小事之代码(八)--客户编码6位数的随机串的生成和登记时间的生成
- POJ 3414 Pots(bfs)
- 对话框最小尺寸的限制
- ubuntu 14.04 安装有道词典
- JAVA 面试题 输出100以内的所有素数
- WC命令详解
- Git忽略规则及.gitignore规则不生效的解决办法
- 面试题67:机器人的运动范围
- VM无法启动Ubuntu
- CSS3 选择器