zoukankan      html  css  js  c++  java
  • SPOJ Pouring Water

    传送门

    POUR1 - Pouring water

    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
    ------------------------

    Solution
    BFS
    写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
    ", 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的地方。



  • 相关阅读:
    洛谷P4768 [NOI2018]归程(可持久化并查集,最短路)
    FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)
    洛谷P2480 [SDOI2010]古代猪文(费马小定理,卢卡斯定理,中国剩余定理,线性筛)
    洛谷P4035 [JSOI2008]球形空间产生器(高斯消元)
    洛谷P2054 [AHOI2005]洗牌(扩展欧几里德)
    洛谷P3868 [TJOI2009]猜数字(中国剩余定理,扩展欧几里德)
    洛谷P1516 青蛙的约会(扩展欧几里德)
    Heaven of Imaginary(PKUSC2018)
    二进制高精度模板(高精度)
    洛谷UVA12995 Farey Sequence(欧拉函数,线性筛)
  • 原文地址:https://www.cnblogs.com/Patt/p/4824875.html
Copyright © 2011-2022 走看看