Giter VIP home page Giter VIP logo

Comments (3)

xiongcaihu avatar xiongcaihu commented on May 6, 2024 1
/**
 * 思路:
 * 每个容器有两个选择,比如:A,可以倒入B,或者倒入C
 * 同样,B可以倒入A,也可以倒入C
 * 那么每次就有8种可能
 * 
 * 每产生一种可能,顺着这种可能的结果,继续去遍历更多可能
 * 那么,将会产生很多解,这些解,有些会是重复的,这个时候需要记录,这里用cache
 * 
 * 然后,还有当前深度搜索的分支如果没有结束,会涉及到已经visited的结果,这些结果需要记录,用totalPath
 * 
 * @returns
 */
function findContainer() {
    var totalPath = new Set();
    var cache = new Map();
    var rel = splitH2O({
        c8: 8,
        c5: 0,
        c3: 0
    }, totalPath, cache);
    if (rel) {
        return Array.from(totalPath);
    }
    return false;
}

function splitH2O({
    c8,
    c5,
    c3
}, totalPath, cache) {
    var path = `${c8}${c5}${c3}`;
    if (totalPath.has(path)) {
        return false;
    }

    if (cache.has(path)) {
        return cache.get(path);
    }

    totalPath.add(path);
    if (c8 == 4 || c5 == 4 || c3 == 4) {
        return true;
    }

    var limits = [8, 5, 3];

    for (var i = 0; i < limits.length; i++) {
        for (var j = 0; j < limits.length; j++) {
            if (j == i) continue;
            var rel = moveH2oToOther(limits[i], limits[j], {
                c8,
                c5,
                c3
            });
            rel = splitH2O(rel, totalPath, cache);
            if (rel) return true;
        }
    }

    totalPath.delete(path);
    cache.set(path, false);
    return false;
}

function moveH2oToOther(limitsA, limitsB, all) {
    a = all[`c${limitsA}`];
    b = all[`c${limitsB}`];

    if (limitsB - b > a) {
        all[`c${limitsA}`] = 0;
        all[`c${limitsB}`] = b + a;
        return all;
    } else {
        all[`c${limitsA}`] = a - (limitsB - b);
        all[`c${limitsB}`] = limitsB;
        return all;
    }
}

from leetcode.

azl397985856 avatar azl397985856 commented on May 6, 2024
/**
 * 思路:
 * 每个容器有两个选择,比如:A,可以倒入B,或者倒入C
 * 同样,B可以倒入A,也可以倒入C
 * 那么每次就有8种可能
 * 
 * 每产生一种可能,顺着这种可能的结果,继续去遍历更多可能
 * 那么,将会产生很多解,这些解,有些会是重复的,这个时候需要记录,这里用cache
 * 
 * 然后,还有当前深度搜索的分支如果没有结束,会涉及到已经visited的结果,这些结果需要记录,用totalPath
 * 
 * @returns
 */
function findContainer() {
    var totalPath = new Set();
    var cache = new Map();
    var rel = splitH2O({
        c8: 8,
        c5: 0,
        c3: 0
    }, totalPath, cache);
    if (rel) {
        return Array.from(totalPath);
    }
    return false;
}

function splitH2O({
    c8,
    c5,
    c3
}, totalPath, cache) {
    var path = `${c8}${c5}${c3}`;
    if (totalPath.has(path)) {
        return false;
    }

    if (cache.has(path)) {
        return cache.get(path);
    }

    totalPath.add(path);
    if (c8 == 4 || c5 == 4 || c3 == 4) {
        return true;
    }

    var limits = [8, 5, 3];

    for (var i = 0; i < limits.length; i++) {
        for (var j = 0; j < limits.length; j++) {
            if (j == i) continue;
            var rel = moveH2oToOther(limits[i], limits[j], {
                c8,
                c5,
                c3
            });
            rel = splitH2O(rel, totalPath, cache);
            if (rel) return true;
        }
    }

    totalPath.delete(path);
    cache.set(path, false);
    return false;
}

function moveH2oToOther(limitsA, limitsB, all) {
    a = all[`c${limitsA}`];
    b = all[`c${limitsB}`];

    if (limitsB - b > a) {
        all[`c${limitsA}`] = 0;
        all[`c${limitsB}`] = b + a;
        return all;
    } else {
        all[`c${limitsA}`] = a - (limitsB - b);
        all[`c${limitsB}`] = limitsB;
        return all;
    }
}

你的代码真有特色

from leetcode.

lvguofeng1303 avatar lvguofeng1303 commented on May 6, 2024

认领

from leetcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.