POJ - 3414 Pots(15.10.10 搜索专题)bfs
来源:互联网 发布:zip解压软件for mac 编辑:程序博客网 时间:2024/06/07 06:04
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
6FILL(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();}}}
- POJ - 3414 Pots(15.10.10 搜索专题)bfs
- POJ 3414 Pots (BFS搜索题)
- POJ 3414 Pots(BFS)||SDUT(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 - 2251 Dungeon Master(15.10.10 搜索专题)bfs
- POJ - 3126 Prime Path(15.10.10 搜索专题)bfs
- POJ 3414 Pots BFS
- POJ 3414 Pots(BFS)
- poj 3414 Pots bfs
- poj 3414 Pots BFS
- kerberos认证全程
- html5学习之侧边栏实现
- 一切交由明天分晓
- OpenCV参考手册之Mat类详解(二)
- 【2015.10.11】bat批处理空格问题
- POJ - 3414 Pots(15.10.10 搜索专题)bfs
- 第四周项目2 建立单链表算法库
- OpenCV参考手册之Mat类详解(三)
- 经典联合查询 ,以表ipdateaex为主,如果表swbase有和他相同的数据,就提取
- spring注解@service("service")括号中的service有什么用
- 凸边形最优三角剖分
- 关于计算机图形学的学习
- 【WinForm窗体控件开发】之四 DesignerSerializationVisibilityAttribute 控件属性的串行化
- 32位保护模式内存寻址原理