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. SPOJ1716 GSS3(线段树)

    题意 Sol 会了GSS1,GSS3就比较无脑了 直接加个单点修改即可,然后update一下 /* */ #include<cstdio> #include<cstring> ...

  2. mongodb-3.2.8 单机复制集安装

    规划: replSet 复制集名称: rs1 MongoDB数据库安装安装路径为:/usr/local/mongodb/ 复制集成员IP与端口: 节点1: localhost:28010   (默认的 ...

  3. leetcode 127 单词接龙

    给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...

  4. Java IO流之字符缓冲流

    字符流: 1.加入字符缓存流,增强读取功能(readLine) 2.更高效的读取数据 BufferedReader 从字符输入流读取文本,缓冲各个字符,从而实现字符.数组和行的高效读取. FileRe ...

  5. 用dfs求联通块(UVa572)

    一.题目 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符所在的格子相邻(横.竖.或者对角线方向),就说它们属于同一个八连块. 二.解题思路 和前面的二叉树遍历类似,图也有DF ...

  6. a标签点击后更改颜色

    function choose(id){ document.getElementById("typeid").value = id; //var infoa=document.ge ...

  7. ios 团购信息客户端demo(一)

    团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...

  8. 控制器生命周期方法(LifeCycle)

    1.init方法: 在init方法中实例化必要的对象(遵从LazyLoad思想) ‍init方法中初始化ViewController本身   2.loadView方法:    当view需要被展示而它 ...

  9. CF-1072-C. Cram Time(贪心,数学)

    CF-1072-C. Cram Time http://codeforces.com/contest/1072/problem/C 题意: 第一天有 a 小时,第二天有 b 小时.第 k 个任务需要 ...

  10. (53)zabbix模板

    zabbix模板是做什么的? 平时工作中,我们需要监控web.mysql.redis.nginx这些服务器,众多服务器的业务都是一样的,所以我们只要事先创建好模板,然后所有服务器链接这个模板即可,如果 ...