Last active
November 23, 2020 16:57
-
-
Save Zorgatone/3513b13ae667deadccbbccbe1511f55f to your computer and use it in GitHub Desktop.
Chess knight moves interview question solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 | |
7 | |
15 | |
67 | |
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat input.txt | node test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const fs = require("fs"); | |
process.stdin.resume(); | |
process.stdin.setEncoding("utf-8"); | |
let inputString = fs | |
.readFileSync("./input.txt", { encoding: "utf-8" }) | |
.toString(); | |
let currentLine = 0; | |
inputString = inputString.split("\n"); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
/* | |
* Complete the 'minMoves' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts following parameters: | |
* 1. INTEGER n | |
* 2. INTEGER startRow | |
* 3. INTEGER startCol | |
* 4. INTEGER endRow | |
* 5. INTEGER endCol | |
*/ | |
const moves = [ | |
[1, -2], | |
[2, -1], | |
[2, 1], | |
[1, 2], | |
[-1, 2], | |
[-2, 1], | |
[-2, -1], | |
[-1, -2], | |
]; | |
main(); | |
function minMovesMatrix(matrix, nSteps, n, endRow, endCol) { | |
let results = []; | |
for (let i = 0; i < n; i += 1) { | |
for (let j = 0; j < n; j += 1) { | |
if (matrix[i][j] === nSteps) { | |
const filteredMoves = moves | |
.map(([row, col]) => [i + row, j + col]) | |
.filter( | |
([newRow, newCol]) => | |
newRow >= 0 && newRow < n && newCol >= 0 && newCol < n | |
); | |
if (filteredMoves.length === 0) { | |
if ( | |
filteredMoves.filter( | |
([newRow, newCol]) => | |
typeof matrix[newRow][newCol] === "undefined" | |
).length < 1 | |
) { | |
return -1; | |
} | |
} | |
const len = filteredMoves.length; | |
for (let k = 0; k < len; k += 1) { | |
const [newRow, newCol] = filteredMoves[k]; | |
if (newRow === endRow && newCol === endCol) { | |
return nSteps + 1; | |
} | |
if (typeof matrix[newRow][newCol] === "undefined") { | |
matrix[newRow][newCol] = nSteps + 1; | |
} | |
} | |
} | |
} | |
} | |
return minMovesMatrix(matrix, nSteps + 1, n, endRow, endCol); | |
} | |
function minMoves(n, startRow, startCol, endRow, endCol) { | |
if (n < 5 || n > 150) { | |
return -1; | |
} | |
if (startRow < 0 || startCol < 0 || startRow >= n || startCol >= n) { | |
return -1; | |
} | |
if (endRow < 0 || endCol < 0 || endRow >= n || endCol >= n) { | |
return -1; | |
} | |
if (startRow === endRow && startCol === endCol) { | |
return 0; | |
} | |
const matrix = new Array(n).fill([]).map(() => new Array(n).fill(undefined)); | |
const nSteps = 0; | |
matrix[startRow][startCol] = nSteps; | |
return minMovesMatrix(matrix, nSteps, n, endRow, endCol); | |
} | |
function main() { | |
const n = parseInt(readLine().trim(), 10); | |
const startRow = parseInt(readLine().trim(), 10); | |
const startCol = parseInt(readLine().trim(), 10); | |
const endRow = parseInt(readLine().trim(), 10); | |
const endCol = parseInt(readLine().trim(), 10); | |
const result = minMoves(n, startRow, startCol, endRow, endCol); | |
console.log(result); | |
process.exit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment