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()); knightChains(n); } const knightMoves = function(boardSize) { const moves = []; for (let i = 1; i < boardSize; i++) { for (let j = 1; j < boardSize; j++){ moves.push([i,j]); } } return moves; }; const nextMoves = function(initialPos, vector){ nextPositions = []; const multipliers = [ [1, 1], [-1,1], [1,-1], [-1,-1] ]; const vectors = []; multipliers.forEach(function(multiplier) { vectors.push([multiplier[0] * vector[0], multiplier[1] * vector[1]]); vectors.push([multiplier[0] * vector[1], multiplier[1] * vector[0]]); }); vectors.forEach(function(vec) { let nextPos = [initialPos[0] + vec[0], initialPos[1] + vec[1]]; if (nextPos[0] >= 0 && nextPos[1] >= 0) { nextPositions.push(nextPos); } }); return nextPositions; }; const knightChains = function(boardSize) { const numberOfMoves = []; const knights = knightMoves(boardSize); const goal = boardSize - 1; const stepCounts = []; for (let i = 0; i < knights.length; i++){ stepCounts.push(pathFind(knights[i], goal)); } const strArr = []; let subArr = []; for (let i = 0; i < stepCounts.length; i++) { subArr.push(stepCounts[i]); if (subArr.length === goal){ strArr.push(subArr.join(" ")); subArr = []; } } for (let i = 0; i < strArr.length; i++){ console.log(strArr[i]); } }; const pathFind = function(knight, goal){ const frontier = [[0,0]]; let visited = {}; visited[[0,0]] = true; while (frontier.length > 0 && (visited[[goal, goal]] === undefined)) { let current = frontier.shift(); let neighbors = nextMoves(current, knight); neighbors.forEach(function(coord){ if (visited[coord] === undefined && coord[0] <= goal && coord[1] <= goal) { frontier.push(coord); visited[coord] = current; } }); } if (visited[[goal, goal]] === undefined){ return -1; } else { let pathLength = findPathLength(visited, [goal, goal]); return pathLength; } }; const findPathLength = function(pathObject, destination){ let current = pathObject[destination]; let steps = 1; while (pathObject[current] !== true){ current = pathObject[current]; steps += 1; } return steps; };