POJ 1606 Jugs
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 4280 | Accepted: 2533 | Special Judge | ||
Description
You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.
A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are
fill A
fill B
empty A
empty B
pour A B
pour B A
success
where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.
You may assume that the input you are given does have a solution.
Input
Output
Sample Input
3 5 4
5 7 3
Sample Output
fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
解题方法:类似于三个水杯倒水问题,用广搜。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; typedef struct cup
{
int a;
int b;
int pre;
int step;
}Cup; Cup Queue[];
int nCount = ; void PrintStep(int step)
{
switch(step)
{
case :
printf("fill A\n");
break;
case :
printf("fill B\n");
break;
case :
printf("empty A\n");
break;
case :
printf("empty B\n");
break;
case :
printf("pour A B\n");
break;
case :
printf("pour B A\n");
break;
}
} void Print(int front)
{
int k = front, j;
for (;;)
{
j = k;
k = Queue[k].pre;
if (Queue[j].pre == -)
{
Queue[j].pre = -;
break;
}
else
{
Queue[j].pre = -;
}
}
for (int i = ; i <= nCount; i++)
{
if (Queue[i].pre == -)
{
PrintStep(Queue[i].step);
}
}
printf("success\n");
} bool IsExit(int a, int b)
{
for (int i = ; i <= nCount; i++)
{
if (Queue[i].a == a && Queue[i].b == b)
{
return true;
}
}
return false;
} void BFS(int va, int vb, int result)
{
if (va == result)
{
printf("fill A\nsuccess\n");
return;
}
if (vb == result)
{
printf("fill B\nsuccess\n");
return;
}
int front = -;
int rear = -;
memset(Queue, , sizeof(Queue));
++nCount;
Queue[++rear].pre = -;
Queue[rear].a = va;
Queue[rear].b = ;
Queue[rear].step = ;
++nCount;
Queue[++rear].pre = -;
Queue[rear].a = ;
Queue[rear].b = vb;
Queue[rear].step = ;
int a, b;
while(front <= rear)
{
int cura = Queue[++front].a;
int curb = Queue[front].b;
if (cura == result || curb == result)
{
Print(front);
return;
}
if (cura > )
{
if (cura + curb > vb)
{
a = cura + curb - vb;
b = vb;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
}
else
{
a = ;
b = cura + curb;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
}
a = ;
b = curb;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
}
a = va;
b = curb;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
if (curb > )
{
if (cura + curb > va)
{
a = va;
b = cura + curb - va;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
}
else
{
a = cura + curb;
b = ;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
}
a = cura;
b = ;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
}
a = cura;
b = vb;
if (!IsExit(a, b))
{
Queue[++rear].a = a;
Queue[rear].b = b;
Queue[rear].pre = front;
Queue[rear].step = ;
++nCount;
}
}
} int main()
{
int va, vb, result;
while(scanf("%d%d%d", &va, &vb, &result) != EOF)
{
nCount = -;
memset(Queue, , sizeof(Queue));
BFS(va, vb, result);
}
return ;
}
POJ 1606 Jugs的更多相关文章
- [POJ] 1606 Jugs(BFS+路径输出)
题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- poj1066 Jugs
poj1066 Jugs http://poj.org/problem?id=1606 解题思路:本题可以用数学方法解得,最易理解,常规的解法是搜索.直接用接近模拟的广度优先搜索即可过. 给两个容器, ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- poj分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
- POJ题目分类(转)
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
随机推荐
- codeforces1025
hackforces + fstforces A 很明显当有一个字母出现次数>1时即合法 $n = 1$的情况需要特判 #include<cstdio> #include<ve ...
- ES-windos环境搭建(1)
前言 由于elasticsearch为Java开发,所以它还依赖Java JDK环境,并且对版本还有要求,需要1.8(含)以上.我们首先来配置Java JDK环境. JDK简介 JDK是Java语言的 ...
- apache下设置域名多站点访问及禁止apache访问80端口
apache下设置域名多站点访问 当前系统:macOS High Sierra 域名访问配置指定端口后,不同域名只能配置不同的端口 apache配置目录: sudo vim /etc/apache2/ ...
- div+css 布局经验 - 最简单的 = 最不变形的(原创技巧)
站酷几年了 一直饱受其恩泽 尤为感激 一直想奉献些什么 但是苦于水平 苦于奔波 今天静下心来 为大家奉献下 自己的div+css 经验 ,以下观点只代表 深海个人立场 希望为初学者提供一条" ...
- COGS 1427. zwei
★☆ 输入文件:zwei.in 输出文件:zwei.out 简单对比时间限制:1 s 内存限制:256 MB [样例输入] 5 5 1 2 3 4 5 1 1 3 1 3 5 0 ...
- ABAP Netweaver, SAP Cloud Platform和Kubernetes的用户区分
ABAP Dialog: Individual, interactive system access. System: Background processing and communication ...
- PHP高端课程
关于目后佐道IT教育 http://www.cnblogs.com/itpua/p/7710917.html 目后佐道IT教育的师资团队 http://www.cnblogs.com/itpua/p/ ...
- 关于 QObject 类
1.QObject类 简述 QObject类是所有Qt对象的基类. QObject是Qt对象模型的核心. 该模型的核心特征是称为信号和槽的对象通信机制. 您可以使用connect()将信号连接到槽 ...
- DirectX9(翻译):介绍
一.简介 二.DirectX Software Development Kit 这本帮助文档总共分为五大部分:DirectX Software Development Kit DirectX Grap ...
- Docker基础内容之网络基础
网络命名空间基本原理 单机版多容器实例网络交互原理 在宿主机上面打开两张网卡eth0与eth1,打通两张网卡的链路 在test1上面启动一个veth网卡,创建一个namespace:并桥接到eth0上 ...