POJ
来源:互联网 发布:网速监控软件 编辑:程序博客网 时间:2024/06/09 18:58
题目链接:http://poj.org/problem?id=3414点击打开链接
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)
Source
跟hdu的非常可乐差不多 主要是记录比较麻烦
每一步都记录之前的步数 然后递推往后
也可以只记录每个步数的前驱 然后最后放到一个栈倒序输出
#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <stack>#include <limits.h>#include <string>#include <string.h>#include <vector>#include <set>#include <map>#include <algorithm>#include <math.h>using namespace std;int n,m,k;struct xjy{ int nv; int mv; int type; char num; int step; int pre;};struct book{ int x; int y;};pair <int ,int >check;set<pair<int ,int > > s;vector <xjy> q;vector <xjy> ::iterator it;int iit;vector<xjy> aans;xjy ans;void bfs(){ xjy beg; beg.nv=0; beg.mv=0; beg.step=0; beg.num='k'; beg.pre=-1; check.first=beg.nv; check.second=beg.mv; q.push_back(beg); s.insert(check); it=q.begin(); while(iit!=q.size()) { xjy mid; mid=q[iit]; iit++; if(mid.mv==k||mid.nv==k) { ans=mid; break; } xjy midmid; midmid=mid; //1.n if(midmid.nv!=n) { midmid.nv=n; midmid.type=1; midmid.num='n'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } //1.m midmid=mid; if(midmid.mv!=m) { midmid.mv=m; midmid.type=1; midmid.num='m'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } //2.m midmid=mid; if(midmid.nv!=n&&midmid.mv!=0) { if(midmid.mv>(n-midmid.nv)) { midmid.mv=midmid.mv-(n-midmid.nv); midmid.nv=n; midmid.type=2; midmid.num='m'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } else { midmid.nv+=midmid.mv; midmid.mv=0; midmid.type=2; midmid.num='m'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } } //2.n midmid=mid; if(midmid.mv!=m&&midmid.nv!=0) { if(midmid.nv>(m-midmid.mv)) { midmid.nv-=(m-midmid.mv); midmid.mv=m; midmid.type=2; midmid.num='n'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } else { midmid.mv+=midmid.nv; midmid.nv=0; midmid.type=2; midmid.num='n'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } } //3.m midmid=mid; if(midmid.mv!=0) { midmid.mv=0; midmid.type=3; midmid.num='m'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } //3.n midmid=mid; if(midmid.nv!=0) { midmid.nv=0; midmid.type=3; midmid.num='n'; midmid.step++; midmid.pre=iit-1; check.first=midmid.nv; check.second=midmid.mv; if(!s.count(check)) { q.push_back(midmid); s.insert(check); } } }}int main(){ scanf("%d%d%d",&n,&m,&k); //if((k&1)&&!(n&1)&&!(m&1)) //printf("impossible\n"); //else { bfs(); if(ans.step) { printf("%d\n",ans.step); int ppre=ans.pre; aans.push_back(ans); while(ppre!=0) { aans.push_back(q[ppre]); ppre=q[ppre].pre; } for(it=aans.end()-1;it>=aans.begin();it--) { if((*it).type==1) { printf("FILL"); printf("("); if((*it).num=='m') printf("2"); else if((*it).num=='n') printf("1"); printf(")\n"); } else if((*it).type==2) { printf("POUR"); printf("("); if((*it).num=='m') printf("2,1"); else if((*it).num=='n') printf("1,2"); printf(")\n"); } else if((*it).type==3) { printf("DROP"); printf("("); if((*it).num=='m') printf("2"); else if((*it).num=='n') printf("1"); printf(")\n"); } } } else { printf("impossible\n"); } }}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 硬链接与软连接
- 软件安装
- 使用Popup做一个ADF的弹窗
- 8086小甲鱼汇编总结
- 2017华为武长区优招面经—网络技术工程师(服务岗)
- POJ
- javascript中this的四种用法
- Ajax实现简单的google索搜框
- DOS命令创建数据库的中文乱码解决方案
- 数据结构 -- 队列 & 循环队列 -- 数组实现
- Java 封装,继承,多态!三者的关系!
- css学习笔记(二)
- AngularJS Phonecat(步骤0-步骤5)
- a标签突出显示盒子非js效果