poj 3414 Pots(BFS+递归打印)
来源:互联网 发布:mac office2011汉化包 编辑:程序博客网 时间:2024/05/22 17:08
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
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<cstdlib>#include<set>#include<queue>#include<stack>#include<vector>#include<map>#define N 100010#define Mod 10000007#define lson l,mid,idx<<1#define rson mid+1,r,idx<<1|1#define lc idx<<1#define rc idx<<1|1const double EPS = 1e-11;const double PI = acos ( -1.0 );const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace std;int n,m,x;string s[3]= {"FILL","DROP","POUR"};bool vis[110][110];struct node{ int num,nex;///num==:1(FILL),2(DROP),3(POUR) int t;///倒满,guang的瓶子 int x1,x2;///num=3时,由x1->x2 int A,B;///a,b状态 int step;///步数} mp[1010];queue<node>que;int fro,nex;bool bfs(){ while(que.size()) que.pop(); memset(vis,0,sizeof vis); node a; fro=0,nex=1; mp[fro].A=0,mp[fro].B=0; mp[fro].nex=-1; mp[fro].step=0; que.push(mp[fro]); vis[0][0]=1; while(que.size()) { a=que.front(); que.pop(); if(a.A==x||a.B==x) { printf("%d\n",a.step); return 1; } mp[nex].nex=-1; if(!vis[n][a.B])///倒满A { mp[nex].nex=fro; mp[nex].A=n,mp[nex].B=a.B; mp[nex].num=1; mp[nex].t=1; mp[nex].step=a.step+1; que.push(mp[nex]); vis[n][a.B]=1; nex++; } if(!vis[a.A][m])///倒满B { mp[nex].nex=fro; mp[nex].A=a.A,mp[nex].B=m; mp[nex].num=1; mp[nex].t=2; mp[nex].step=a.step+1; que.push(mp[nex]); vis[a.A][m]=1; nex++; } if(!vis[0][a.B])///倒光A { mp[nex].nex=fro; mp[nex].A=0,mp[nex].B=a.B; mp[nex].num=2; mp[nex].t=1; mp[nex].step=a.step+1; que.push(mp[nex]); vis[0][a.B]=1; nex++; } if(!vis[a.A][0])///倒光B { mp[nex].nex=fro; mp[nex].A=a.A,mp[nex].B=0; mp[nex].num=2; mp[nex].t=2; mp[nex].step=a.step+1; que.push(mp[nex]); vis[a.A][m]=1; nex++; } if(a.A>0)///A->B { int t=m-a.B; if(a.A>t)// { mp[nex].A=a.A-t; mp[nex].B=m; } else // { mp[nex].A=0; mp[nex].B=a.A+a.B; } if(!vis[mp[nex].A][mp[nex].B]) { mp[nex].num=3; mp[nex].x1=1,mp[nex].x2=2; mp[nex].nex=fro; mp[nex].step=a.step+1; que.push(mp[nex]); vis[mp[nex].A][mp[nex].B]=1; nex++; } } if(a.B>0)///B->A { int t=n-a.A; if(a.B>t)// { mp[nex].A=n; mp[nex].B=a.B-t; } else // { mp[nex].A=a.A+a.B; mp[nex].B=0; } if(!vis[mp[nex].A][mp[nex].B]) { mp[nex].num=3; mp[nex].x1=2,mp[nex].x2=1; mp[nex].nex=fro; mp[nex].step=a.step+1; que.push(mp[nex]); vis[mp[nex].A][mp[nex].B]=1; nex++; } } fro++; } return 0;}void print(int x){ if(mp[x].nex!=-1) { print(mp[x].nex); if(mp[x].num==1) printf("FILL(%d)\n",mp[x].t); else if(mp[x].num==2) printf("DROP(%d)\n",mp[x].t); else printf("POUR(%d,%d)\n",mp[x].x1,mp[x].x2); }}int main(){ while(cin>>n>>m>>x) { if(!bfs()) cout<<"impossible"<<endl; else { print(fro); } } return 0;}
- poj 3414 Pots(BFS+递归打印)
- POJ 3414 Pots ( BFS , 打印路径 )
- POJ -3414 Pots(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)
- HDU 1495 非常可乐(bfs)|POJ 3414 Pots(bfs+打印路径)
- POJ 3414 Pots BFS
- POJ 3414 Pots(BFS)
- poj 3414 Pots bfs
- 背完这444句,你的口语绝对不成问题了
- [leetcode 85] Maximal Rectangle 待续
- 部闪痈参坑诳磐拐劫溉有靶探兹廊
- vector中的push_back()学习
- 正向代理与反向代理的区别分析
- poj 3414 Pots(BFS+递归打印)
- iOS数据持久化
- 搭建自动切换的HA
- Latex并列图形(不是subfigure)的画法,用到figure和minipage
- Java语言利用开源项目获取手机号码归属地城市
- iOS
- 收集:PowerDesigner常见问题解决与设置集锦
- <JAVA>多线程-生产者消费者例子
- uvaoj 10006 Carmichael Numbers 快速幂取模