POJ - 3414 Pots(15.10.10 搜索专题)bfs
2015-10-11 22:24
726 查看
Pots
Submit Status
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 potj).
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
Sample Output
思路:简单题,倒水瓶游戏,挺好玩的题目,直接模拟操作然后bfs就好了。
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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 potj).
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就好了。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<string> #include<stack> using namespace std; const string tip[8] = { "", "DROP(1)", "DROP(2)", "FILL(1)", "FILL(2)", "POUR(1,2)", "POUR(2,1)" }; int A, B, C; struct node { int inA, inB; int step; node(int a, int b, int s) { inA = a; inB = b; step = s; } }; struct Op { int opnum; int prea,preb; Op() { prea = -1; preb = -1;} }op[105][105]; int lastA, lastB; bool vis[105][105]; void pourAtoB(int &a, int &b) { int yul = B - b; if (a <= yul) { b += a; a = 0; } else { b = B; a = a - yul; } } void pourBtoA(int &a, int &b) { int yul = A - a; if (b <= yul) { a += b; b = 0; } else { a = A; b = b - yul; } } int bfs() { queue<node>que; que.push(node(0, 0, 0)); vis[0][0] = true; while (!que.empty()) { node loc = que.front(); que.pop(); if (loc.inA == C || loc.inB==C) { lastA = loc.inA, lastB = loc.inB; return loc.step; } for (int i = 1; i <= 6; i++) { if (i == 1) //drop A { int nA = 0; int nB = loc.inB; if (!vis[nA][nB]) { que.push(node(nA, nB, loc.step + 1)); vis[nA][nB] = true; op[nA][nB].prea = loc.inA; op[nA][nB].preb = loc.inB; op[nA][nB].opnum = 1; } } else if (i == 2) //drop B { int nA = loc.inA; int nB = 0; if (!vis[nA][nB]) { que.push(node(nA, nB, loc.step + 1)); vis[nA][nB] = true; op[nA][nB].prea = loc.inA; op[nA][nB].preb = loc.inB; op[nA][nB].opnum = 2; } } else if (i == 3) //fill A { int nA = A; int nB = loc.inB; if (!vis[nA][nB]) { que.push(node(nA, nB, loc.step + 1)); vis[nA][nB] = true; op[nA][nB].prea = loc.inA; op[nA][nB].preb = loc.inB; op[nA][nB].opnum = 3; } } else if (i == 4) //fill B { int nA = loc.inA; int nB = B; if (!vis[nA][nB]) { que.push(node(nA, nB, loc.step + 1)); vis[nA][nB] = true; op[nA][nB].prea = loc.inA; op[nA][nB].preb = loc.inB; op[nA][nB].opnum = 4; } } else if (i == 5) //pour A to B { int nA = loc.inA; int nB = loc.inB; pourAtoB(nA, nB); if (!vis[nA][nB]) { que.push(node(nA, nB, loc.step + 1)); vis[nA][nB] = true; op[nA][nB].prea = loc.inA; op[nA][nB].preb = loc.inB; op[nA][nB].opnum = 5; } } else if (i == 6) //pour B to A { int nA = loc.inA; int nB = loc.inB; pourBtoA(nA, nB); if (!vis[nA][nB]) { que.push(node(nA, nB, loc.step + 1)); vis[nA][nB] = true; op[nA][nB].prea = loc.inA; op[nA][nB].preb = loc.inB; op[nA][nB].opnum = 6; } } } } return -1; } int main() { while (scanf("%d%d%d", &A, &B, &C) != EOF) { memset(vis, false, sizeof(vis)); for (int i = 0; i <= 100;i++) for (int j = 0; j <= 100; j++) op[i][j].prea = op[i][j].preb = -1; int ans = bfs(); if (ans == -1) { cout << "impossible" << endl; continue; } cout << ans << endl; stack<int>ansop; ansop.push(op[lastA][lastB].opnum); while (1) { int ta = op[lastA][lastB].prea; int tb = op[lastA][lastB].preb; if (op[ta][tb].prea == -1) break; ansop.push(op[ta][tb].opnum); lastA = ta; lastB = tb; } int c = 1; while (!ansop.empty()) { cout << tip[ansop.top()] << endl; ansop.pop(); } } }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- JavaScript中数组的排序、乱序和搜索实现代码
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法
- 基于ASP.NET的lucene.net全文搜索实现步骤
- 做个自己站内搜索引擎
- PHP查找与搜索数组元素方法总结