Pots(POJ_3414)
2015-08-21 11:16
351 查看
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)
Source
Northeastern Europe 2002, Western Subregion代码
//咱写的很通俗易懂啦 #include <iostream> #include <queue> #include <map> #include <cstdio> #include <cstring> using namespace std; struct node { int x,y; //x为第一个杯子当前水的体积,y为第二个... string s;//s记录获得当前状态所进行的操作 bool operator <(const node b)const { if(x!=b.x) return x<b.x; return y<b.y; } }; int main() { int a,b,c,sum; node t,temp; t.x=t.y=0; temp.x=-1; map<node,node>m; queue<node>q; q.push(t); m[t]=temp; cin>>a>>b>>c; while(q.size()) { t=q.front(); q.pop(); if(t.x==c||t.y==c) break; temp.x=a; temp.y=t.y; temp.s="FILL(1)"; if(m.find(temp)==m.end()) { q.push(temp); m[temp]=t; } temp.x=t.x; temp.y=b; temp.s="FILL(2)"; if(m.find(temp)==m.end()) { q.push(temp); m[temp]=t; } temp.x=0; temp.y=t.y; temp.s="DROP(1)"; if(m.find(temp)==m.end()) { q.push(temp); m[temp]=t; } temp.x=t.x; temp.y=0; temp.s="DROP(2)"; if(m.find(temp)==m.end()) { q.push(temp); m[temp]=t; } temp.x=(t.x+t.y<=b?0:t.x-(b-t.y)); temp.y=(t.x+t.y<b?t.x+t.y:b); temp.s="POUR(1,2)"; if(m.find(temp)==m.end()) { q.push(temp); m[temp]=t; } temp.x=(t.x+t.y<a?t.x+t.y:a); temp.y=(t.x+t.y<=a?0:t.y-(a-t.x)); temp.s="POUR(2,1)"; if(m.find(temp)==m.end()) { q.push(temp); m[temp]=t; } } sum=0; if(t.x==c||t.y==c) { string ans=""; for(temp=t;temp.x||temp.y;temp=m[temp]) { ans=temp.s+"\n"+ans; sum++; } cout<<sum<<endl<<ans; } else cout<<"impossible"<<endl; return 0; }
相关文章推荐
- 手机移动端左右滚动特效
- Java abstract抽象类与匿名内部类
- 关于fragment中嵌套viewpager的问题
- maven setting.xml和pom.xml配置详解
- poj1797 最短路变形
- redis:hash数据类型与操作
- [hdu5416 CRB and Tree]树上路径异或和,dfs
- Servlet中Web.xml配置详解(二)
- C++ 11: 右值引用,转移语义与完美转发
- Java之获取系统属性
- winform 创建自定义控件
- ViewPaper+Fragment+RadioButton
- 线程方法详解及方法小例子
- eclipse 一些快捷用法
- Oracle数据库的启动和关闭
- hdu 1869 六度分离(Floyd算法)
- 去掉iframe的边框
- Pomelo:网易开源基于 Node.js 的游戏服务端框架
- PostgreSQL表空间、数据库、模式、表、用户/角色之间的关系(转)
- 自定义VS的ItemTemplates 实现任意文件结构