日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Python利用Prim算法生成迷宮_python

作者:Leleprogrammer ? 更新時(shí)間: 2023-02-14 編程語(yǔ)言

Prim算法隨機(jī)生成后的迷宮數(shù)列矩陣如下圖:

15x15:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0?
0 s 0 1 0 1 0 1 1 1 1 1 0 1 0?
0 1 0 1 0 1 0 0 0 1 0 0 0 1 0?
0 1 1 1 1 1 1 1 1 1 1 1 0 1 0?
0 1 0 0 0 1 0 0 0 1 0 0 0 1 0?
0 1 0 1 1 1 0 1 1 1 0 1 0 1 0?
0 0 0 0 0 0 0 0 0 1 0 1 0 1 0?
0 1 1 1 1 1 1 1 1 1 1 1 1 1 0?
0 1 0 1 0 0 0 1 0 0 0 1 0 0 0?
0 1 0 1 0 1 1 1 0 1 1 1 1 1 0?
0 1 0 1 0 0 0 1 0 0 0 1 0 1 0?
0 1 0 1 0 1 1 1 1 1 0 1 0 1 0?
0 0 0 1 0 0 0 1 0 0 0 0 0 1 0?
0 1 1 1 0 1 1 1 1 1 1 e 0 1 0?
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0?

27x27:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0?
0 s 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0?
0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0?
0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0?
0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0?
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 1 0?
0 1 0 0 0 0 0 0 0 0 0 0 0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1 1 e 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 0?
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0?

實(shí)現(xiàn)該效果的代碼如下:?

import random as rd
 
nearmaybe=[
    [-2,0],
    [2,0],
    [0,-2],
    [0,2]
]
 
def createMaze(row,col):
    maze=[[0 for i in range(col)] for i in range(row)]
    check=[]
    firstrow=rd.randrange(1,row-2,2)
    firstcol=rd.randrange(1,col-2,2)
    maze[firstrow][firstcol]=1
    check.append([firstrow,firstcol])
    while len(check):
        c=rd.choice(check)
        nears=[]
        conditions=[]
        for maybe in nearmaybe:
            conditions.append([c[0]+maybe[0],c[1]+maybe[1]])
        for condition in conditions:
            if condition[0]>=1 and condition[0]<=row-2 \
                    and condition[1]>=1 and condition[1]<=col-2:
                nears.append([condition[0],condition[1]])
        for n in nears.copy():
            if maze[n[0]][n[1]]:
                nears.remove(n)
        for block in nears:
            if block[0]==c[0]:
                if block[1]<c[1]:
                    maze[block[0]][c[1]-1]=1
                    maze[block[0]][block[1]]=1
                    check.append([block[0],block[1]])
                else:
                    maze[block[0]][block[1]-1]=1
                    maze[block[0]][block[1]]=1
                    check.append([block[0],block[1]])
            else:
                if block[0]<c[0]:
                    maze[c[0]-1][block[1]]=1
                    maze[block[0]][block[1]]=1
                    check.append([block[0],block[1]])
                else:
                    maze[block[0]-1][block[1]]=1
                    maze[block[0]][block[1]]=1
                    check.append([block[0],block[1]])
        if not len(nears):
            check.remove(c)
    maze[1][1]="s"
    while True:
        c=rd.randint(1,col-2)
        if maze[row-2][c]:
            maze[row-2][c]="e"
            break
 
    return maze

調(diào)用該函數(shù)的方法:

maze=createMaze(27,27)
for l in maze:
    for s in l:
        print(s,end=" ")
    print()

CreateMaze傳入的兩個(gè)參數(shù)必須都是單數(shù),否則程序可能會(huì)無法運(yùn)行!

下面的for循環(huán)遍歷就是將生成的迷宮矩陣打印出來

S表示出發(fā)點(diǎn)

1表示路

0表示墻

E表示終點(diǎn)

生成迷宮矩陣后,就可以將其引用到游戲中去啦!

原文鏈接:https://blog.csdn.net/leleprogrammer/article/details/124205148

欄目分類
最近更新