-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
70 lines (58 loc) · 1.96 KB
/
solver.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
from klase import Tabla
import copy
from collections import defaultdict
class Cvor:
def __init__(self, tabla:Tabla):
self._val = tabla
def getTabla(self):
return self._val.getTabla()
def getN(self):
return self._val.getN()
def getM(self):
return self._val.getM()
def nextMoves(self):
moves = []
for move in ['u', 'r', 'l', 'd']:
tab = Tabla(3,3)
tab.setTabla(copy.copy(self._val.getTabla()), self.getN(), self.getM())
if tab.pomeri(move):
moves.append((move, Cvor(tab)))
return moves
class Solver:
def __init__(self, start:Cvor):
self._startPos = start
@staticmethod
def solve(startTab:Tabla):
start = Cvor(startTab)
temp = [i for i in range(1, start._val.getM()*start._val.getN())]
temp.append(0)
goal = tuple(temp)
path = {}
path[tuple(start._val.getTabla())] = []
q = []
visited = defaultdict(int)
visited[tuple(start._val.getTabla())] += 1
q.append(start)
while(len(q)):
current = q.pop(0)
if tuple(current.getTabla()) == goal:
return path[goal]
for move in current.nextMoves():
if visited[tuple(move[1].getTabla())] > 0:
continue
path[tuple(move[1].getTabla())] = []
path[tuple(move[1].getTabla())] += path[tuple(current.getTabla())]
path[tuple(move[1].getTabla())]+= move[0]
new = Cvor(copy.copy(move[1]))
q.append(new)
visited[tuple(move[1].getTabla())] += 1
'''
cvor = Cvor(Tabla(3,3))
print(cvor._val.getTabla(), 'ovo je koju tablu resava')
Solver.solve(cvor)
[4, 8, 3, 6, 7, 2, 1, 5, 0] ovo je koju tablu resava
['r', 'r', 'd', 'l', 'u', 'r',
'd', 'd', 'l', 'u', 'l', 'u',
'r', 'r', 'd', 'd', 'l', 'u',
'r', 'u', 'l', 'd', 'l', 'u']
'''