process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var n = parseInt(readLine()); // your code goes here var positions = Array(n).fill(0).map(function () { return []; }); function position(x, y) { var isValid = x < n && y < n && !(x < 0 || y < 0); var isLast = x == y && y == n - 1; if (isValid) { return positions[x][y] || (positions[x][y] = { x: x, y: y, moves: moves, move: move, isLast: last, isValid: valid, toString: toString }); } else { return { x: x, y: y, moves: moves, move: move, isLast: last, isValid: valid, toString: toString }; } function toString() { return '(' + x + ',' + y + ')'; } function valid() { return isValid; } function last() { return isLast; } function move(i, j) { return position(x + i, y + j); } function validate(pos) { return pos.isValid(); } function moves(i, j) { return [ move(-i, -j), move(-i, +j), move(+i, -j), move(+i, +j), move(-j, -i), move(-j, +i), move(+j, -i), move(+j, +i) ].filter(validate); } } function knight(i, j) { var startPos = position(0, 0); var paths = [[startPos]]; var hist = [startPos]; function move(path) { return path.slice(-1)[0] .moves(i, j) .filter(function (pos) { return hist.indexOf(pos) == -1; }).map(function (pos) { hist.push(pos); var arr = path.slice(); arr.push(pos); return arr; }); } function flatten(a, b) { return a.concat(b); } function isEnd(path) { return path.slice(-1)[0].isLast(); } while (paths.length > 0) { paths = paths.map(move) .reduce(flatten); if (paths.some(isEnd)) { return paths[0].length - 1; } } return -1; } var moves = Array(n).fill(0).map(function () { return []; }); for (var i = 1; i < n; i += 1) { for (var j = i; j < n; j += 1) { moves[i - 1][j - 1] = knight(i, j); if (i != j) { moves[j - 1][i - 1] = moves[i - 1][j - 1]; } } } for (i = 0; i < n; i += 1) { console.log(moves[i].join(' ')); } }