mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 12:46:31 +08:00
57cf6b1ea6
Some checks failed
Dart / Dart stable on macos-latest (push) Has been cancelled
Dart / Dart stable on ubuntu-latest (push) Has been cancelled
Dart / Dart stable on windows-latest (push) Has been cancelled
.NET / .NET 8.0.x on macos-latest (push) Has been cancelled
.NET / .NET 8.0.x on ubuntu-latest (push) Has been cancelled
.NET / .NET 8.0.x on windows-latest (push) Has been cancelled
Go / Go 1.19.x on macos-latest (push) Has been cancelled
Go / Go 1.19.x on ubuntu-latest (push) Has been cancelled
Go / Go 1.19.x on windows-latest (push) Has been cancelled
Python / Python 3.10 on macos-latest (push) Has been cancelled
Python / Python 3.11 on macos-latest (push) Has been cancelled
Python / Python 3.10 on ubuntu-latest (push) Has been cancelled
Python / Python 3.11 on ubuntu-latest (push) Has been cancelled
Python / Python 3.10 on windows-latest (push) Has been cancelled
Python / Python 3.11 on windows-latest (push) Has been cancelled
Ruby / Ruby 3.3 on macos-latest (push) Has been cancelled
Ruby / Ruby 3.3 on ubuntu-latest (push) Has been cancelled
Ruby / Ruby 3.3 on windows-latest (push) Has been cancelled
Rust / build (macos-latest) (push) Has been cancelled
Rust / build (ubuntu-latest) (push) Has been cancelled
Rust / build (windows-latest) (push) Has been cancelled
Swift / Swift on macos-14 (push) Has been cancelled
Swift / Swift on ubuntu-22.04 (push) Has been cancelled
43 lines
968 B
Go
43 lines
968 B
Go
// File: min_path_sum_test.go
|
|
// Created Time: 2023-07-23
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_dynamic_programming
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestMinPathSum(t *testing.T) {
|
|
grid := [][]int{
|
|
{1, 3, 1, 5},
|
|
{2, 2, 4, 2},
|
|
{5, 3, 2, 1},
|
|
{4, 3, 5, 2},
|
|
}
|
|
n, m := len(grid), len(grid[0])
|
|
|
|
// 暴力搜索
|
|
res := minPathSumDFS(grid, n-1, m-1)
|
|
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
|
|
|
|
// 记忆化搜索
|
|
mem := make([][]int, n)
|
|
for i := 0; i < n; i++ {
|
|
mem[i] = make([]int, m)
|
|
for j := 0; j < m; j++ {
|
|
mem[i][j] = -1
|
|
}
|
|
}
|
|
res = minPathSumDFSMem(grid, mem, n-1, m-1)
|
|
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
|
|
|
|
// 动态规划
|
|
res = minPathSumDP(grid)
|
|
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
|
|
|
|
// 空间优化后的动态规划
|
|
res = minPathSumDPComp(grid)
|
|
fmt.Printf("从左上角到右下角的最小路径和为 %d\n", res)
|
|
}
|