博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3414——Pots(BFS)
阅读量:2343 次
发布时间:2019-05-10

本文共 2132 字,大约阅读时间需要 7 分钟。

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)

简单的BFS,三个操作总共六种状态,遍历就行

#include 
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 1010using namespace std;struct Node{ int a,b; int op[10000],cnt;};bool flag;int vis[110][110];void bfs(Node start,int a,int b,int c){ queue
q; q.push(start); while(!q.empty()) { Node tmp=q.front(); q.pop(); //cout<
<<" "<
<
b-tmp1.b) { tmp1.a-=(b-tmp1.b); tmp1.b=b; tmp1.op[tmp1.cnt++]=312; } else { tmp1.b+=tmp1.a; tmp1.a=0; tmp1.op[tmp1.cnt++]=312; } if(!vis[tmp1.a][tmp1.b]) { vis[tmp1.a][tmp1.b]=1; q.push(tmp1); } tmp1=tmp; if(tmp1.b>a-tmp1.a) { tmp1.b-=(a-tmp1.a); tmp1.a=a; tmp1.op[tmp1.cnt++]=321; } else { tmp1.a+=tmp1.b; tmp1.b=0; tmp1.op[tmp1.cnt++]=321; } if(!vis[tmp1.a][tmp1.b]) { vis[tmp1.a][tmp1.b]=1; q.push(tmp1); } }}int main(){ int a,b,c; while(~scanf("%d%d%d",&a,&b,&c)) { flag=false; Node start; memset(vis,0,sizeof(vis)); start.a=0; start.b=0; start.cnt=0; bfs(start,a,b,c); if(!flag) cout<<"impossible"<

转载地址:http://cicvb.baihongyu.com/

你可能感兴趣的文章
JQueryUI实现对话框
查看>>
Java流(Stream)/文件(File)/IO
查看>>
文件处理(压缩与解压)
查看>>
Java中的目录
查看>>
JQuery实现对select选择框的赋值
查看>>
JavaNIO学习(与IO比较)
查看>>
SweetAlert插件
查看>>
Java开发者必读的10篇精选优秀技术文章
查看>>
Java数据库开发
查看>>
编程精华资源(ITeye优秀专栏)大汇总
查看>>
先进软件开发技术与工具
查看>>
高级软件工程
查看>>
HTML学习总结
查看>>
JSP注释常用的有两种:HTML注释和隐藏注释(JSP专有注释)
查看>>
CSS学习总结
查看>>
大龄程序员的未来在何方
查看>>
MyEclipse中Egit安装与使用
查看>>
Egit使用过程中遇到的问题及解决办法
查看>>
Git学习总结
查看>>
JSON学习
查看>>