Jugs
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4280   Accepted: 2533   Special Judge

Description

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

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

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

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的更多相关文章

  1. [POJ] 1606 Jugs(BFS+路径输出)

    题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...

  2. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  3. poj1066 Jugs

    poj1066 Jugs http://poj.org/problem?id=1606 解题思路:本题可以用数学方法解得,最易理解,常规的解法是搜索.直接用接近模拟的广度优先搜索即可过. 给两个容器, ...

  4. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  5. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  6. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  7. poj 题目分类(1)

    poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...

  8. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  9. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

随机推荐

  1. Sass基本特性

    Sass扩展/继承@extend 代码的继承,声明方式:.class;调用方式:@extend 如: .btn { border: 1px solid #ccc; padding: 6px 10px; ...

  2. Android 坑爹问题

    A/art: art/runtime/jdwp/jdwp_event.cc:] Check failed: Thread::Current() != GetDebugThread() (Thread: ...

  3. github入门之基本操作--4

    1.初始化仓库 如果成功执行git init 命令,该目录下会生成一个.git的目录 2.查看仓库状态 *注: 实际工作中,git status使用次数非常多,一定要记住.因为当工作树和仓库被操作的过 ...

  4. shell框架

    #!/bin/bash#注释#注释#环境变量相关,如下PATH=/sbin:/bin:/usr/bin:/usr/sbin #引入库函数,如下,类似于c语言的#include "*.h&qu ...

  5. jmeter中通过jdbc方式连接mysql数据库的配置参考

    jmeter中通过jdbc方式连接mysql数据库的配置参考: Database URL=jdbc:mysql://ip:port/dbname?useUnicode=true&allowMu ...

  6. JS 操作对象 事件 样式

    1.获取标记对象 css 1 - class 2 - id 3 - 标记选择器 js 1 - class 2 - id 3 - 标记 4 - name + document.getElementByI ...

  7. JS 一个页面关闭多个页面

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  8. 洛谷 P1376 机器工厂

    题目描述 小T开办了一家机器工厂,在N(N<=10000)个星期内,原材料成本和劳动力价格不断起伏,第i周生产一台机器需要花费Ci(1<=Ci<=5000)元.若没把机器卖出去,每保 ...

  9. C#入门(2)

    C#入门(2) Exception 基本异常的核心成员: System.Exception Property Meaning Data read-only,实现了IDirectory接口的一些键值对, ...

  10. UEditor1.4.3的实例程序

    官网:http://ueditor.baidu.com/website/ 配置下就可以使用 (1)下载,解压后文件结构如下: (2)将整个文件夹改名ueditor后复制到WebRoot目录下: (3) ...