电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Python實現隨機生成迷宮并自動尋路

瀏覽:2日期:2022-06-17 08:04:37
目錄Python深搜版:Python 廣搜版lua版:Python深搜版:

核心在于帶隨機的深搜(見代碼第23到27行,其實也可以用22行代替這幾行代碼,你可以試著把第24行的數字4改大或者改小,即調整隨機程度)

import osimport randomfrom queue import Queueimport numpyimport coloramafrom colorama import Fore, Back, Styleimport sys from bmpEditor import bmpcolorama.init()# numpy.random.seed(1)_xy = [0,2,0,-2,0]size = 31sys.setrecursionlimit(100000000)road = set()def dfs(curr_pos):road.add(curr_pos)# for i in numpy.random.permutation(4):p = [0,1,2,3]for i in range(4):l = random.randint(0,3)r = random.randint(0,3)p[l], p[r] = p[r], p[l]for i in p:next_pos = (curr_pos[0] + _xy[i], curr_pos[1] + _xy[i+1])if (0<=next_pos[0]<size and0<=next_pos[1]<size andnext_pos not in road ):road.add(((curr_pos[0] + next_pos[0])/2, (curr_pos[1] + next_pos[1])/2))dfs(next_pos)dfs((0,0))q = Queue()q.put((0,0))ans_road = set()def dfs_getans(curr_pos):# print(curr_pos)ans_road.add(curr_pos)if (size-1, size-1) in ans_road:returnfor i in range(4):next_pos = (curr_pos[0] + _xy[i]//2, curr_pos[1] + _xy[i+1]//2)if (0<=next_pos[0]<size and0<=next_pos[1]<size andnext_pos in road andnext_pos not in ans_road and(size-1, size-1) not in ans_road):dfs_getans(next_pos)if (size-1, size-1) not in ans_road:ans_road.remove(curr_pos)dfs_getans((0,0))for i in range(size):for j in range(size):print((Back.WHITE + ’ ’) if (i,j) in road else (Back.BLACK + ’ ’), end=’ ’)print()wall_width = 2cell_size = 6image = bmp((size+3)*cell_size-wall_width, (size+3)*cell_size-wall_width, 0x000000)for i in range(size+3):for j in range(size+3):if (i-1, j-1) in road:image.paint_rect(i*cell_size, j*cell_size, cell_size*2-wall_width, cell_size*2-wall_width, 0xffffff)file_name = '%dmaze.bmp'%sizeimage.save_image(file_name)os.system(file_name)for p in ans_road:# image.paint_rect(p[0]+1, p[1]+1)image.paint_rect((p[0]+1)*cell_size + (cell_size - wall_width)//2,(p[1]+1)*cell_size + (cell_size - wall_width)//2,cell_size, cell_size,0xff0000)file_name = '%dans.bmp'%sizeimage.save_image(file_name)os.system(file_name)

效果

3131:

Python實現隨機生成迷宮并自動尋路

Python實現隨機生成迷宮并自動尋路

8181:

Python實現隨機生成迷宮并自動尋路

Python實現隨機生成迷宮并自動尋路

坐標系有翻轉,控制臺中的左上角對應圖片中的左下角

其中bmpEditor不是官方庫,代碼地址(文件名為bmpEditor.py,和這以上代碼放同一個路徑下即可)

Python 廣搜版

在隊列的基礎上把隊列中的元素順序打亂(第24行)

