POJ 3414 Pots 均分水(bfs)
2016-04-13 14:49
447 查看
题目链接:
POJ 3414 Pots题目大意:
有两个水杯,一开始都没有装水,给出两个水杯的容量,和要达到的目标容量每个水被有三种操作:1.装满水,2.倒掉所有水,3,将水倒入另一个杯子中.
思路:
两个杯子,六种操作,,求最短路,并打印路径,6入口bfs,跟非常可乐一题类似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 4Sample Output
6FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
代码:
/************************************************************************* > File Name: poj_3414.cpp > Author: dulun > Mail: dulun@xiyoulinux.org > Created Time: 2016年04月13日 星期三 12时59分54秒 ************************************************************************/ #include<iostream> #include<stdio.h> #include<cstring> #include<cstdlib> #include<queue> #include<algorithm> #define LL long long using namespace std; const int N = 186; struct Node { int a, b, step; char str ; }; bool v ; void bfs(int a, int b, int c) { memset(v, 0, sizeof(v)); Node t, s; queue<Node> q; t = (Node){0, 0, 0}; q.push(t); v[t.a][t.b] = true; while(!q.empty()) { t = q.front(); q.pop(); v[t.a][t.b] = true; if(t.a == c || t.b == c) { printf("%d\n", t.step); for(int i = 1; i <= t.step; i++) { printf("%s\n", t.str[i]); } return ; } if(t.a == 0) { s = t; s.a = a; s.step++; strcpy(s.str[s.step], "FILL(1)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } else if(t.a <= a) { s = t; s.a = 0; s.step++; strcpy(s.str[s.step], "DROP(1)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } if(t.b < b) { s = t; if(t.a+t.b <= b) { s.a = 0; s.b = t.a+t.b; } else{ s.a = t.a - (b-t.b); s.b = b; } s.step++; strcpy(s.str[s.step], "POUR(1,2)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } } if(t.b == 0) { s = t; s.b = b; s.step++; strcpy(s.str[s.step], "FILL(2)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } else if(t.b <= b) { s = t; s.b = 0; s.step++; strcpy(s.str[s.step], "DROP(2)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } if(t.a < a) { if(t.a+t.b < a) { s = t; s.a = t.a+t.b; s.b = 0; } else{ s = t; s.a = a; s.b = t.b - (a-t.a); } s.step++; strcpy(s.str[s.step], "POUR(2,1)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } } } printf("impossible\n"); return; } int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); bfs(a, b, c); return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- Surrounded Regions