Algorithm for finding all paths in a NxN grid

前端 未结 10 1443
忘了有多久
忘了有多久 2020-11-28 08:27

Imagine a robot sitting on the upper left hand corner of an NxN grid. The robot can only move in two directions: right and down. How many possible paths are there for the ro

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 09:03

    This is for if the robot can go 4 directions rather than just 2, but the recursive solution below (in Javascript) works and I've tried to make it as legible as possible:

    //first make a function to create the board as an array of arrays
    var makeBoard = function(n) {
      var board = [];
      for (var i = 0; i < n; i++) {
        board.push([]);
        for (var j = 0; j < n; j++) {
          board[i].push(false);
        }
      }
      board.togglePiece = function(i, j) {
        this[i][j] = !this[i][j];
      }
      board.hasBeenVisited = function(i, j) {
        return !!this[i][j];
      }
      board.exists = function(i, j) {
        return i < n && i > -1 && j < n && j > -1;
      }
      board.viablePosition = function(i, j) {
        return board.exists(i, j) && !board.hasBeenVisited(i,j);
      }
      return board;
    };
    
    
    var robotPaths = function(n) {
      var numPaths = 0;
      //call our recursive function (defined below) with a blank board of nxn, with the starting position as (0, 0)
      traversePaths(makeBoard(n), 0, 0);
    
      //define the recursive function we'll use
      function traversePaths(board, i, j) {
        //BASE CASE: if reached (n - 1, n - 1), count as solution and stop doing work
        if (i === (n - 1) && j === (n - 1)) {
          numPaths++;
          return;
        }
        //mark the current position as having been visited. Doing this after the check for BASE CASE because you don't want to turn the target position (i.e. when you've found a solution) to true or else future paths will see it as an unviable position
        board.togglePiece(i, j);
    
        //RECURSIVE CASE: if next point is a viable position, go there and make the same decision
    
        //go right if possible
        if (board.viablePosition(i, j + 1)) {
          traversePaths(board, i, j + 1);
        }
    
        //go left if possible
        if (board.viablePosition(i, j - 1)) {
          traversePaths(board, i, j - 1);
        }
    
        //go down if possible
        if (board.viablePosition(i + 1, j)) {
          traversePaths(board, i + 1, j);
        }
    
        //go up if possible
        if (board.viablePosition(i - 1, j)) {
          traversePaths(board, i - 1, j);
        }
    
        //reset the board back to the way you found it after you've gone forward so that other paths can see it as a viable position for their routes
        board.togglePiece(i, j);
      }
      return numPaths;
    };
    

    A cleaner version:

    var robotPaths = function(n, board, i, j) {
        board = board || makeBoard(n),
        i = i || 0,
        j = j || 0;
    
        // If current cell has been visited on this path or doesn't exist, can't go there, so do nothing (no need to return since there are no more recursive calls below this)
        if (!board.viablePosition(i, j)) return 0;
        // If reached the end, add to numPaths and stop recursing
        if (i === (n - 1) && j === (n - 1)) return 1;
        // Mark current cell as having been visited for this path
        board.togglePiece(i, j);
        // Check each of the four possible directions
        var numPaths = robotPaths(n, board, i + 1, j) + robotPaths(n, board, i - 1, j) + robotPaths(n, board, i, j + 1) + robotPaths(n, board, i, j - 1);
        // Reset current cell so other paths can go there (since board is a pointer to an array that every path is accessing)
        board.togglePiece(i, j);
        return numPaths;
    }
    

    So:

    robotPaths(5); //returns 8512
    

提交回复
热议问题