博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-3414 Pots (BFS)
阅读量:5012 次
发布时间:2019-06-12

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

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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) 题目分析:打眼一看就是BFS,还是普通的BFS。 代码如下:
1 # include
2 # include
3 # include
4 # include
5 # include
6 # include
7 # include
8 using namespace std; 9 struct node 10 { 11 int a,b,t; 12 vector
op; 13 bool operator < (const node &a) const { 14 return t>a.t; 15 } 16 node & operator = (const node &p) { 17 a=p.a,b=p.b,t=p.t; 18 op.clear(); 19 for(int i=0;i
q; 28 memset(vis,0,sizeof(vis)); 29 node sta; 30 sta.a=sta.b=sta.t=0; 31 sta.op.clear(); 32 vis[0][0]=1; 33 q.push(sta); 34 while(!q.empty()) 35 { 36 node u=q.top(); 37 q.pop(); 38 if(u.a==C||u.b==C){ 39 printf("%d\n",u.t); 40 for(int i=0;i
0){ 65 node now=u; 66 now.a=0,now.b=u.b; 67 if(!vis[now.a][now.b]){ 68 vis[now.a][now.b]; 69 now.t=u.t+1; 70 now.op.push_back("DROP(1)"); 71 q.push(now); 72 } 73 } 74 if(u.b>0){ 75 node now=u; 76 now.a=u.a,now.b=0; 77 if(!vis[now.a][now.b]){ 78 vis[now.a][now.b]; 79 now.t=u.t+1; 80 now.op.push_back("DROP(2)"); 81 q.push(now); 82 } 83 } 84 if(u.a
0){ 85 node now=u; 86 now.a=min(A,u.a+u.b); 87 now.b=max(0,u.b-A+u.a); 88 if(!vis[now.a][now.b]){ 89 vis[now.a][now.b]=vis[now.b][now.a]=1; 90 now.t=u.t+1; 91 now.op.push_back("POUR(2,1)"); 92 q.push(now); 93 } 94 } 95 if(u.a>0&&u.b

 

转载于:https://www.cnblogs.com/20143605--pcx/p/4734479.html

你可能感兴趣的文章
第10章 使用Apache服务部署静态网站
查看>>
关于给予webApp框架的开发工具
查看>>
c语言编写的生成泊松分布随机数
查看>>
Maven入门笔记
查看>>
iOS webView的常见属性和方法
查看>>
理解position:relative
查看>>
Codeforces Round #344 (Div. 2) Messager KMP的应用
查看>>
20145308刘昊阳 《Java程序设计》第4周学习总结
查看>>
js倒计时
查看>>
EasyUI datagrid 格式 二
查看>>
Android虹软人脸识别sdk使用工具类
查看>>
UI:基础
查看>>
浅谈 @RequestParam 和@PathVariable
查看>>
设计模式之---装饰器设计模式
查看>>
基于WordNet的英文同义词、近义词相似度评估及代码实现
查看>>
Equation漏洞混淆利用分析总结(上)
查看>>
shell学习1shell简介
查看>>
Qt 【无法打开 xxxx头文件】
查看>>
JAVA项目将 Oracle 转 MySQL 数据库转换(Hibernate 持久层)
查看>>
三层架构(我的理解及详细分析)
查看>>