『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

  今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧,用心在自己的研究上。晚上级会开完也就八点多了,开始打打题,今天在HDU杭电的ACM集训题看到一个奇葩的题,前来献上。

今日推荐:

《全球风暴》 一部宇宙航空和地球气候片的良心佳作,后期特效建模都是特别杠杠的大片,不会让你失望的哟,我已经三刷了哈哈哈。这部片在爱奇艺有上线,有兴趣的朋友可以看看鸭。

爱奇艺链接:https://www.iqiyi.com/v_19rr7pl8vg.html

------------------------------------------------题目----------------------------------------------------------

Jugs

Problem 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

------------------------------------------------题目----------------------------------------------------------

(一) 原题大意:

    你有两个杯子,A、B;你只有三种操作,(1)清空任何一个杯子 (2)当被子是空的时候可以填满任何一个杯子  (3)将某一杯的水倒到另一杯中(不会溢出计算)直到B杯达到目标数Aim C。

    输入的A、B升数有要求,一定需要相邻互质。并且A小于B,且目标数Aim C要小于B即可。

    题目好像想象挺容易,手写也好像能解出来,但就是电脑老是犯傻逼。扔HDU的OJ上老是显示超时,我看了一下时间限制也很足够啊:

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    后来我发现坑在哪里了。

    这道题我居然在一个小学课本的趣味题发现的,真的是,现在的小学益智题怕是很多都能改成编程题了,而且改了编程题之后你还不一定解的出来哈哈哈。

(二) 题目分析:

    其实方法很简单,你们可以试一下下面这个步骤,是一定能得到结果的:

    (1)装满A

    (2)从A倒B

    (3)如果B满了清空

    基本以上三个步骤都能找打准确的结果。

    这就是经典的“灌水定理”,这里提一下,在下面我会引出这个定理,理论上倒水的步骤是不唯一的,所以我就太在意样例。

    然而在无数次交OJ的时候疯狂的WA超时,我终于从样例发现了不对劲。在其他OJ上是可以过的,但在HDU OJ好像并没有被智能处理化:

    如果目标数C小于A,必须从A开始倒满。如果目标数C大于A,则必须从B开始倒满。原因是为了寻求最短步骤操作,拿样例来说,3 5 4,如果先倒A,那么你需要八步,而如果先到B,那么你需要六步,所以这道题杭电OJ是默认要求最短步骤了,题目并没有说,所以害我 一股脑热直接从A循环交,就错了。

(三) 代码分块:

    首先:我先构造三个步骤出来:Fill、Pour、Empty

void pour(bool x)
{
    if(x)
    {
        if(cap_b + cap_a <= temp_b)
        {
            cap_b = cap_b + cap_a;
            cap_a = 0;
        }
        else
        {
            cap_a  = cap_a - (temp_b - cap_b);
            cap_b = temp_b;
        }
        printf("pour A B\n");
    }

    else
    {
        if(cap_b + cap_a <= temp_a)
        {
            cap_a = cap_b + cap_a;
            cap_b = 0;
        }
        else
        {
            cap_b  = cap_b - (temp_a - cap_a);
            cap_a = temp_a;
        }
        printf("pour B A\n");
    }
}
void fill(bool x)
{
    if(x)
    {
        cap_a = temp_a;
        printf("fill A\n");
    }
    else
    {
        cap_b = temp_b;
        printf("fill B\n");
    }
 }
 void empty(bool x)
{
    if(x)
    {
    cap_b = 0;
    printf("empty B\n");
    }
    else
    {
    cap_a = 0;
    printf("empty A\n");
    }
 }

  其中x为真是以A为主,为假时是B为主操作。难点应该在于Pour倾倒函数的书写,你需要区分从A倒到B时,是否B满了,如果满了就不能倒,A要留剩下,如果没满,就相当于把A清空了。这是要注意的地方。

  第二步:特殊处理

        if(aim == 0)
        {
            printf("success\n");
            continue;
        }
        if(temp_b == aim)
        {
            printf("fill B\n");
            printf("success\n");
            continue;
        }
        if(temp_a == aim)
        {
            printf("fill A\n");
            printf("pour A B\n");
            printf("success\n");
            continue;
        }

  这个就是我超时的原因之一了,因为我没有考虑到 3 5 3 或者是 3 5 5这种情况,当目标数直接等于其中一个杯子量时的操作,就会让程序一直循环操作得不到结果超时了。各位要注意。

  第三步:进行循环了

            if(temp_a >= aim)
            {
                if(cap_a == 0) fill(true);
                pour(true);
                if(cap_b == temp_b) empty(true);
            }
            else
            {
                if(cap_b == 0) fill(false);
                pour(false);
                if(cap_a == temp_a && cap_b != aim) empty(false);
            }

  这里就要提到我刚刚分析的时候说的最短步骤问题了,如果目标数C小于A,必须从A开始倒满。如果目标数C大于A,则必须从B开始倒满。就在这里体现,核心步骤其实很简单,就是刚刚的三步,填满、移动、清空已满。

  第四步:得到结果退出永真循环

            if(cap_a == aim)
            {
                if(cap_b != 0) printf("empty B\n");
                printf("pour A B\n");
                printf("success\n");
                break;
            }
            if(cap_b == aim)
            {
                printf("success\n");
                break;
            }

