1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import time, sys
sys.setrecursionlimit(50000)
file = open('maze.txt','r')
area = file.read().replace(',','').splitlines()
file.close()
area = [[int(x) for x in l] for l in area]
for x, line in enumerate(area):
if x < 3 or x == 1499:
pass
else:
for y, obj in enumerate(line):
if y < 2 or y == 1499:
pass
else:
if obj == 9 :
ids = (x,y)
break
break
nb = 10
def look_around(coords):
x, y = coords
if area[x-1][y] == 1:
area[x-1][y] = nb
look_around((x-1,y))
if area[x+1][y] == 1:
area[x+1][y] = nb
look_around((x+1,y))
if area[x][y-1] == 1:
area[x][y-1] = nb
look_around((x,y-1))
if area[x][y+1] == 1:
area[x][y+1] = nb
look_around((x,y+1))
def find_other_nine(coords):
global nb, pcd
x, y = coords
if area[x-1][y] == 9 and (x-1, y) != pcd :
pcd = coords
return x-1, y
if area[x+1][y] == 9 and (x+1, y) != pcd :
pcd = coords
return x+1, y
if area[x][y-1] == 9 and (x, y-1) != pcd :
pcd = coords
return x, y-1
if area[x][y+1] == 9 and (x, y+1) != pcd :
pcd = coords
return x, y+1
print('Start :',ids)
pcd = ids[0]-1,ids[1]
while ids != (1498, 1498):
look_around(ids)
nb += 1
ids = find_other_nine(ids)
#print(ids)
print('end')
string = ''
for line in area:
string+=str(line).replace(' ','')[1:-1]
string+='\n'
#string = string.replace('9','1')
#string = string.replace('2','1')
file = open('new_maze.txt','w')
file.write(string)
file.close() |
Partager