SPOJ Pouring Water

传送门

POUR1 - Pouring water

#gcd #recursion

Given two vessels, one of which can accommodate a litres of water and the other - b litres of water, determine the number of steps required to obtain exactly c litres of water in one of the vessels.

At the beginning both vessels are empty. The following operations are counted as ‘steps‘:

  • emptying a vessel,
  • filling a vessel,
  • pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.

Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by t sets of input data, each consisting of three positive integers a, b, c, not larger than 40000, given in separate lines.

Output

For each set of input data, output the minimum number of steps required to obtain c litres, or -1 if this is impossible.

Example

Sample input:

2
5
2
3
2
3
4

Sample output:

2
-1------------------------

SolutionBFS写BFS最重要的是避免同一状态重复入队。另外检查目标状态应该在每个状态出队时进行,因为状态的出口是“唯一”的,而入口一般有多种情况(即由队首状态一般可转移到多个新状态),注意代码中加粗的那行。另外由于问题中由初始状态可转移到的状态并不多(也由于二维数组开不下),应当用map存每个状态到初始状态的距离(及所需的最少转移步数)。还有一个技巧就是将enqueue写成一个函数,这样就避免了向多个状态转移带来的代码重复。
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;
int gcd(int a, int b){return b?gcd(b, a%b):a;}
map<P,int> mp;
queue<P> que;
void enque(int a, int b, int d){
    int tmp=mp[{a, b}];
    if(!tmp||tmp>d){
        mp[{a, b}]=d;
        que.push({a, b});
    }
}
bool ok(int a, int b, int c){return a==c||b==c;}
// how BFS works?
void bfs(int x, int y, int a, int b, int c){
    mp.clear();
    while(!que.empty()) que.pop();
    mp[{x, y}]=1;
    que.push({x, y});
    while(!que.empty()){
        P top=que.front(); que.pop(); int d=mp[top];
        int x=top.first, y=top.second;
        if(x==c||y==c){printf("%d\n", d-1); return;}
        if(x) enque(0, y, d+1);
        if(y) enque(x, 0, d+1);
        if(x!=a){
            enque(a, y, d+1);
            if(y){
                int add=min(y, a-x);
                 enque(x+add, y-add, d+1);
            }
        }
        if(y!=b){
            enque(x, b, d+1);
            if(x){
                int add=min(x, b-y);
                enque(x-add, y+add, d+1);
            }
        }
    }
    puts("-1");
}
int main(){
    int T; scanf("%d", &T);
    for(int a, b, c; T--;){
        scanf("%d%d%d", &a, &b, &c);
        if(c>max(a, b)){puts("-1"); continue;}
        if(c==a||c==b){puts("1"); continue;}
        if(a==b){puts("-1"); continue;}
        if(c%gcd(a,b)){puts("-1"); continue;}
        bfs(0, 0, a, b, c);
    }
}

----------------------------------------

写这题时把main()里的几个continue全写成return了,竟然过了样例,果然样例没有不坑的。以后debug时要留心有没有continue写成return的地方。

时间: 2024-11-05 16:38:06

SPOJ Pouring Water的相关文章

Sicily 1027 MJ,Nowhere to Hide

1027. MJ, Nowhere to Hide Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description On BBS, there is a familiar term called MJ (short for MaJia), which means another BBS ID of one person besides his/her main ID.These days, a lot of ACMers pour

《zw版&#183;Halcon-delphi系列原创教程》 Halcon分类函数006, image,影像处理(像素图)

<zw版·Halcon-delphi系列原创教程> Halcon分类函数006, image,影像处理(像素图) 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“procedure” :: 用大写字母“X”,替换:“IHUntypedObjectX” :: 省略了字符:“const”.“OleVariant” [示例] 说明 函数: procedure AddNoiseWhiteContourXld( const Contours: IHUntypedO

Meditation Guide

Meditation "Stop!!!" don't we just scream[vi. 尖叫:呼啸:发出尖锐刺耳的声音:令人触目惊心 ] this in our minds when the day has been very bad and all we think about is "work, problem, work, problem" over and over again? Sometimes we wish our minds would jus

What Is Mathematics?

What Is Mathematics? The National Council of Teachers of Mathematics (NCTM), the world's largest organization devoted to improving mathematics education, is developing a set of mathematics concepts, or standards, that are important for teaching and l

halcon算子

halcon的算子列表 Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训练数据上. 2.classify_class_gmm 功能:通过一个高斯混合模型来计算一个特征向量的类. 3. clear_all_class_gmm 功能:清除所有高斯混合模型. 4. clear_class_gmm 功能:清除一个高斯混合模型. 5. clear_samp

sicily 1027 MJ, Nowhere to Hide 字符串匹配与排序

1027. MJ, Nowhere to Hide Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description On BBS, there is a familiar term called MJ (short for MaJia), which means another BBS ID of one person besides his/her main ID.These days, a lot of ACMers pour

Halcon算子解释

Halcon算子解释大全 Halcon/Visionpro视频教程和资料,请访问 重码网,网址: http://www.211code.com Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训练数据上. 2.classify_class_gmm 功能:通过一个高斯混合模型来计算一个特征向量的类. 3. clear_all_class_gmm 功能

play scala 2 Water Pouring 问题的scala解法

给它一个初始状态,给它一些规则,推它一把,它就生生不息了. 问题描述 只有一只容量为4的杯子和一只容量为9的杯子,怎么才能取到正好6单位的水? 2.

zoj 2974 Just Pour the Water矩阵快速幂

Just Pour the Water Time Limit: 2 Seconds      Memory Limit: 65536 KB Shirly is a very clever girl. Now she has two containers (A and B), each with some water. Every minute, she pours half of the water in A into B, and simultaneous pours half of the