(四) AC代码:

#include<bits/stdc++.h>
using namespace std;
int temp_a,temp_b,aim;
int cap_a,cap_b;
void pour(bool x)
{
    if(x)
    {
        if(cap_b + cap_a <= temp_b)
        {
            cap_b = cap_b + cap_a;
            cap_a = 0;
        }
        else
        {
            cap_a  = cap_a - (temp_b - cap_b);
            cap_b = temp_b;
        }
        printf("pour A B\n");
    }

    else
    {
        if(cap_b + cap_a <= temp_a)
        {
            cap_a = cap_b + cap_a;
            cap_b = 0;
        }
        else
        {
            cap_b  = cap_b - (temp_a - cap_a);
            cap_a = temp_a;
        }
        printf("pour B A\n");
    }
}
void fill(bool x)
{
    if(x)
    {
        cap_a = temp_a;
        printf("fill A\n");
    }
    else
    {
        cap_b = temp_b;
        printf("fill B\n");
    }
 }
 void empty(bool x)
{
    if(x)
    {
    cap_b = 0;
    printf("empty B\n");
    }
    else
    {
    cap_a = 0;
    printf("empty A\n");
    }
 }
int main()
{
    while(~scanf("%d %d %d",&temp_a,&temp_b,&aim))
    {
        if(aim == 0)
        {
            printf("success\n");
            continue;
        }
        if(temp_b == aim)
        {
            printf("fill B\n");
            printf("success\n");
            continue;
        }
        if(temp_a == aim)
        {
            printf("fill A\n");
            printf("pour A B\n");
            printf("success\n");
            continue;
        }

        cap_a = cap_b = 0;//记得每个样例要清空杯子
        for(;;)
        {
            if(temp_a >= aim)
            {
                if(cap_a == 0) fill(true);
                pour(true);
                if(cap_b == temp_b) empty(true);
            }
            else
            {
                if(cap_b == 0) fill(false);
                pour(false);
                if(cap_a == temp_a && cap_b != aim) empty(false);
            }
            if(cap_a == aim)
            {
                if(cap_b != 0) printf("empty B\n");
                printf("pour A B\n");
                printf("success\n");
                break;
            }
            if(cap_b == aim)
            {
                printf("success\n");
                break;
            }
         }
    }
    return 0;
}

(五)AC截图:

(六) 解后分析:

    这道题如果不要求最短路径的话,其实就是非常简单的题目了,只要循环那三个步骤肯定能出结果,而且代码量直接大大减少。不过这道题解法我是比较直接的一种解法,在解后去网上找找别的解法,那就是还有一种就是用BFS(宽度优先搜索)

    代码贴上:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int MAXN=1050;
struct node{
    int a,b;
    node(int _a,int _b):a(_a),b(_b){}
};
int A,B,N;
int vis[MAXN][MAXN];
vector<int> path[MAXN][MAXN];
void op(int &a,int &b,int i){
    switch(i){
        case 1:{
            a=A;
            break;
        }
        case 2:{
            b=B;
            break;
        }
        case 3:{
            a=0;
            break;
        }
        case 4:{
            b=0;
            break;
        }
        case 5:{
            if(a+b>B){
                a=(a+b)-B;
                b=B;
            }
            else{
                b+=a;
                a=0;
            }
            break;
        }
        case 6:{
            if(b+a>A){
                b=(b+a)-A;
                a=A;
            }
            else{
                a+=b;
                b=0;
            }
            break;
        }
    }
}
void op_print(int i){
    switch(i){
        case 1:{
            printf("fill A\n");
            break;
        }
        case 2:{
            printf("fill B\n");
            break;
        }
        case 3:{
            printf("empty A\n");
            break;
        }
        case 4:{
            printf("empty B\n");
            break;
        }
        case 5:{
            printf("pour A B\n");
            break;
        }
        case 6:{
            printf("pour B A\n");
            break;
        }
    }
}
void bfs(){
    memset(vis,-1,sizeof(vis));
    for(int i=0;i<A;i++){
        for(int j=0;j<B;j++){
            path[i][j].clear();
        }
    }
    queue<node> que;
    que.push(node(0,0));
    vis[0][0]=0;
    while(!que.empty()){
        node tmp=que.front();
        que.pop();
        int ta=tmp.a;
        int tb=tmp.b;
        if(tb==N){
            for(int i=0;i<path[ta][tb].size();i++){
                op_print(path[ta][tb][i]);
            }
            printf("success\n");
            return;
        }
        for(int i=1;i<=6;i++){
            int ta=tmp.a;
            int tb=tmp.b;
            op(ta,tb,i);
            if(vis[ta][tb]==-1){
                vis[ta][tb]=vis[tmp.a][tmp.b]+1;
                for(int j=0;j<vis[tmp.a][tmp.b];j++){
                    path[ta][tb].push_back(path[tmp.a][tmp.b][j]);
                }
                path[ta][tb].push_back(i);
                que.push(node(ta,tb));
            }
        }
    }
}
int main(void){
    while(~scanf("%d%d%d",&A,&B,&N)){
        bfs();
    }
    return 0;
}

