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)递推. ...
随机推荐
- leetcode395 Longest Substring with At Least K Repeating Characters
思路: 尺取法. 循环i:1~26,分别计算恰好包含i种字母并且每种字母出现的次数大于等于k个的最长子串长度. 没法直接使用尺取法,因为不满足区间单调性,但是使用如上的方法却是可以的,因为子串中包含的 ...
- Android Studio 升级到3.0后出现编译错误\.gradle\caches\transforms-1\files-1.1\*****-release.aar
Android Studio 升级到3.0后出现各种编译问题,其中有一个问题是关于资源找不到的问题,百度了半天,也没有相关的文章 C:\Users.gradle\caches\transforms-1 ...
- 最详细的github快速入门教程
一:下载github 二:安装GitHub 下载之后点击 进行安装过程,安装之后桌面上会有两个图标,如下图 三:新建项目 GitHub是图形界面模式,Git Shell是命令行模式,在Windows系 ...
- 继承UIView的初始化 、重绘、以及绘制图片
大家对于UIViewController的生命周期都相当了解了.但是对于继承UIView的子类能做什么,却很少有文章介绍的. 1. -initWithFrame:(CGRect)rect是view指 ...
- SQLServer怎样把本地数据导入到远程服务器上(转载)
平常用到mssql时间比较少,总是过一段时间就忘记应该怎么操作了.当要做mssq把本地数据导入到远程服务器的时候,就去网上搜索很久都没有图解的,所以今天自己收集一下免得下次又到处去找.希望对自己,同时 ...
- Fedora CentOS Red Hat中让vim支持语法高亮设置
Fedora / CentOS / Red Hat这三个系统里默认的vi是没有语法高亮显示的,白色的字体看起来很不舒服. 首先用命令行cat /etc/os-release查看当前linux系统的类型 ...
- x+2y+3z=n非负整数解
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; ty ...
- uva12264 Risk
最小值最大,就二分判断. map[i] = '0'+map[i];这样更方便 每个点拆成i,i’, S连i,cap为a[i],i’连T,cap为1(保证至少剩一个)或mid. i,i’ ,a[i] ...
- CPP-网络/通信:SOCKET
客户端实现代码: //引入头文件 #include <WinSock2.h> //客户端创建Socket////////////////////////////////////////// ...
- Java截取视频文件缩略图
/** * 截取视频第0帧的图片 */public static void videoImage(String filePath, String fileName,int widthdist, int ...