Knight's Tour算法 - TypeError:无法设置undefined的属性

时间:2017-01-16 13:04:39

标签: javascript algorithm

我正试图从N. Wirth的“算法与数据结构”一书中实现Knight's Tour算法。原始版本是在Oberon写的: https://drive.google.com/file/d/0B7KbTu852Hi3cG5jYUVDX3Z4Yjg/view?usp=sharing

问题是我在行h[u][v] = i;收到错误“无法设置未定义属性'-1' 在步骤36,算法进入板的死胡同。帮助不大?

var n = 8; // size of the board
var nsqr = n * n;
// board matrix
var h = [];
for (var i = 0; i < n; i++) {
        h[i] = [];
}
var dx = [2, 1, -1, -2, -2, -1, 1, 2]; // coordinates difference along X, possible 8 moves
var dy = [1, 2, 2, 1, -1, -2, -2, -1]; // coordinates difference along Y, possible 8 moves

// knight's tour
function knightsTour(xin, yin) {
    clear();
    h[xin][yin] = 1; // initial position and step number
    var done = false;
    tryNextMove(xin, yin, 1, done);
}
// clear
function clear() {
    for (var i = 0; i < n; i++) {
        for (var j = 0; j < n; j++) {
            h[i][j] = 0;
        }
    }
}
// try next move
function tryNextMove(x, y, i, done) {
    var eos; // end of steps, bool
    var u, v; // new step coordinates
    var k; // new step index

    function next(eos) {
        do {
            k++;
            if (k < 8) {
                u = x + dx[k];
                v = y + dy[k];
            }
        } while (k < 8 && (u < 0 || u >= n || v < 0 || v >= n || h[u][v] != 0));
        eos = (k == 8);
    }

    function first(eos) {
        eos = false;
        k = -1;
        next(eos);
    }

    if (i < nsqr) {
        first(eos);
        while (!eos && !canBeDone(u, v, i+1)) {
        next(eos);
    }
    done = !eos;
    } else {
        done = true;
    }
}
// can tour be done
function canBeDone(u, v, i) {
    var done = false; // bool
    h[u][v] = i; // ERROR here
    tryNextMove(u, v, i, done);
    if (!done) { h[u][v] = 0; }
    return done;
}

knightsTour(3, 1);
console.log(h);

此版本适用于n = 4 ... 9:

function knightsTour(x0, y0, size = 8) {
    var done = false;
    var h = []; // chess board matrix
    for (var i = 0; i < size; i++) {
        h[i] = [];
    }
    var nsqr = size * size;

    // initializing the board
    for (var i = 0; i < size; i++) {
        for (var j = 0; j < size; j++) {
            h[i][j] = 0;
        }
    }

    // coordinates difference along X and Y, possible 8 moves
    var dx = [2, 1, -1, -2, -2, -1, 1, 2];
    var dy = [1, 2, 2, 1, -1, -2, -2, -1];

    h[x0][y0] = 1; // initial position and step number

    // check if move can be done
    function canBeDone(u, v, i) {
        h[u][v] = i;
        done = tryNextMove(u, v, i);
        if (!done) {
            h[u][v] = 0;
        }
        return done;
    }

    // try next move
    function tryNextMove(x, y, i) {
        var done;
        // u, v - new step coordinates; k - new step index; eos - end of steps, bool
        var env = {"done": false, "eos": false, "u": x, "v": y, "k": -1};

        function next() {
            x = env.u;
            y = env.v;
            while (env.k < 8) {
                env.k += 1;
                if (env.k < 8) {
                    env.u = x + dx[env.k];
                    env.v = y + dy[env.k];
                }
                if ((env.u >= 0 && env.u < size) && (env.v >= 0 && env.v < size) && h[env.u][env.v] == 0) {
                    break;
                }
            }
            env.eos = (env.k == 8);
        }

        if (i < nsqr) {
            next();
            while (!env.eos && !canBeDone(env.u, env.v, i+1)) {
                next();
            }
            done = !env.eos;
        } else {
            done = true;
        }
        return done;
    }

    tryNextMove(x0, y0, 1);
    //console.log(h);
    return h;
}

1 个答案:

答案 0 :(得分:0)

要检查的第一件事是您如何确定vfirst()的范围。

检查此代码。

next()u中,您将vu作为参数。但这意味着该函数与您v中引用的函数具有canBeDoneu=9的不同副本。

现在问题是算法在var n = 8; // size of the board var nsqr = n * n; // board matrix var h = []; for (var i = 0; i < n; i++) { h[i] = []; } var dx = [2, 1, -1, -2, -2, -1, 1, 2]; // coordinates difference along X, possible 8 moves var dy = [1, 2, 2, 1, -1, -2, -2, -1]; // coordinates difference along Y, possible 8 moves // knight's tour function knightsTour(xin, yin) { clear(); h[xin][yin] = 1; // initial position and step number var done = false; tryNextMove(xin, yin, 1, done); } // clear function clear() { for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { h[i][j] = 0; } } } // try next move function tryNextMove(x, y, i, done) { var eos; // end of steps, bool var u, v; // new step coordinates var k; // new step index function next(eos) { do { k++; if (k < 8) { uNew = x + dx[k]; vNew = y + dy[k]; } } while (k < 8 || (u < 0 && u >= n && v < 0 && v >= n && (h[u][v] != 0))); eos = (k == 8); } function first(eos) { eos = false; k = -1; next(eos, u, v); } if (i < nsqr) { first(eos); while (!eos && !canBeDone(u, v, i+1)) { next(eos); } done = !eos; } else { done = true; } } // can tour be done function canBeDone(u, v, i) { var done = false; // bool h[u][v] = i; // ERROR here tryNextMove(u, v, i, done); if (!done) { h[u][v] = 0; } return done; } knightsTour(3, 1); console.log(h); 时失败了。这是进一步调试的起点。

//first, create a Blob object. Assume that variable myCSV has our CSV data as a string
//The Blob constructor requires an array, so place in brackets [].
var myblob = new Blob([myCSV], {type: 'text/csv'});

//Create a new FormData object.
var myFD = new FormData();
//Add our file (the blob) to it.
myFD.append('htmlFormName', myblob, 'someFileName.csv');
//We can also append other fields if necessary.
myFD.append('inputName', 'inputValue');

//If we're using jQuery to send the form...
$.ajax({
    url: 'http://remotewebsite.com/formhandler.php',
    type: 'POST',
    data: myFD,
    processData: false, // <-- important!
    contentType: false, // <-- important!
    success: function(data) {
        console.log('Posted successfully.');
    }
});