博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3414 Pots 记录路径的搜索
阅读量:4217 次
发布时间:2019-05-26

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

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 ≤ ≤ 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 AB, 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)

题目大意:两个水壶,给出各自容量,开始都没有水,问经过如上几种操作后,如何能在最少操作下到达其中一个水壶中的水为c升。并输出该操作过程。

 

题目难点在于如何保存路径,这里的路径不单单是常数,而是一个操作。

这里采用map<data,data> 把后一个点指向前一个点,类似一棵树的多个分支,最后可以通过不断往回找 可以到达最初的点

即 初始状态。

注意:map 用到结构体时候 需要自定义 比较 bool operator < (DataType data) const

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;int a,b,c;struct Pot{ int a,b; string s; //必须 指定 结构体的 大小比较 map 需要 bool operator < (Pot o)const { if(a != o.a) return a < o.a; else return b < o.b; }};map
mp;void bfs(int s_a, int s_b){ Pot pot,tmp; queue
q; tmp.b = tmp.a = -1; pot.a = s_a; pot.b = s_b; mp[pot] = tmp; q.push(pot); while(!q.empty()){ pot = q.front(); q.pop(); if(pot.a == c || pot.b == c){ break; } // fill a tmp.a = a; tmp.b = pot.b;tmp.s = "FILL(1)"; if(mp.find(tmp) == mp.end()) mp[tmp] = pot,q.push(tmp); //fill b tmp.a = pot.a; tmp.b = b; tmp.s = "FILL(2)"; if(mp.find(tmp) == mp.end()) mp[tmp] = pot,q.push(tmp); // drop a tmp.a = 0; tmp.b = pot.b; tmp.s ="DROP(1)"; if(mp.find(tmp) == mp.end()) mp[tmp] = pot,q.push(tmp); // drop b tmp.a = pot.a; tmp.b = 0; tmp.s = "DROP(2)"; if(mp.find(tmp) == mp.end()) mp[tmp] = pot,q.push(tmp); //pour a to b tmp.a = max(0,pot.a+pot.b-b); tmp.b = min(pot.a+pot.b,b); tmp.s = "POUR(1,2)"; if(mp.find(tmp) == mp.end()) mp[tmp] = pot,q.push(tmp); // pour b to a tmp.a = min(a,pot.a+pot.b); tmp.b = max(0,pot.a+pot.b-a); tmp.s = "POUR(2,1)"; if(mp.find(tmp) == mp.end()) mp[tmp] = pot,q.push(tmp); } if(pot.a == c || pot.b == c){ int step = 0; string ans = ""; // p.a == 0 && p.b == 0 时候则为初始状态 结束 for(Pot p = pot; p.a || p.b; p = mp[p]){ ++step; ans = p.s + "\n" + ans; } cout << step << endl; cout << ans <
> a >> b >> c; bfs(0,0); return 0; }

 

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

你可能感兴趣的文章
Web前端学习笔记——JavaScript之变量、操作符、表达式和语句
查看>>
Web前端学习笔记——JavaScript之WEBAPI、BOM、DOM及获取页面元素
查看>>
Web前端学习笔记——JavaScript之特效
查看>>
Web前端学习笔记——JavaScript之事件详解
查看>>
Web前端学习笔记——JavaScript之事件、创建元素、节点操作
查看>>
Web前端学习笔记——JavaScript之正则表达式、伪数组、垃圾回收
查看>>
Web前端学习笔记——JavaScript 之继承、函数进阶
查看>>
Web前端学习笔记——JavaScript之面向对象游戏案例:贪吃蛇
查看>>
Web前端学习笔记——JavaScript之面向对象编程
查看>>
上海控安成功举办普陀区科普创新专项智能网联车学术活动
查看>>
控安轩辕实验室:利用开源项目实现定位和时间欺骗(二)
查看>>
基于预测的自动驾驶全球导航卫星系统欺骗攻击检测
查看>>
上海工业互联网协会安全专委会成立,加快提升工业互联网安全保障能力
查看>>
普陀区委副书记顾军、区政府副区长魏静带队调研上海控安
查看>>
上海市水务工控系统安全联合研究实验室正式启用
查看>>
基于数字孪生的水务系统安全试验床正式上线
查看>>
上海控安成功入选“2020年度上海市工业互联网平台和专业服务商推荐目录”
查看>>
多传感器融合定位是否足够安全?(一)
查看>>
多传感器融合定位是否足够安全?(二)
查看>>
上海申通地铁集团院士专家工作站与上海控安达成战略合作
查看>>