Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 与日益增多的路径算法问题作斗争_Javascript_Algorithm - Fatal编程技术网

Javascript 与日益增多的路径算法问题作斗争

Javascript 与日益增多的路径算法问题作斗争,javascript,algorithm,Javascript,Algorithm,我正在努力找到一个很好的解决方案来解决一个问题,这个问题要求所有路径在遍历2D数组/矩阵时增加(而不是经典的“最长增加路径”) 问题是: Definitions • Path: a sequence of two or mere spaces for which ea. space is horizontally or vertically adjacent to the previous • Increasing path: a path for which each space has

我正在努力找到一个很好的解决方案来解决一个问题,这个问题要求所有路径在遍历2D数组/矩阵时增加(而不是经典的“最长增加路径”)

问题是:


Definitions 
• Path: a sequence of two or mere spaces for which ea. space is horizontally or vertically adjacent to the previous 
• Increasing path: a path for which each space has a greater value than the previous space. 

Example 1: 
[
   [5, 1],
   [2, 7]
]

There are 4 Increasing paths.
1 -> 5
1 -> 7
2 -> 5
2 -> 7

Example 2: 
[
   [0, 4, 3],
   [5, 8, 9],
   [5, 9, 9]
]

There are 4 Increasing paths.
0 -> 4
0 -> 4 -> 8
0 -> 4 -> 8
0 -> 4 -> 8 -> 9 
0 -> 5 
0 -> 5 -> 8
... and so on. 

我尝试过一些事情,但没有一件是我需要的。因为我不想让这看起来像是“为我回答家庭作业”,这里是我尝试过的代码(我知道它不会100%起作用,但这对我来说是一个好的开始,我不确定从那里走到哪里)

