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)递推. ...
随机推荐
- Android开发笔记(一百四十三)任务调度JobScheduler
Android开发笔记(一百四十三)任务调度JobScheduler
- inputStream 与 String 的互相转换
一. String 转换为InputStream String str = "String 与 inputStream转换"; InputStream ins1 = new Byt ...
- Android学习总结(十四) ———— ListView Item多布局的实现
一.基本概念 实现一个Item的多布局.像我们经常在用的各种即时通讯工具,QQ.微信等,假设他们的会话界面是ListView实现的,那么ListView就有多种Item布局,要实现ListView里面 ...
- 8 Java 归并排序(MergerSort)
图片素材与文字描述来自:尚硅谷-韩顺平数据结构与算法. 1.基本思想 归并排序是利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divi ...
- Feign-请求不同注册中心的服务
场景 需要通过Feign Client请求,其他注册中心或者其他Restful服务. 临时方案 Feign 请求转为RestTemplate http请求. 优点:能适应,feign环境和非feign ...
- 线程调度的问题:Lock Convoy(锁封护)与Priority Inversion(优先级反转)
Lock Convoy(锁封护) [1]Lock Convoy是在多线程并发环境下由于锁的使用而引起的性能退化问题.当多个相同优先级的线程频繁地争抢同一个锁时可能会引起lock convoy问题,一般 ...
- Bootstrap图片支持响应式
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- JsonUtils工具类
public class JsonUtils { public static void printTimeObject(Object obj, HttpServletResponse response ...
- 四. python网络编程
第八章.网络基础知识 1. TCP/IP协议介绍 1.TCP/IP概念 TCP/IP: Transmission Control Protocol/Internet Protocol的简写,中译名为传 ...
- Ukulele 常用和弦