Skip to content

Instantly share code, notes, and snippets.

@grevych
Created March 1, 2019 23:58
Show Gist options
  • Save grevych/4e97fc380a89a7b5c3bc0460a446555b to your computer and use it in GitHub Desktop.
Save grevych/4e97fc380a89a7b5c3bc0460a446555b to your computer and use it in GitHub Desktop.
Have the function ChessboardTraveling(x, y, a, b) read str which will be a string consisting of the location of a space on a standard 8x8 chess board with no pieces on the board along with another space on the chess board. The structure of str will be the following: "(x y)(a b)" where (x y) represents the position you are currently on with x and…
/*
Example:
(1, 1) -> (1, 2) -> (2, 2)
(1, 1) -> (2, 1) -> (2, 2)
*/
function chessboardTraveling(x, y, a, b) {
let count = 0;
const queue = [];
queue.push([x, y]);
while (queue.length > 0) {
const currentPosition = queue.shift()
const [ currentX, currentY ] = currentPosition;
if (currentX == a && currentY == b) {
count++;
continue;
}
if (currentX + 1 <= a) {
queue.push([currentX + 1, currentY]);
}
if (currentY + 1 <= b) {
queue.push([currentX, currentY + 1]);
}
}
return count;
}
console.log(chessboardTraveling(1, 1, 3, 3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment