-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaberinto.py
More file actions
112 lines (94 loc) · 1.74 KB
/
laberinto.py
File metadata and controls
112 lines (94 loc) · 1.74 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# encoding: utf-8
"""
laberinto.py
Created by Dann on 2015-03-23.
Dado un archivo laberinto.txt con muros como '|' y espacios como '0',
busca e imprime todas las soluciones.
"""
import sys
import os
lab=[]
m=0
n=0
movs=[[0,1],[1,0],[0,-1],[-1,0]]
def imprime(x):
labP=list(lab)
if x>=10:
for row in labP:
for car in row:
car=str(car)
if car.isdigit():
for i in range(0,len(str(x))-len(car)):
car=str(' '+str(car))
else:
for i in range(0,len(str(x))+1):
car+=str(car)
#print labP
print "Solución:\n"
if x<10:
for row in lab:
for car in row:
print car,
print '\n'
else:
for row in lab:
for car in row:
car=str(car)
if car.isdigit():
for i in range(0,len(str(x))-len(car)):
print ' ',
for char in car:
print char,
print ' ',
else:
for i in range(0,len(str(x))+1):
print car,
#print ' ',
print '\n'
print '\n'
def sol(x,i,j,m,n):
global lab
if(i==1 and j==n-1):
lab[i][j+1]=x
imprime(x)
return
for mov in movs:
ti=i
tj=j
ti=ti+mov[0]
tj=tj+mov[1]
if(ti>=0 and tj>=0 and ti<m and tj<n and lab[ti][tj]==0):
lab[ti][tj]=x
sol(x+1,ti,tj,m,n)
lab[ti][tj]=0
return
def main():
print "***Laberinto***\n"
with open('laberinto.txt','r') as f:
lines=f.readlines()
f.close()
m=0
for line in lines:
lab.append([])
for n in range(0,len(line)-1):
if line[n]=='0':
lab[m].append(0)
else:
lab[m].append(line[n])
m+=1
print m
print n
if (lab[m-1][1]==0 and lab[1][n-1]==0):
for row in lab:
for car in row:
print car,
print '\n'
print '\n'
lab[m-1][1]=1
sol(2,m-1,1,m,n)
else:
print "Error en laberinto\n"
pass
if __name__ == '__main__':
main()