go最小路径

package main

import (
    "fmt"
    "flag"
    "math/rand"
    "time"
    )

var i int
var j int
var path [][]int
var value [][]int

func init() {
    flag.IntVar(&i, "i", 10, "The greeting object.")
    flag.IntVar(&j, "j", 10, "The greeting object.")
}

func main() {
    flag.Parse()
    initArray()
    fmt.Printf("初始化数组为:%v \n", "")
    for a := 0; a < i; a++ {
        fmt.Printf("%v \n", path[a])
    }
    walk()
    fmt.Printf("路径权值为:%v \n", "")
    for a := 0; a < i; a++ {
        fmt.Printf("%v \n", value[a])
    }
    fmt.Printf("最小路径为:%v \n", value[0][0])
}

// 计算路径权值
func walk() {
    value[i-1][j-1] = path[i-1][j-1]
    for a := j-2; a >= 0; a-- {
        value[i-1][a] = value[i-1][a+1] + path[i-1][a]
    }
    for a := i-2; a >= 0; a-- {
        value[a][j-1] = value[a+1][j-1] + path[a][j-1]
    }
    for a := i-2; a >= 0; a-- {
        for b := j-2; b >= 0; b-- {
            minA := value[a+1][b] + path[a][b]
            minB := value[a][b+1] + path[a][b]
            value[a][b] = min(minA,minB)
        }
    }
}

// 比较两个数大小,返回较小的
func min(a int,b int)int{
    if(a < b){
        return a
    }else{
        return b
    }
}

// 随机生成初始化数组
func initArray(){
    rand.Seed(time.Now().UnixNano())
    for a := 0; a < i; a++ {
        arrayA := []int{}
        arrayB := []int{}
        for b := 0; b < j; b++ {
            arrayA = append(arrayA,rand.Intn(10))
            arrayB = append(arrayB,0)
        }
        path = append(path,arrayA)
        value = append(value,arrayB)
    }
}
12-26 19:39