欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

实验。迷宫逃亡者

最编程 2024-07-06 21:53:15
...
import base64 class mi_gong: def __init__(self,_len,_start,_end,_migong): self.len =_len #迷宫的大小 n*n self.start =_start #开始点 self.end = _end #终点 例:['3','5'] self.migong = _migong #迷宫 self.success = 0 #判断是否到达终点 self.arrived = [] #已经访问的 def run(self): return str(self.zou_migong()) def zou_migong(self): self.DFS(self.start) # print(self.arrived) if self.success ==1: return 1 return 0 #位置+1 def addx(self, x): x += 1 if x >= self.len: return x - 1 return x #位置-1 def subx(self, x): x -= 1 if x < 0: return x + 1 return x #采用深度优先搜索 def DFS(self,location): try: if self.migong[location[0]][location[1]] =="X" or location in self.arrived: return 0 else: self.arrived.append(location) #表示已访问过 if location == self.end: self.success =1 return 0 #上下左右遍历 shang =[self.subx(location[0]),location[1]] self.DFS(shang) you =[location[0],self.addx(location[1])] self.DFS(you) xia =[self.addx(location[0]),location[1]] self.DFS(xia) zuo =[location[0],self.subx(location[1])] self.DFS(zuo) except Exception as e: print(e) result ="" #最后的结果 with open('ins.txt','r') as f: migong_num =int(f.readline().strip()) #迷宫的数量 while(migong_num): m_len = int(f.readline().strip()) start = [int(x)-1 for x in f.readline().split()] end = [int(x)-1 for x in f.readline().split()] #生成迷宫 migong_list= [] for i in range(m_len): migong_list.append(f.readline().strip()) r =mi_gong(m_len,start,end,migong_list) result += str(r.run()) migong_num -= 1 f.close() print(result) ans ="" for i in range(0,len(result),8): ans+=chr(int(result[i:i+8],2)) print(ans) print(base64.b64decode(ans))

推荐阅读