代码之家  ›  专栏  ›  技术社区  ›  Tal Rofe

二维阵列N-1,N-1的最短路径

  •  0
  • Tal Rofe  · 技术社区  · 6 年前

    我试着解决一个问题。。但我不明白为什么我的算法会给数组的单元格分配新数组而不是整数。。

    enter image description here

    函数的输入是一个字符串

    这是我的尝试:

    function pathFinder(maze){
      maze = maze.split('\n').map(value => [...value]);
      function pF(i, j, steps){
        if (i === maze.length || j === maze.length || i === -1 ||
          j === -1 || maze[i][j] === 'W') return;
        else if (maze[i][j] === '.' || steps < maze[i][j]){
          maze[i][j] = steps;
          pF(i, j+1, steps + 1); pF(i-1, j, steps + 1);
          pF(i+1, j, steps + 1); pF(i, j-1, steps + 1);
        }
        else return;
       }
      pF(0, 0, 0);
      if (maze[maze.length - 1, maze.length - 1] === '.') return false;
      return maze[maze.length - 1, maze.length - 1];
    }
    

    由于某些原因我不明白,每个单元都被分配了数组 maze[i][j] = steps 声明。。

    0 回复  |  直到 6 年前