java-如何计算矩阵中最小和为[0,0]到[M,N]的路径?

前端之家收集整理的这篇文章主要介绍了java-如何计算矩阵中最小和为[0,0]到[M,N]的路径? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要使用矩阵的最小和求和来计算从[0,0]到[M,N]的路径?

我找到了这样的链接https://www.programcreek.com/2014/05/leetcode-minimum-path-sum-java/,但“动态编程”选项却根本不清楚.

我试图用BFS算法自己实现它,但这是一个缓慢的解决方

  1. public int minPathSum(final int[][] grid) {
  2. if (grid.length == 1 && grid[0].length == 1) {
  3. return grid[0][0];
  4. }
  5. final int[][] moves = {new int[]{1,0},new int[]{0,1}};
  6. final Queue<int[]> positions = new ArrayDeque<>();
  7. final Queue<Integer> sums = new ArrayDeque<>();
  8. positions.add(new int[]{0,0});
  9. sums.add(grid[0][0]);
  10. int minSum = Integer.MAX_VALUE;
  11. while (!positions.isEmpty()) {
  12. final int[] point = positions.poll();
  13. final int sum = sums.poll();
  14. for (final int[] move : moves) {
  15. final int x = point[0] + move[0];
  16. final int y = point[1] + move[1];
  17. if (x == grid.length - 1 && y == grid[0].length - 1) {
  18. minSum = Math.min(minSum,sum);
  19. } else if (x > -1 && y > -1 && x < grid.length && y < grid[0].length) {
  20. positions.add(new int[]{x,y});
  21. sums.add(sum + grid[x][y]);
  22. }
  23. }
  24. }
  25. return minSum + grid[grid.length - 1][grid[0].length - 1];
  26. }

您能否解释一下,如果可能的话,您将如何解决

最佳答案
我对如何实现广度优先搜索感到困惑,但是在这里却难以理解动态公式,对我来说,这似乎更简单:)

这几乎是经典的动态编程问题.除第一个单元外,到达第一个单元的solution [y] [x]最多具有两个前身:选项1和选项2.假定我们知道到达每个单元的最佳解决方案,我们将选择哪个边?显然,这两种选择中更好的一种!

如果M保留给定值,则形式上稍微正式一点:

  1. solution[0][0] = M[0][0]
  2. // only one choice along
  3. // the top horizontal and
  4. // left vertical
  5. solution[0][x] =
  6. M[0][x] + solution[0][x - 1]
  7. solution[y][0] =
  8. M[y][0] + solution[y - 1][0]
  9. // two choices otherwise:
  10. // the best of option 1 or 2
  11. solution[y][x] =
  12. M[y][x] + min(
  13. solution[y][x - 1],solution[y - 1][x]
  14. )

我们可以看到我们可以创建一个适当的例程,例如使用for循环,以“自下而上”的顺序访问我们的解决方案矩阵的单元格,因为每个单元格的值取决于我们已经计算出的一个或两个前任.

JavaScript代码

  1. function show(M){
  2. let str = '';
  3. for (let row of M)
  4. str += JSON.stringify(row) + '\n';
  5. console.log(str);
  6. }
  7. function f(M){
  8. console.log('Input:\n');
  9. show(M);
  10. let solution = new Array();
  11. for (let i=0; i<M.length; i++)
  12. solution.push(new Array(M[0].length).fill(Infinity));
  13. solution[0][0] = M[0][0];
  14. // only one choice along
  15. // the top horizontal and
  16. // left vertical
  17. for (let x=1; x<M[0].length; x++)
  18. solution[0][x] =
  19. M[0][x] + solution[0][x - 1];
  20. for (let y=1; y<M.length; y++)
  21. solution[y][0] =
  22. M[y][0] + solution[y - 1][0];
  23. console.log('Solution borders:\n');
  24. show(solution);
  25. // two choices otherwise:
  26. // the best of option 1 or 2
  27. for (let y=1; y<M.length; y++)
  28. for (let x=1; x<M[0].length; x++)
  29. solution[y][x] =
  30. M[y][x] + Math.min(
  31. solution[y][x - 1],solution[y - 1][x]
  32. );
  33. console.log('Full solution:\n');
  34. show(solution);
  35. return solution[M.length-1][M[0].length-1];
  36. }
  37. let arr = [];
  38. arr[0] = [0,7,-7];
  39. arr[1] = [6,-8];
  40. arr[2] = [1,2,0];
  41. console.log(f(arr));

猜你在找的Java相关文章