poj 3414 Pots
2015-08-19 16:03
357 查看
题意:给出A, B, C分别代表第一个瓶子的容量,第二个瓶子的容量,以及目标水量,只要是经过以下六个步骤能够有一个瓶子能够达到目标水量,就完成目标,记录步骤;
1. 倒空第一个
2. 倒空第二个
3. 把第一个瓶子的水倒入第二个
4. 把第二个瓶子的水倒入第一个
5. 倒满第一个
6. 倒满第二个
ps : 上述六个步骤无先后顺序
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
Sample Output
1. 倒空第一个
2. 倒空第二个
3. 把第一个瓶子的水倒入第二个
4. 把第二个瓶子的水倒入第一个
5. 倒满第一个
6. 倒满第二个
ps : 上述六个步骤无先后顺序
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
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #define LL long long #define INF 0x3f3f3f3f #define PI 3.1415926 #define RR freopen("in.txt","r",stdin) #define WW freopen("out.txt","w",stdout) using namespace std; int A,B,C; struct node { int a,b; int step; string str[200]; }; bool vis[110][110]; bool BFS() { memset(vis,false,sizeof(vis)); queue<node>Q; node tmp,c; tmp.a = 0; tmp.b = 0; tmp.step = 0; vis[0][0] = true; Q.push(tmp); while(!Q.empty()) { tmp = Q.front(); Q.pop(); if(tmp.a == C || tmp.b == C) { printf("%d\n",tmp.step); for(int i=1; i<=tmp.step; i++) { cout<<tmp.str[i]<<endl; } return true; } if(tmp.a == 0) { c = tmp; c.a = A; c.step ++; c.str[c.step] = "FILL(1)"; if(!vis[c.a][c.b]) { vis[c.a][c.b] = true; Q.push(c); } } if(tmp.b == 0) { c = tmp; c.step ++; c.b = B; c.str[c.step] = "FILL(2)"; if(!vis[c.a][c.b]) { vis[c.a][c.b] = true; Q.push(c); } } if(tmp.a <= A && tmp.a != 0) { c = tmp; c.step ++; c.a = 0; c.str[c.step] = "DROP(1)"; if(!vis[c.a][c.b]) { vis[c.a][c.b] = true; Q.push(c); } } if(tmp.b <= B && tmp.b != 0) { c = tmp; c.step ++; c.b = 0; c.str[c.step] = "DROP(2)"; if(!vis[c.a][c.b]) { vis[c.a][c.b] = true; Q.push(c); } } if(tmp.a < A && tmp.b) { c = tmp; c.step ++; if(c.a + c.b <= A) { c.a += c.b; c.b = 0; } else { c.b -= (A- c.a); c.a = A; } c.str[c.step] = "POUR(2,1)"; if(!vis[c.a][c.b]) { vis[c.a][c.b] = true; Q.push(c); } } if(tmp.b < B && tmp.a) { c = tmp; c.step ++; if(c.a + c.b <= B) { c.b += c.a; c.a = 0; } else { c.a -= (B - c.b); c.b = B; } c.str[c.step] = "POUR(1,2)"; if(!vis[c.a][c.b]) { vis[c.a][c.b] = true; Q.push(c); } } } return false; } int main() { cin>>A>>B>>C; if(!BFS()) printf("impossible\n"); return 0; }
相关文章推荐
- ngx_lua模块学习笔记
- Android Studio中R文件丢失
- java concurrent包自带线程池和队列详细讲解
- 使RemoteFX媒体流在 Windows Server 2012 和 Windows 8无缝的多媒体体验
- ORA-00942: 表或视图不存在 "的原因和解决方法
- Android之如何在XML中添加单击事件
- MVC 页面跳转
- 【appium】根据UIAutomator定位元素
- Ugly Number
- UML类图几种关系的总结
- Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入
- 桌面支持--Win7下安装域管理工具
- 点击文字弹出图片层
- (C++ 11) 关联容器
- boot param for exmple
- ATSAMA5D35下pcf8563 i2c驱动分析
- Android 好看的搜索界面,大赞Animation
- iOS中视图控制器View的加载与生命周期
- 进制互转
- NYOJ 6 喷水装置(一)