import osimport randomfrom queue import Queueimport numpyimport coloramafrom colorama import Fore, Back, Styleimport sys import random from bmpEditor import bmpcolorama.init()numpy.random.seed(1)_xy = [0,2,0,-2,0]size = 59sys.setrecursionlimit(size*size//4+size)q = []q.append((0,0))road = set()road.add((0,0))while len(q) != 0:random.shuffle(q)curr_pos = q.pop()# print(curr_pos)for i in range(4):next_pos = (curr_pos[0] + _xy[i], curr_pos[1] + _xy[i+1])if (0<=next_pos[0]<size and0<=next_pos[1]<size andnext_pos not in road ):road.add( ((curr_pos[0] + next_pos[0])//2, (curr_pos[1] + next_pos[1])//2) )q.append(next_pos)road.add(next_pos)ans_road = set()def dfs_getans(curr_pos):ans_road.add(curr_pos)if (size-1, size-1) in ans_road:returnfor i in range(4):next_pos = (curr_pos[0] + _xy[i]//2, curr_pos[1] + _xy[i+1]//2)if (0<=next_pos[0]<size and0<=next_pos[1]<size andnext_pos in road andnext_pos not in ans_road and(size-1, size-1) not in ans_road):dfs_getans(next_pos)if (size-1, size-1) not in ans_road:ans_road.remove(curr_pos)dfs_getans((0,0))print(len(ans_road))for i in range(0, size):for j in range(0, size):print((Back.WHITE + ’ ’) if (i,j) in road else (Back.BLACK + ’ ’), end=’ ’)print()wall_width = 1cell_size = 5image = bmp((size+3)*cell_size-wall_width, (size+3)*cell_size-wall_width, 0x000000)for i in range(size+3):for j in range(size+3):if (i-1, j-1) in road:image.paint_rect(i*cell_size, j*cell_size, cell_size*2-wall_width, cell_size*2-wall_width, 0xffffff)file_name = '%dmaze.bmp'%sizeimage.save_image(file_name)os.system(file_name)for p in ans_road:# image.paint_rect(p[0]+1, p[1]+1)image.paint_rect((p[0]+1)*cell_size + (cell_size - wall_width)//2,(p[1]+1)*cell_size + (cell_size - wall_width)//2,cell_size, cell_size,0xff0000)file_name = '%dans.bmp'%sizeimage.save_image(file_name)os.system(file_name)

效果:

Python實現隨機生成迷宮并自動尋路

Python實現隨機生成迷宮并自動尋路

相比深度優先的,這種迷宮會更加“直”一些

lua版:

大體上是深搜,加了一定的隨機性使得搜索過程中有一定概率暫時放棄當前路徑。見表stop_points,(第7行、第74行及其后面的repeat循環)

local _xy = {0,2,0,-2,0}local size = 41local base = size+1local road = {}stop_points = {}function dfs(curr_x, curr_y)road[curr_x*base+curr_y] = trueif math.random(1,10) <= 3 thenstop_points[curr_x*base+curr_y] = truereturnend-- os.execute('cls')-- print_map()local permutation = {1,2,3,4}for i=1, 4 dolocal l = math.random(1,4)local r = math.random(1,4)permutation[l], permutation[r] = permutation[r], permutation[l]endfor i=1, 4 dolocal next_x = curr_x+_xy[permutation[i]]local next_y = curr_y+_xy[permutation[i]+1]if next_x>=1 and next_x<=size andnext_y>=1 and next_y<=size androad[next_x*base+next_y] == nil thenlocal mid_x = math.floor((curr_x+next_x)/2)local mid_y = math.floor((curr_y+next_y)/2)road[mid_x*base+mid_y] = truedfs(next_x, next_y)endendendlocal ans_geted = falselocal parent = {}function get_ans(curr_x, curr_y)-- print(curr_x, curr_y)for i=1, 4 donext_x = (curr_x + math.floor(_xy[i])/2 )next_y = (curr_y + math.floor(_xy[i+1])/2 )-- print(next_x, next_y)if next_x >= 1 and next_x <= size andnext_y >= 1 and next_y <= size androad[next_x*base+next_y] andparent[next_x*base+next_y]==nilthenparent[next_x*base+next_y] = curr_x*base+curr_yget_ans(next_x, next_y)endendendlocal ans_road = {}function print_map()for i=0, size+1 dolocal line = ''for j=0, size+1 doif ans_road [i*base+j] thenline = line..'..'elseif road[i*base+j]==true thenline = line..' 'elseline = line..'HH'endendprint(line)endendstop_points[1*base+1] = true-- create mazerepeatlocal has_point = falsefor v,_ in pairs(stop_points) dohas_point = truestop_points[v] = nildfs(math.floor(v/base), v%base)breakend-- print(has_point)until not has_pointget_ans(1,1)parent[1*base+1] = nilprint('')-- for k,v in pairs(parent) do-- print(string.format('[%d,%d]->[%d,%d]', math.floor(k/base), k%base, math.floor(v/base), v%base))-- endprint('')local x = sizelocal y = sizerepeat-- print(x,y)ans_road[x*base+y] = truelocal v = parent[x*base+y]x = math.floor(v/base)y = v%baseuntil --[[(x==1 and y== 1)]] not parent[x*base+y]ans_road[1*base+1] = trueprint_map()

效果:

4141:

Python實現隨機生成迷宮并自動尋路

8989

Python實現隨機生成迷宮并自動尋路

到此這篇關于Python實現隨機生成迷宮并自動尋路的文章就介紹到這了,更多相關Python生成迷宮并自動尋路內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 中式装修设计_室内中式装修_【云臻轩】中式设计机构 | POS机办理_个人pos机免费领取-银联pos机申请首页 | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 湖南印刷厂|长沙印刷公司|画册印刷|挂历印刷|台历印刷|杂志印刷-乐成印刷 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 档案密集架_电动密集架_移动密集架_辽宁档案密集架-盛隆柜业厂家现货批发销售价格公道 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 桐城新闻网—桐城市融媒体中心主办| 耙式干燥机_真空耙式干燥机厂家-无锡鹏茂化工装备有限公司 | 净化工程_无尘车间_无尘车间装修-广州科凌净化工程有限公司 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | 美缝剂_美缝剂厂家_美缝剂加盟-地老板高端瓷砖美缝剂 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 福兰德PVC地板|PVC塑胶地板|PVC运动地板|PVC商用地板-中国弹性地板系统专业解决方案领先供应商! 福建成考网-福建成人高考网 | 证券新闻,热播美式保罗1984第二部_腾讯1080p-仁爱影院 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 桨叶搅拌机_螺旋挤压/方盒旋切造粒机厂家-无锡市鸿诚输送机械有限公司 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 冷却塔风机厂家_静音冷却塔风机_冷却塔电机维修更换维修-广东特菱节能空调设备有限公司 | 防火卷帘门价格-聊城一维工贸特级防火卷帘门厂家▲ | 设定时间记录电子秤-自动累计储存电子秤-昆山巨天仪器设备有限公司 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | 游泳池设计|设备|配件|药品|吸污机-东莞市太平洋康体设施有限公司 | 农产品溯源系统_农产品质量安全追溯系统_溯源系统 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 新车测评网_网罗汽车评测资讯_汽车评测门户报道 | 石家庄律师_石家庄刑事辩护律师_石家庄取保候审-河北万垚律师事务所 | 代理记账_免费注册公司_营业执照代办_资质代办-【乐财汇】 | 伟秀电气有限公司-10kv高低压开关柜-高低压配电柜-中置柜-充气柜-欧式箱变-高压真空断路器厂家 | 辊道窑炉,辊道窑炉厂家-山东艾希尔 | 打包箱房_集成房屋-山东佳一集成房屋有限公司 | 影合社-影视人的内容合作平台 | 磁粉制动器|张力控制器|气胀轴|伺服纠偏控制器整套厂家--台灵机电官网 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 沈阳楼承板_彩钢板_压型钢板厂家-辽宁中盛绿建钢品股份有限公司 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 化工ERP软件_化工新材料ERP系统_化工新材料MES软件_MES系统-广东顺景软件科技有限公司 | YT保温材料_YT无机保温砂浆_外墙保温材料_南阳银通节能建材高新技术开发有限公司 |