尝试将切片作为参数传递给递归函数 (Go) 时,切片超出范围

我正在尝试将切片作为参数传递给递归函数。由于切片作为参考传递,我相信我传递给它的递归函数应该能够毫无问题地执行操作。我只使用 append() ,因此不应该对容量不足的切片有问题吗?


package main


import "fmt"


func allPossiblePaths(arrGraph [8][8]bool, src int, dest int) [][]int {

    var visited []bool //a slice that marks if visited

    var path []int     //a slice to store a possible path

    var paths [][]int  //a slice to store all paths


    visited = make([]bool, 8) //set all nodes to unvisited


    dfs(arrGraph, src, dest, visited, path, paths)


    return paths


}


func dfs(myGraph [8][8]bool, src int, dest int, visited []bool, path []int, paths [][]int) {

    //add current node to path

    path = append(path, src)


    //mark current node as visited

    visited[src] = true


    //if the current node is the destination

    //print the path and return

    if src == dest {


        //make a copy of path slice

        buffer := make([]int, len(path))

        copy(buffer, path)


        //append the copy of path slice into the slice of paths

        paths = append(paths, buffer)


        fmt.Println(path) //Just for debugging purpose

        return

    }


    for i := 1; i <= 7; i++ { //loop through all nodes


        //if ith node is a neighbour of the current node and it is not visited

        if myGraph[src][i] && visited[i] == false {


            // call dfs on the current node

            dfs(myGraph, i, dest, visited, path, paths)


            //mark the current node as unvisited

            //so that we can other paths to the final destination

            visited[i] = false


            //re-slice the slice - get rid of the current node

            path = path[:len(path)-1]

        }


    }


}

预期输出:(在使用全局变量而不是将变量传递给函数时实现)


[[1 2 3 4 6 7] [1 2 3 6 7] [1 2 5 6 7] [1 3 2 5 6 7] [1 3 4 6 7] [1 3 6 7] [1 6 7]]


知道我做错了什么吗?


叮当猫咪
浏览 186回答 2
2回答

小唯快跑啊

错误消息准确地说明了问题所在:恐慌:运行时错误:切片超出范围由于您正在迭代调用相同的函数并重新切片切片,因此您每次都需要检查是否达到切片容量范围,换句话说,如果切片指针指向有效地址(索引),否则您会收到out of range error消息.而且由于您正在进行递归迭代,因此每次通过减小路径长度,您必须检查切片索引是否在有效范围内。//re-slice the slice - get rid of the current nodeif len(path) > 0 {&nbsp; &nbsp; &nbsp; &nbsp;path = path[:len(path)-1]}https://play.golang.org/p/pX2TlAP-bp

尚方宝剑之说

我通过使用指向我的切片“路径”的指针作为递归函数的参数来解决它。现在可以了!不管怎么说,还是要谢谢你。package mainimport "fmt"func allPossiblePaths(arrGraph [8][8]bool, src int, dest int) [][]int {&nbsp; &nbsp; var visited []bool //a slice that marks if visited&nbsp; &nbsp; var path []int&nbsp; &nbsp; &nbsp;//a slice to store a possible path&nbsp; &nbsp; var paths [][]int&nbsp; //a slice to store all paths&nbsp; &nbsp; visited = make([]bool, 8) //set all nodes to unvisited&nbsp; &nbsp; dfs(arrGraph, src, dest, visited, &path, paths)&nbsp; &nbsp; return paths}func dfs(myGraph [8][8]bool, src int, dest int, visited []bool, path *[]int, paths [][]int) {&nbsp; &nbsp; //add current node to path&nbsp; &nbsp; *path = append(*path, src)&nbsp; &nbsp; //mark current node as visited&nbsp; &nbsp; visited[src] = true&nbsp; &nbsp; //if the current node is the destination&nbsp; &nbsp; //print the path and return&nbsp; &nbsp; if src == dest {&nbsp; &nbsp; &nbsp; &nbsp; //make a copy of path slice&nbsp; &nbsp; &nbsp; &nbsp; buffer := make([]int, len(*path))&nbsp; &nbsp; &nbsp; &nbsp; copy(buffer, *path)&nbsp; &nbsp; &nbsp; &nbsp; //append the copy of path slice into the slice of paths&nbsp; &nbsp; &nbsp; &nbsp; paths = append(paths, buffer)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(*path) //Just for debugging purpose&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; for i := 1; i <= 7; i++ { //loop through all nodes&nbsp; &nbsp; &nbsp; &nbsp; //if ith node is a neighbour of the current node and it is not visited&nbsp; &nbsp; &nbsp; &nbsp; if myGraph[src][i] && visited[i] == false {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // call dfs on the current node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dfs(myGraph, i, dest, visited, path, paths)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //mark the current node as unvisited&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //so that we can other paths to the final destination&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited[i] = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //re-slice the slice - get rid of the current node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *path = (*path)[:len(*path)-1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //fmt.Println(path)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; var myGraph [8][8]bool //the graph&nbsp; &nbsp; //creating the graph&nbsp; &nbsp; myGraph[1] = [...]bool{false, false, true, true, false, false, true, false}&nbsp; &nbsp; myGraph[2] = [...]bool{false, true, false, true, false, true, false, false}&nbsp; &nbsp; myGraph[3] = [...]bool{false, true, true, false, true, false, true, false}&nbsp; &nbsp; myGraph[4] = [...]bool{false, false, false, true, false, false, true, false}&nbsp; &nbsp; myGraph[5] = [...]bool{false, false, true, false, false, false, true, false}&nbsp; &nbsp; myGraph[6] = [...]bool{false, true, false, true, true, false, false, true}&nbsp; &nbsp; myGraph[7] = [...]bool{false, false, false, false, false, false, true, false}&nbsp; &nbsp; fmt.Println(allPossiblePaths(myGraph, 1, 7))}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go