Pots(POJ_3414)
来源:互联网 发布:室内在线设计软件 编辑:程序博客网 时间:2024/06/05 04:46
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)
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;}
- Pots(POJ_3414)
- poj_3414 Pots(bfs)
- poj_3414
- Pots
- Pots
- Pots
- Pots
- Pots
- Pots
- pots
- pots
- Pots
- Pots
- Pots
- Pots
- Pots
- Pots
- Pots
- ViewPaper+Fragment+RadioButton
- winform 创建自定义控件
- 2.DB-Mysql++实例
- Java abstract抽象类与匿名内部类
- 学习iOS,你能迎娶到白富美吗?
- Pots(POJ_3414)
- 分离式JavaScript
- 安全密码(2013中南大学研究生复试上机题)
- android oom 全解析
- iOS URL Scheme
- web服务器
- SEO外链专员操作规范
- Leetcode: Invert Binary Tree
- 超长正整数的相加