用隊列求解迷宮最短路徑及其應用(圍住神經貓)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

問題

給定一個M×N的迷宮圖,求一條從指定入口到出口的最短路徑.假設迷宮圖(M=8, N=8)


對於圖中的每個方塊,空白表示通道,陰影表示牆。所求路徑必須是簡單路徑,即在求得路徑上不能重複出現同一通道塊。
為了演算法方便,在迷宮外圍加了一道圍牆。
對應迷宮數組為:

var gameMap = [M + 2][N + 2]int{        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},        {1, 0, 0, 0, 0, 1, 1, 0, 0, 1},        {1, 0, 1, 1, 1, 0, 0, 0, 0, 1},        {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},        {1, 0, 1, 0, 0, 0, 1, 0, 0, 1},        {1, 0, 1, 1, 1, 0, 1, 1, 0, 1},        {1, 1, 0, 0, 0, 0, 0, 0, 0, 1},        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},    }

實現

go語言實現求解:

package mainimport (    "fmt")const (    M = 8    N = 8)// 方塊類型type Box struct {    i   int // 方塊行號    j   int // 方塊列號    pre int // 上一個方塊在隊列中位置}// 順序隊type Queue struct {    data  []Box    front int    rear  int}var (    gameMap = [M + 2][N + 2]int{        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},        {1, 0, 0, 0, 0, 1, 1, 0, 0, 1},        {1, 0, 1, 1, 1, 0, 0, 0, 0, 1},        {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},        {1, 0, 1, 0, 0, 0, 1, 0, 0, 1},        {1, 0, 1, 1, 1, 0, 1, 1, 0, 1},        {1, 1, 0, 0, 0, 0, 0, 0, 0, 1},        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},    })func gameSearch(xStart, yStart, xEnd, yEnd int) bool {    var i, j, di int    find := false    var queue Queue    queue.data = []Box{}    queue.front = -1    queue.rear = -1    queue.rear++    queue.data = append(queue.data, Box{})    queue.data[queue.rear].i = xStart    queue.data[queue.rear].j = yStart // (xStart, yStart)進隊    queue.data[queue.rear].pre = -1    gameMap[xStart][yStart] = -1    for queue.front != queue.rear && !find {        queue.front++        i = queue.data[queue.front].i        j = queue.data[queue.front].j        if i == xEnd && j == yEnd {            find = true            printPath(&queue, queue.front)            return true        }        // 順時針        for di = 0; di < 4; di++ {            switch di {            case 0:                i = queue.data[queue.front].i - 1                j = queue.data[queue.front].j            case 1:                i = queue.data[queue.front].i                j = queue.data[queue.front].j + 1            case 2:                i = queue.data[queue.front].i + 1                j = queue.data[queue.front].j            case 3:                i = queue.data[queue.front].i                j = queue.data[queue.front].j - 1            }            if gameMap[i][j] == 0 {                queue.rear++                queue.data = append(queue.data, Box{})                queue.data[queue.rear].i = i                queue.data[queue.rear].j = j                queue.data[queue.rear].pre = queue.front                gameMap[i][j] = -1            }        }    }    return false}func printPath(queue *Queue, front int) {    var k, j, ns = front, 0, 0    var maxSize = len(queue.data)    fmt.Println("\n")    for k != 0 {        j = k        k = queue.data[k].pre        queue.data[j].pre = -1    }    k = 0    fmt.Println("迷宮路徑如下:\n")    for k < maxSize {        if queue.data[k].pre == -1 {            ns++            fmt.Printf("\t(%d, %d)", queue.data[k].i, queue.data[k].j)            if ns%5 == 0 {                fmt.Println("\n")            }        }        k++    }}func main() {    gameSearch(1, 1, 8, 8)}

運行結果

應用

圍住神經貓

遊戲使用C#寫的,項目源碼
下載體驗

最後

附上我喜歡的歌的英文翻譯
心做し

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.