参考:https://blog.csdn.net/westbrook1998/article/details/80937164

  好了由于时间关系,先写在这,因为要睡觉了hhhh,明天满课,从早八点到晚九点半,要死,所以还是先早睡啦~

  (1)大数计算:加减乘除乘方开根问题

  (2)BFS搜索算法了解

  (3)灌水定理研究

  这是这周的任务了吧,这周解决这三个点!!~~

注:如果有更好的解法,真心希望您能够评论留言贴上您的代码呢~互相帮助互相鼓励才能成长鸭~~

原文地址:https://www.cnblogs.com/winniy/p/10428708.html

时间: 2024-08-01 23:18:50

『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)的相关文章

『ACM C++』HDU杭电OJ | 1425 - sort (排序函数的特殊应用)

今天真的是累哭了,周一课从早八点半一直上到晚九点半,整个人要虚脱的感觉,因为时间不太够鸭所以就回头看看找了一些比较有知识点的题来总结总结分析一下,明天有空了就开始继续打题,嘻嘻嘻. 今日兴趣电影: <超能查派> 这是一部关于未来人工智能的一个故事,感觉特别有思维开拓性,一个程序员写出了真正的AI智能机器人,可以从婴儿开始学习,然后以极快极强的学习速度不断成长,最后拯救身边人的故事.很感人,强烈推荐哈哈~ 爱奇艺:https://www.iqiyi.com/v_19rroly1wo.html?f

杭电OJ(HDU)-ACM Steps-Chapter Two-《Biker&#39;s Trip Odometer》《Climbing Worm》《hide handkerchief》《Nasty Hac》

1.2.1 Biker's Trip Odometer #include<stdio.h> #include<math.h> const double PI=acos(-1.0); /* 计算题,根据公式做就行,PI*d*r/(12*5280);res1/t*3600; Sample Input 26 1000 5 27.25 873234 3000 26 0 1000 Sample Output Trip #1: 1.29 928.20 Trip #2: 1179.86 1415

杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> /* 题意:找闰年. if((i%4==0 && i%100!=0) || i%400==0)count++; 3 2005 25 1855 12 2004 10000 2108 1904 43236 */ int main() { int t,y,n; int i,count=0; whil

杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse&amp;#39; Trade》《今年暑假不AC》《排名》《开门人和关门人》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3 1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h>

杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse&#39; Trade》《今年暑假不AC》《排名》《开门人和关门人》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3 1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h>

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电 OJ 提交代码需要注意的问题

杭电acm 提交代码需要注意的问题 1. 用 Java 的时候类名请用 Main 2. Java 提交出现 PE 的可能原因有 1) 最基本的错误是空格问题,比如注意每行的末尾是否输出空格 2) 用 Java 提交的时候需要注意换行是用的什么方法输出的,如果用 System.out.printf() 这个格式化输出,请使用 %n 或者 \r\n 作为转义符,而不要用 \n,也可以用 System.out.println() 输出换行 3. 对包含比较精确的数字计算最好使用 C/C++ 语言,对于

杭电oj 1009 FatMouse&#39; Trade

Tips:本题采用贪心算法,类似于背包问题,关键在于读入数据之后,将数据按 J[i]/F[i] 从大到小排列即可. 1 /**本程序主要采用贪心算法思想,类似于背包问题*/ 2 #include<stdio.h> 3 #include<string.h> 4 int main() 5 { 6 int M,N; 7 while(scanf("%d %d",&M,&N)) 8 { 9 if(M == -1 && N == -1) 10

异或^符号在C/C++中的使用 &amp; 杭电oj 2095

异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终结果. 特点:任何数和0异或都等于它本身;两个相同的数异或后的结果是0: 举例如下: int a = 4 =100(二进制) int b = 3 =011(二进制) int c = a^b = 111 = 7: 下面就^常用应用做个介绍: 1. 在一排数中找到独一无二的一个数 本例启发来自于杭电oj