-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
310 lines (259 loc) · 7.64 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#import bisect
import time
import collections
class Point:
def __lt__(self, other):
return self.id < other.id
def __init__(self):
self.up = "x"
self.down = "x"
self.right = "x"
self.left = "x"
self.str_arr = "x"
self.id = 0
self.trace = ""
def print_arr(x):
print(x.str_arr[0:3])
print(x.str_arr[3:6])
print(x.str_arr[6:9])
print()
def myreplace(mystr, myinput):
index = mystr.find(myinput)
mystr = mystr.replace("0", myinput, 1)
mystr = mystr[:index] + "0" + mystr[index + 1:]
return mystr
def make_neighbors(x):
index = x.str_arr.find("0")
#ak som v poslednom riadku
if index > (X * (Y - 1)) - 1:
x.down = "x"
else:
x.down = x.str_arr[index + X]
#ak som v prvom riadku
if index < X:
x.up = "x"
else:
x.up = x.str_arr[index - X]
#ak som v poslednom stlpci
if index % X == X - 1:
x.right = "x"
else:
x.right = x.str_arr[index + 1]
#ak som v prvom stlpci
if index % X == 0:
x.left = "x"
else:
x.left = x.str_arr[index - 1]
def play(temp):
print_arr(temp)
global finaltrace
if len(finaltrace) == 0:
return
sign = finaltrace[0]
finaltrace = finaltrace[1:]
make_neighbors(temp)
if sign == "↓":
temp.str_arr = myreplace(temp.str_arr, temp.down)
play(temp)
if sign == "↑":
temp.str_arr = myreplace(temp.str_arr, temp.up)
play(temp)
if sign == "→":
temp.str_arr = myreplace(temp.str_arr, temp.right)
play(temp)
if sign == "←":
temp.str_arr = myreplace(temp.str_arr, temp.left)
play(temp)
def move(x):
make_neighbors(x)
previous = "x"
if len(x.trace):
previous = x.trace[-1]
# idem down
if x.down != "x" and previous != "↑":
child = Point()
child.str_arr = myreplace(x.str_arr, x.down)
child.trace = x.trace + "↓"
child.id = int(child.str_arr)
if not checkexist(0, child):
existing.append(child)
# idem hore
if x.up != "x" and previous != "↓":
child = Point()
child.str_arr = myreplace(x.str_arr, x.up)
child.trace = x.trace + "↑"
child.id = int(child.str_arr)
if not checkexist(0, child):
existing.append(child)
# idem doprava
if x.right != "x" and previous != "←":
child = Point()
child.str_arr = myreplace(x.str_arr, x.right)
child.trace = x.trace + "→"
child.id = int(child.str_arr)
if not checkexist(0, child):
existing.append(child)
# idem do lava
if x.left != "x" and previous != "→":
child = Point()
child.str_arr = myreplace(x.str_arr, x.left)
child.trace = x.trace + "←"
child.id = int(child.str_arr)
if not checkexist(0, child):
existing.append(child)
def move_reverse(x):
make_neighbors(x)
previous = "x"
if len(x.trace):
previous = x.trace[-1]
# robim pohyby ale zapisujem opacne pohyby lebo idem odzadu
if x.down != "x" and previous != "↓":
child = Point()
child.str_arr = myreplace(x.str_arr, x.down)
child.trace = x.trace + "↑"
child.id = int(child.str_arr)
if not checkexist(1, child):
existing_reversed.append(child)
# idem hore
if x.up != "x" and previous != "↑":
child = Point()
child.str_arr = myreplace(x.str_arr, x.up)
child.trace = x.trace + "↓"
child.id = int(child.str_arr)
if not checkexist(1, child):
existing_reversed.append(child)
# idem doprava
if x.right != "x" and previous != "→":
child = Point()
child.str_arr = myreplace(x.str_arr, x.right)
child.trace = x.trace + "←"
child.id = int(child.str_arr)
if not checkexist(1, child):
existing_reversed.append(child)
# idem do lava
if x.left != "x" and previous != "←":
child = Point()
child.str_arr = myreplace(x.str_arr, x.left)
child.trace = x.trace + "→"
child.id = int(child.str_arr)
if not checkexist(1, child):
existing_reversed.append(child)
def checkexist(rev, x):
if rev:
for y in existing_reversed:
if x.id == y.id:
return True
#for y in spracovaneR:
# if x.id == y.id:
# return True
if processed_reversed[x.id] is not None:
return True
if not rev:
for y in existing:
if x.id == y.id:
return True
for y in processed:
if x.id == y.id:
return True
return False
def check():
global finaltrace
#hashmapa
for x in processed:
if processed_reversed[x.id] is not None:
finaltrace = x.trace + processed_reversed[x.id][::-1]
print(finaltrace)
return True
"""
#nezoradene polia
for x in spracovane:
for y in spracovaneR:
if x.id == y.id:
finaltrace = x.trace + y.trace[::-1]
print(finaltrace)
return True
"""
"""
#zoradene polia
m = len(spracovane)
n = len(spracovaneR)
i, j = 0, 0
while i < m and j < n:
if spracovane[i].id < spracovaneR[j].id:
i += 1
elif spracovaneR[j].id < spracovane[i].id:
j += 1
else:
finaltrace = spracovane[i].trace + spracovaneR[j].trace[::-1]
print(finaltrace)
return True
"""
return False
def solvable(x):
sum = 0
i = 0
while i < 8:
j = i + 1
while j < 9:
if int(x.str_arr[i]) and int(x.str_arr[j]) and int(x.str_arr[i]) > int(x.str_arr[j]):
sum += 1
j += 1
i += 1
return sum % 2
def solve(zac):
i = 0
existing.append(zac)
existing_reversed.append(finish)
level = 10
while i < 15000000:
x = existing[0]
xr = existing_reversed[0]
move(x)
move_reverse(xr)
i += 1
#hashmapa
processed.append(existing.pop(0))
tempx = existing_reversed.pop(0)
tempid = tempx.id
processed_reversed[tempid] = tempx.trace
"""
#nezoradene polia
spracovane.append(existujuce.pop(0))
spracovaneR.append(existujuceR.pop(0))
"""
"""
#zoradene spracovane
bisect.insort_left(spracovane, existujuce.pop(0))
bisect.insort_left(spracovaneR, existujuceR.pop(0))
"""
if check():
return
if i == level:
level *= 10
print("PROGRAM SKONCIL")
return
#main
X = 3
Y = 3
existing = []
processed = []
existing_reversed = []
#spracovaneR = []
processed_reversed = collections.defaultdict(lambda: None, {})
beginning = Point()
beginning.str_arr = "123405678"
beginning.id = int(beginning.str_arr)
finish = Point()
finish.str_arr = "087654321"
finish.id = int(finish.str_arr)
global finaltrace
if solvable(beginning) == solvable(finish):
start = time.time()
solve(beginning)
length = len(finaltrace)
play(beginning)
end = time.time()
print("Trvanie: " + str(round(end - start, 2)) + "s")
print(str(length) + " tahov")
else:
print("Hra nieje riesitelna")