POJ3414(KB1-H BFS)
Pots
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
//2017-02-24
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int a, b, step, pre, op;
}q[]; int pots[], POT[], book[][];
string option[] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; void fill(int i)
{
pots[i] = POT[i];
} void drop(int i)
{
pots[i] = ;
} void pour(int i, int j)
{
if(pots[i] > POT[j]-pots[j]){
pots[i] -= (POT[j]-pots[j]);
pots[j] = POT[j];
}else{
pots[j] += pots[i];
pots[i] = ;
}
} void init(int a, int b)
{
pots[] = a;
pots[] = b;
} void push(int pos, int a, int b, int step, int pre, int op)
{
q[pos].a = a;
q[pos].b = b;
q[pos].step = step;
q[pos].pre = pre;
q[pos].op = op;
} void print(int pos)
{
if(q[pos].pre == -)return;
print(q[pos].pre);
cout<<option[q[pos].op]<<endl;
} int min(int a, int b)
{
return a < b ? a : b;
} int main()
{
int C, a, b, step;
while(cin>>POT[]>>POT[]>>C)
{
int head = , tail = ;
q[].a = ;
q[].b = ;
q[].step = ;
q[].pre = -;
memset(book, , sizeof(book));
book[][] = ;
while(head < tail)
{
a = q[head].a;
b = q[head].b;
step = q[head].step; if(a==C || b==C){
cout<<step<<endl;
print(head);
break;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} if(a>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} if(b>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} head++;
}
if(head>=tail)cout<<"impossible"<<endl;
}
return ;
}
POJ3414(KB1-H BFS)的更多相关文章
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj3414 Pots(BFS)
题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...
- FZU - 2150 bfs [kuangbin带你飞]专题一
题意:两个人玩很变态的游戏,将一个草坪的某两个点点燃,点燃的草坪可以向上下左右四个方向扩散,问能否将整块草坪上面的草都点燃.如果能,输出最短时间(^_^他们就能玩更变态的游戏了),如果不能,输出-1. ...
- UVA-11882 bfs + dfs + 剪枝
假设当前已经到达(x,y),用bfs判断一下还可以到达的点有maxd个,如果maxd加上当前已经经过的长度小于当前答案的长度就退出,如果相同,就将bfs搜索到的点从大到小排序,如果连最大序列都无法大于 ...
- 广度优先(bfs)和深度优先搜索(dfs)的应用实例
广度优先搜索应用举例:计算网络跳数 图结构在解决许多网络相关的问题时直到了重要的作用. 比如,用来确定在互联网中从一个结点到另一个结点(一个网络到其他网络的网关)的最佳路径.一种建模方法是采用无向图, ...
- 邻接矩阵实现图的存储,DFS,BFS遍历
图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...
- ZH奶酪:【数据结构与算法】搜索之BFS
1.目标 通过本文,希望可以达到以下目标,当遇到任意问题时,可以: 1.很快建立状态空间: 2.提出一个合理算法: 3.简单估计时空性能: 2.搜索分类 2.1.盲目搜索 按照预定的控制策略进行搜索, ...
- 扫描线+堆 codevs 2995 楼房
2995 楼房 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...
- 【BZOJ 1054】 [HAOI2008]移动玩具
Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动 ...
随机推荐
- react-router-dom 手动控制路由跳转
基于 react-router 4.0 版本,我们想要通过 JS 手动控制路由跳转,分三步: 第一步:引入 propTypes const PropTypes = require('prop-type ...
- [JS深入学习]——数组对象排序
(转) JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序. sort() 方法用于对数组的元素进行排序.语法如下: arrayObject. ...
- jmeter插件之自定义场景图(万能场景设计)
添加扩展插件 自定义线程组:jp@gc - Ultimate Thread Group 此线程组功能强大,可以实现多种场景设置,添加路径如图 参数含义解释 Start Threads Count:当前 ...
- day 46 Django 学习3 数据库单表操作以及反向解析
前情提要: Django 已经学了不少了, 今天学习链接数据库的操作.以及相关的反向解析等 一:反向解析 1:反向解析模板层 跳转时设定url会随着前面的路由改变而改变 2:反向解析之 ...
- pythonweb框架Flask学习笔记04-模板继承
# -*- coding:utf-8 -*- from flask import render_template,Flask app=Flask(__name__) @app.route('/hell ...
- celery初始化
# 在任务处理者一端加初始化 import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", &qu ...
- 【13】JMicro微服务-ID生成与Redis
如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 往下看前,建议完成前面1到12小节 1. 微服务中ID地位 如果说前面小节的功能点是微服务的大脑,那么全局唯一ID则是微服务 ...
- EF基础知识小记七(拆分实体到多个表以及拆分表到多个实体)
一.拆分实体到多个表 1.在日常开发中,会经常碰到一些老系统,当客户提出一些新的需求,这些需求需要在原来的表的基础上加一些字段,大多数人会选择通过给原表添加字段的方式来完成这些需求,方法,虽然可行,但 ...
- postman—post方式几种请求参数区别
postman中 form-data.x-www-form-urlencoded.raw.binary的区别 版权声明参考: https://blog.csdn.net/wangjun5159/art ...
- Ubuntu系统下OpenDaylight源码编译安装
操作系统:Linux x64 / Ubuntu 14.04 研究领域:软件定义网络SDN (Software-defined Networking) 开发组件:OpenDaylight 声明:转载请注 ...