/*
这一尝试是我所做的
从最长的时间里收集,
所以这显然是无效的。
*/ 
功能(五){
设n=v.长度;
设m=v[0]。长度;
设mem=(新数组(n));
对于(设i=0;i一些提示:

将网格视为图形,节点是每个网格点,边形成在可以从
a
移动到
b

递增路径意味着从较小的值
a
到较大的值
b

  • 这确保了图形是有方向的。(不能返回同一条边)
  • 这样的路径a->b->c..->f您永远无法连接到路径中的任何较早点,因为这意味着
    a
    。这意味着图形没有循环
具有有向边且没有圈的图称为DAG(有向无环图)。许多图算法在这些图上容易得多,包括列出所有路径

您的任务只是编写一个DFS(深度优先搜索)并在每个网格点上启动它(最后过滤掉长度为0的路径)。

一些提示:

将网格视为图形,节点是每个网格点,边形成在可以从
a
移动到
b

递增路径意味着从较小的值
a
到较大的值
b

  • 这确保了图形是有方向的。(不能返回同一条边)
  • 这样的路径a->b->c..->f您永远无法连接到路径中的任何较早点,因为这意味着
    a
    。这意味着图形没有循环
具有有向边且没有圈的图称为DAG(有向无环图)。许多图算法在这些图上容易得多,包括列出所有路径


您的任务只是编写一个DFS(深度优先搜索)并在每个网格点上启动它(最后过滤掉长度为0的路径)。

以备不时之需。部分提示是一个不正确的问题,因此我遇到了太多的麻烦,直到我放弃,后来被告知错误

let increasingPaths;

const checkAdj = (grid, y, x) => {
  let current = grid[y][x]
  let above;
  let below;
  let right;
  let left;

  if (grid[y][x+1] != undefined)
    right = grid[y][x + 1];
  if (current < right) {
    increasingPaths++
    checkAdj(grid, y, x + 1);
  }

  if (grid[y+1] != undefined)
    below = grid[y+1][x];
  if (current < below) {
    increasingPaths++
    checkAdj(grid, y + 1, x);
  }
  
  if (grid[y][x-1] != undefined)
    left = grid[y][x-1];
  if (current < left) {
    increasingPaths++
    checkAdj(grid, y, x-1);
  }
  
  if (grid[y-1] != undefined)
    above = grid[y-1][x]
  if (current < above) {
    increasingPaths++
    checkAdj(grid, y-1, x);
  }
}

function findPaths(grid) {
  grid.forEach(row => console.log(row));
  
  for (let y = 0; y < grid.length; y++) {
    for (let x = 0; x < grid.length; x++) {
      checkAdj(grid, y, x);
    }
  }
  return increasingPaths
}

let递增路径;
常量检查调整=(网格,y,x)=>{
让电流=栅极[y][x]
让上面;
下面让我们看看;
让权利;
让我们离开;
如果(网格[y][x+1]!=未定义)
右=栅格[y][x+1];
如果(当前<右侧){
递增路径++
勾选调整(网格,y,x+1);
}
如果(网格[y+1]!=未定义)
下方=网格[y+1][x];
如果(电流<以下){
递增路径++
检查调整(网格,y+1,x);
}
如果(网格[y][x-1]!=未定义)
左=网格[y][x-1];
如果(电流<左){
递增路径++
检查调整(网格,y,x-1);
}
if(网格[y-1]!=未定义)
上方=网格[y-1][x]
如果(电流<以上){
递增路径++
检查调整(网格,y-1,x);
}
}
函数findpath(网格){
grid.forEach(row=>console.log(row));
for(设y=0;y
以备不时之需。提示的一部分是一个错误的问题,因此我遇到了太多的麻烦,直到我放弃,后来被告知该错误

let increasingPaths;

const checkAdj = (grid, y, x) => {
  let current = grid[y][x]
  let above;
  let below;
  let right;
  let left;

  if (grid[y][x+1] != undefined)
    right = grid[y][x + 1];
  if (current < right) {
    increasingPaths++
    checkAdj(grid, y, x + 1);
  }

  if (grid[y+1] != undefined)
    below = grid[y+1][x];
  if (current < below) {
    increasingPaths++
    checkAdj(grid, y + 1, x);
  }
  
  if (grid[y][x-1] != undefined)
    left = grid[y][x-1];
  if (current < left) {
    increasingPaths++
    checkAdj(grid, y, x-1);
  }
  
  if (grid[y-1] != undefined)
    above = grid[y-1][x]
  if (current < above) {
    increasingPaths++
    checkAdj(grid, y-1, x);
  }
}

function findPaths(grid) {
  grid.forEach(row => console.log(row));
  
  for (let y = 0; y < grid.length; y++) {
    for (let x = 0; x < grid.length; x++) {
      checkAdj(grid, y, x);
    }
  }
  return increasingPaths
}

let递增路径;
常量检查调整=(网格,y,x)=>{
让电流=栅极[y][x]
让上面;
下面让我们看看;
让权利;
让我们离开;
如果(网格[y][x+1]!=未定义)
右=栅格[y][x+1];
如果(当前<右侧){
递增路径++
勾选调整(网格,y,x+1);
}
如果(网格[y+1]!=未定义)
下方=网格[y+1][x];
如果(电流<以下){
递增路径++
检查调整(网格,y+1,x);
}
如果(网格[y][x-1]!=未定义)
左=网格[y][x-1];
如果(电流<左){
递增路径++
检查调整(网格,y,x-1);
}
if(网格[y-1]!=未定义)
上方=网格[y-1][x]
如果(电流<以上){
递增路径++
检查调整(网格,y-1,x);
}
}
函数findpath(网格){
grid.forEach(row=>console.log(row));
for(设y=0;y
祝福你的心。我真的很努力地把它想象成一个图表,在笔记本上写了8次,但都没能得到,因为我知道我遗漏了什么。这确实帮了我大忙。非常感谢!!:)祝福你的心。我真的很努力地把它想象成一个图表,在笔记本上写了8次,但就是没能得到它,因为我知道它遗漏了什么。这实际上帮助很大。非常感谢!!:)问题是列出路径还是计算路径?@PaulHankin count他们,我很抱歉。所以在这个例子中,第一条路径是1->5,1->7..以此类推,正确的返回输出应该是4。第二个例子,22。问题中有一个有趣的歧义。你是在通过坐标网格还是所有数字序列寻找所有路径rs是这样发现的?
8->9
是一个数字序列,但可以从中间向右移动或从中间向下移动。在一个大网格中,这些数字可能非常不同。问题是列出路径,还是计数路径?@PaulHankin计数路径,我很抱歉。因此,在本例中,第一条路径是1->5,1->7.以此类推,正确的返回输出应该是4.第二个例子,22.在