4.迷宫大逃亡

根据题目的说明,编写代码达到题目的要求。即可得到flag,一般flag格式是ctf{},所以在编写的代码,最终结果可能需要转码操作

你掉进了一个连环迷宫, 这个连环迷宫总共有T个迷宫~

每个迷宫有如下信息:
迷宫大小n (表示这是n*n的迷宫)
迷宫入口坐标
迷宫出口坐标
迷宫地图(由'X', 'O'组成的n行n列的迷宫,'X'表示障碍物, 即不可走,'O'表示可走的道路)

如果能走出这个迷宫那么你将得到一个1,否则你将得到一个0
这T个 0/1就是你走出这个连环迷宫的钥匙

示例:
T=2时:
2
3
1 1
1 3
OXO
OOO
XXX

3
1 2
3 3
XOX
OOX
XXO

那么钥匙为:10

Hint: 

这T个迷宫在in.txt文件里,这个文件第一行就是T, 接下来就是T个迷宫的信息

key值:CTF{xxxx}

# coding=utf-8
import threading
import time
import base64
def openfile():
result = ""
f = open(r"E:PythonMyScript实验吧in.txt", "r")
i=int(f.readline().strip())
while (i != 0):
line=None
while not line:
line = f.readline().strip()
scale = int(line)
start = []
for x in f.readline().strip().split():
start.append(int(x)-1)
end = []
for x in f.readline().strip().split():
end.append(int(x)-1)
migong = []
for _ in range(scale):
migong.append(f.readline().strip())
r=pyqueue(len(migong[0]), start, end, migong)
result+=str(r.run())
i = i - 1
#print result
print(result)
flag=""
for i in range(0,len(result),8):
c = result[i:i+8]
flag+=chr(int(c,2))
print(base64.b64decode(flag))
class pyqueue:
def __init__(self, _len, _start, _end, _migong):
self.len = _len #地图尺寸
self.start = _start #起点
self.end = _end #终点点 格式 [x,y]
self.migong = _migong #地图
self.queue = [self.start] #初始的地方
self.steptrace = [] #走过的地方
self.sucess = 0 #判断找到了终点
def Iqueue(self, location):
self.queue.append(location)
self.steptrace.append(location)

def Oqueue(self):
temp = self.queue[0]
del (self.queue[0])
return temp

def Void(self, location):
try:
if location in self.steptrace or self.migong[location[0]][location[1]] == "X":
return
else:
if location == self.end:
self.sucess=1
return
self.steptrace.append(location)
self.Iqueue(location)
return
except:
print("error")
print(location)

def empty(self):
try:
self.queue[0]
return True
except IndexError:
return False

def addx(self, x):
x += 1
if x >= self.len: return x - 1
return x

def subx(self, x):
x -= 1
if x < 0: return x + 1
return x

def run(self):
return str(self.zoumigong())

def zoumigong(self):
while self.empty():
location = self.Oqueue()
#print location
self.Void([self.addx(location[0]),location[1]])
self.Void([self.subx(location[0]), location[1]])
self.Void([location[0],self.addx(location[1])])
self.Void([location[0], self.subx(location[1])])
# 上下左右各试探一下
if self.sucess==1:
return 1
return 0

# localtion=[x,y]
# migong[location[0],local[1]]

if __name__ == '__main__':
openfile()

原文地址:https://www.cnblogs.com/DennyT/p/11331246.html