Skip to content

Latest commit

 

History

History
 
 

RobotPaths

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Robot Paths

Let’s say a robot is located at the top-left corner of an n x n grid. The robot can move either up, down, left, or right, one space at a time. It also cannot visit the same spot twice. The robot is trying to reach the bottom-right corner of the grid.

Write a function to count the number of ways that the robot can reach the bottom-right corner of the grid. Your function should receive n (the size of the grid) as a parameter.

A board can be generated using this function:

function makeBoard(n) {
  let board = [];
  for (let i = 0; i < n; i++) {
    board.push([]);
    for (let j = 0; j < n; j++) {
      board[i].push(false);
    }
  }
  board.toggle = (i, j) => {
    board[i][j] = !board[i][j];
  };
  board.hasBeenVisited = (i, j) => {
    return board[i][j];
  };
  return board;
}

Example

robotPaths(3) should create a 3 X 3 board, count the following paths, and return 12

S
1
23E
S
145
23E
S56
147
23E
S
123
E
S34
125
E
S
12
3E
S12
3
E
S12
43
5E
S12
543
67E
S1
2
3E
S1
32
45E
S1
23
E