-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC6.py
178 lines (141 loc) · 3.47 KB
/
C6.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
##########################################
#
# #TuentiChallenge3 (2013).
# Challenge 6
# Isaac Roldán (@saky)
#
##########################################
#INFO: You are in an ice-cave like the ones in the pokemon games
# You have to find the min time necessary to scape.
# The solution expands a tree with each possible path in each iteration (avoiding loops)
import copy
superArray = []
def main():
testCases = int(raw_input(""))
for i in range(testCases):
global superArray
del superArray[:]
data = raw_input("")
data = data.split(" ")
w = int(data[0])
h = int(data[1])
speed = int(data[2])
time = int(data[3])
matrix = []
for row in range(h):
string = raw_input("")
str2 = ""
flag=0
for c in string:
if c=="#": str2+="#"
elif c=="X": str2+="X"
elif c=="O": str2+="O"
elif flag==0:
str2+="."
flag=1
else:
flag=0
matrix.append(str2)
fI,cI = findInit(matrix)
fS,cS = findExit(matrix)
history=[]
history.append([fI,cI])
findPath(matrix, fI, cI, fS, cS,w,h,speed,time,[fI,cI],history,0)
if len(superArray)>0:
print min(superArray)
#Recursive method to find the optimal path
def findPath(matrix,fI,cI,fS,cS,w,h,speed,time,anterior,history,coste):
if fI==fS and cI==cS:
# print "found! -> ",int(round(coste))
addToArray(int(round(coste)))
return
#Get each possible path from the current position.
caminos = expand(matrix,fI,cI,h,w,anterior)
for camino in caminos:
#We save the positions history to avoid loops
newHistory=copy.deepcopy(history)
if [camino[1],camino[2]] in history:
continue #loop detected, continue with the next iteration
else:
newHistory.append([camino[1],camino[2]])
lon = camino[0]
aux = lon/float(speed)+time
newCoste = copy.deepcopy(coste)
newCoste = newCoste + aux
if len(superArray)>0:
if newCoste>min(superArray):
continue
#if newCoste>(h*w): continue
findPath(matrix,camino[1],camino[2],fS,cS,w,h,speed,time,[fI,cI],newHistory,newCoste)
def addToArray(total):
global superArray
superArray.append(total)
def expand(matrix,fI,cI,h,w,anterior):
if matrix[fI][cI]=="O":
return [0,fI,cI]
caminos = []
for i in range(fI,-1,-1):
if matrix[i][cI]=="#":
if [i+1,cI]!=anterior:
camino=[fI-(i+1),i+1,cI]
if fI-(i+1)>0:
caminos.append(camino)
break
else:
break
if matrix[i][cI]=="O":
camino=[fI-i,i,cI]
caminos.append(camino)
break
for i in range(fI,h):
if matrix[i][cI]=="#":
if [i-1,cI]!=anterior:
camino=[i-1-fI,i-1,cI]
if i-1-fI>0:
caminos.append(camino)
break
else:
break
if matrix[i][cI]=="O":
camino=[i-fI,i,cI]
caminos.append(camino)
break
for i in range(cI,-1,-1):
if matrix[fI][i]=="#":
if [fI,i+1]!=anterior:
camino=[cI-(i+1),fI,i+1]
if cI-(i+1)>0:
caminos.append(camino)
break
else:
break
if matrix[fI][i]=="O":
camino=[cI-i,fI,i]
caminos.append(camino)
break
for i in range(cI,w):
if matrix[fI][i]=="#":
if [fI,i-1]!=anterior:
camino=[i-1-cI,fI,i-1]
if i-1-cI>0:
caminos.append(camino)
break
else:
break
if matrix[fI][i]=="O":
camino=[i-cI,fI,i]
caminos.append(camino)
break
return caminos
def findExit(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j]=="O":
return i,j
def findInit(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j]=="X":
return i,j
if __name__ == '__main__':
main()