Pots POJ
来源:互联网 发布:a星算法优化 编辑:程序博客网 时间:2024/06/15 18:19
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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, andC. 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 operationsK. 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
[Submit] [Go Back] [Status] [Discuss]
这类题真的是长的恶心。。。
tho: 重点大概就在保存路径上。
对了这叫六个入口的BFS 还不算太多
#include <iostream>#include <queue>#include <cstring>#include <cstdio>#include <algorithm>#define inf 0x3f3f3f3f#define ms(x) memset(x,0,sizeof(x))using namespace std;const int N = 200;int A, B, C;struct node { int a, b; int op; int pre; int step;} q[N * N];bool vis[N][N];int id[N * N];int f,s,en;void bfs() { node now, next; q[0].a = 0; q[0].b = 0; q[0].pre = -1; q[0].step = 0; int head, tail; head = tail = 0; tail++; vis[0][0] = 1; while (head < tail) { //用数组模拟队列的好处是保存编号 now = q[head]; head++; for(int i = 1; i <= 6; i++) { if(i==1){ // fill 1; next.a = A; next.b = now.b; } else if(i==2){ // fill 2; next.b = B; next.a = now.a; } else if(i==3){ // drop 1; next.a = 0; next.b = now.b; } else if(i==4){ // drop 2; next.b = 0; next.a = now.a; } else if(i==5){ // 1 to 2; if(now.a + now.b > B) { next.b = B; } else { next.b = now.a + now.b; } next.a = now.a + now.b - next.b; } else if(i==6){ // 2 to 1 if(now.a + now.b > A) { next.a = A; } else { next.a = now.a + now.b; } next.b = now.a + now.b - next.a; } if(!vis[next.a][next.b]) { next.op = i; next.pre = head - 1; //存前一个操作的编号 vis[next.a][next.b] = 1; next.step = now.step + 1; q[tail++] = next; if(next.a == C || next.b == C) { f = 1; en = tail - 1; s = next.step; printf("%d\n", s); return ; } } } }}int main() { scanf("%d%d%d", &A, &B, &C); ms(vis); f=0; bfs(); if(f) { id[s] = en; //这是最后一步操作的编号 for(int i = s-1; i>=1;i--){ id[i] = q[id[i+1]].pre; //向前遍历,找当前步的前一步 } for(int i=1;i<=s;i++){ switch(q[id[i]].op){ case 1: puts("FILL(1)"); break; case 2: puts("FILL(2)"); break; case 3: puts("DROP(1)"); break; case 4: puts("DROP(2)"); break; case 5: puts("POUR(1,2)"); break; case 6: puts("POUR(2,1)"); break; } } } else puts("impossible\n"); return 0;}
- Pots POJ
- Pots (POJ
- Pots POJ
- Pots POJ
- Pots POJ
- Pots POJ
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots BFS
- POJ 3414 Pots
- poj 3414 Pots
- poj 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- POJ 3414(Pots)
- POJ 3414 Pots
- POJ 3414 Pots
- 深入了解MyBatis参数
- Java设计模式之迭代器模式
- Annotation(注解)
- 有关Lucene的问题(3): 向量空间模型与Lucene的打分机制
- 网站被降权或site首页不见的处理方法
- Pots POJ
- elasticsearch-5.6.0及head插件安装
- 学习Tutorial--catlikecoding
- ScrollView自动布局的实现方式
- LIVE555再学习 -- Linux 下编译
- 基于S3C2440的嵌入式Linux驱动——Framebuffer子系统解读
- Java与Highcharts实例(一)
- MyBatis中的OGNL教程
- spring 和 redis的集成(使用注解方式)