博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pots bfs
阅读量:5281 次
发布时间:2019-06-14

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

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,六种操作,代码中op数组给出,然后需要标记ab,同样的状态不重复记录。queue不太会用,还是用数组队列吧 代码:
#include 
#include
#include
#include
#include
#include
using namespace std;int a,b,c,ta,tb,flag,head=0,tail=0;string op[6]={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};struct que{ int al,bl,f,c; int ope;}q[10000];int vis[101][101];///用这个有保障void print(int x){ if(q[x].f!=-1)print(q[x].f); else return; cout<
<
=ta)?(tb+=ta,ta=0):(ta-=b-tb,tb=b);} else{a-ta>=tb?(ta+=tb,tb=0):(tb-=a-ta,ta=a);} if(vis[ta][tb])continue; vis[ta][tb]=1; q[tail].ope=i; q[tail].al=ta,q[tail].bl=tb,q[tail].f=head,q[tail++].c=q[head].c+1; } head++; } else {flag=1;cout<<0<

 

转载于:https://www.cnblogs.com/8023spz/p/7246436.html

你可能感兴趣的文章
C# 通过 Quartz .NET 实现 schedule job 的处理
查看>>
关于java之socket输入流输出流可否放在不同的线程里进行处理
查看>>
目前为止用过的最好的Json互转工具类ConvertJson
查看>>
Day13
查看>>
tensorflow saver简介+Demo with linear-model
查看>>
Luogu_4103 [HEOI2014]大工程
查看>>
Oracle——SQL基础
查看>>
项目置顶随笔
查看>>
Redis的安装与使用
查看>>
P1970 花匠
查看>>
java语言与java技术
查看>>
NOIP2016提高A组五校联考2总结
查看>>
iOS 项目的编译速度提高
查看>>
table中checkbox选择多行
查看>>
Magento开发文档(三):Magento控制器
查看>>
性能调优攻略
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
2019-8-5 考试总结
查看>>
JS中实现字符串和数组的相互转化
查看>>