leetcode365-水壶问题

原题

有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?

如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。

你允许:

  • 装满任意一个水壶
  • 清空任意一个水壶
  • 从一个水壶向另外一个水壶倒水,直到装满或者倒空

示例 1:(From the famous "Die Hard" example)

输入: x = 3, y = 5, z = 4
输出: True

示例 2:

输入: x = 2, y = 6, z = 5
输出: False

解法

思想

DFS/BFS

在任意一个时刻,我们可以且仅可以采取以下几种操作:

  • 把 X 壶的水灌进 Y 壶,直至灌满或倒空;
  • 把 Y 壶的水灌进 X 壶,直至灌满或倒空;
  • 把 X 壶灌满;
  • 把 Y 壶灌满;
  • 把 X 壶倒空;
  • 把 Y 壶倒空。

可以使用BFS或DFS,然而Java没有类似Pair的数据结构,可以自己写一个类出来。

数学

另一个方法是数学定理 裴蜀定理_百度百科

若 a,b 为整数,且 gcd(a,b) = d,则对于任意的整数x,y,ax + by都一定是 d 的倍数。 特别地,一定存在整数 x,y,使得 ax + by = d 成立。

代码

BFS(有时间回来改写)

class Solution {
    public boolean canMeasureWater(int x, int y, int z) {
        if (x + y < z) {
            return false;
        }
        if (x == 0 || y == 0) {
            return x == z || y == z;
        }
        Queue<List<Integer>> queue = new LinkedList<>();
        Set<List<Integer>> set = new HashSet<>();
        List<Integer> list = Arrays.asList(0, 0);
        queue.offer(list);
        set.add(list);

        while (!queue.isEmpty()) {
            List<Integer> temp = queue.poll();
            int cur_x = temp.get(0);
            int cur_y = temp.get(1);
            if (z == cur_x || z == cur_y || z == cur_x + cur_y) {
                return true;
            }
            //1、给x加满水
            List<Integer> condition1 = Arrays.asList(x, cur_y);
            if (!set.contains(condition1)) {
                set.add(condition1);
                queue.offer(condition1);
            }
            //2、给y加满水
            List<Integer> condition2 = Arrays.asList(cur_x, y);
            if (!set.contains(condition2)) {
                set.add(condition2);
                queue.offer(condition2);
            }
            //3、清空x的水
            List<Integer> condition3 = Arrays.asList(0, cur_y);
            if (!set.contains(condition3)) {
                set.add(condition3);
                queue.offer(condition3);
            }
            //4、清空y的水
            List<Integer> condition4 = Arrays.asList(cur_x, 0);
            if (!set.contains(condition4)) {
                set.add(condition4);
                queue.offer(condition4);
            }
            //5、x给y倒水
            List<Integer> condition5 = (cur_x + cur_y >= y) ?
                    Arrays.asList(cur_x + cur_y - y, y) :
                    Arrays.asList(0, cur_x + cur_y);
            if (!set.contains(condition5)) {
                set.add(condition5);
                queue.offer(condition5);
            }
            //6、y给x倒水
            List<Integer> condition6 = (cur_x + cur_y >= x) ?
                    Arrays.asList(x, cur_x + cur_y - x) :
                    Arrays.asList(cur_x + cur_y, 0);
            if (!set.contains(condition6)) {
                set.add(condition6);
                queue.offer(condition6);
            }
        }
        return false;
    }
}

裴蜀定理:

class Solution {
    public boolean canMeasureWater(int x, int y, int z) {
        if (x + y < z) return false;
        if (x == z || y == z || x + y == z) return true;
        return (z % gcd(x, y) == 0);
    }

    public int gcd(int x,int y){
        if(y == 0) return x;
        int r = x % y;
        return gcd(y,r);
    }
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode365-%e6%b0%b4%e5%a3%b6%e9%97%ae%e9%a2%98/

发表回复

登录后才能评论