Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def updateP1(P1):
- x = P1[0]
- y = P1[1]
- n = P1[2]
- if (y == 4 and n == 1):
- n = n * (-1)
- if (y == 0 and n == -1):
- n = n * (-1)
- ynew = y + n
- return (x, ynew, n)
- def updateP2(P2):
- x = P2[0]
- y = P2[1]
- n = P2[2]
- if (x == 5 and y == 4 and n == 1):
- n = n * (-1)
- if (y == 0 and x == 9 and n == -1):
- n = n * (-1)
- xnew = x - n
- ynew = y + n
- return (xnew, ynew, n)
- def updateP3(P3):
- x = P3[0]
- y = P3[1]
- n = P3[2]
- if (x == 5 and n == -1):
- n = n * (-1)
- if (x == 9 and n == 1):
- n = n * (-1)
- xnew = x + n
- return (xnew, y, n)
- def isValid(x, y, P1, P2, P3):
- t = 1
- if (x == P1[0] and y == P1[1]) or (x == P1[0] and y == P1[1] + 1):
- t = 0
- if (x == P2[0] and y == P2[1]) or (x == P2[0] and y == P2[1] + 1) or (x == P2[0] + 1 and y == P2[1]) or (
- x == P2[0] + 1 and y == P2[1] + 1):
- t = 0
- if (x == P3[0] and y == P3[1]) or (x == P3[0] + 1 and y == P3[1]):
- t = 0
- return t
- class Istrazuvac(Problem):
- def __init__(self, initial, goal):
- self.initial = initial
- self.goal = goal
- def successor(self, state):
- successors = dict()
- X = state[0]
- Y = state[1]
- P1 = state[2]
- P2 = state[3]
- P3 = state[4]
- P1new = updateP1(P1)
- P2new = updateP2(P2)
- P3new = updateP3(P3)
- # Levo
- if Y - 1 >= 0:
- Ynew = Y - 1
- Xnew = X
- if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
- successors['Levo'] = (Xnew, Ynew, P1new, P2new, P3new)
- # Desno
- if X < 5 and Y < 5:
- Ynew = Y + 1
- Xnew = X
- if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
- successors['Desno'] = (Xnew, Ynew, P1new, P2new, P3new)
- elif X >= 5 and Y < 10:
- Xnew = X
- Ynew = Y + 1
- if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
- successors['Desno'] = (Xnew, Ynew, P1new, P2new, P3new)
- # Dolu
- if X < 10:
- Xnew = X + 1
- Ynew = Y
- if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
- successors['Dolu'] = (Xnew, Ynew, P1new, P2new, P3new)
- # Gore
- if Y >= 6 and X > 5:
- Xnew = X - 1
- Ynew = Y
- if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
- successors['Gore'] = (Xnew, Ynew, P1new, P2new, P3new)
- elif Y < 6 and X > 0:
- Xnew = X - 1
- Ynew = Y
- if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
- successors['Gore'] = (Xnew, Ynew, P1new, P2new, P3new)
- return successors
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- possible = self.successor(state)
- return possible[action]
- def goal_test(self, state):
- g = self.goal
- return (state[0] == g[0] and state[1] == g[1])
- IstrazuvacInstance = Istrazuvac((choveche_redica, choveche_kolona, (2, 2, -1), (7, 2, 1), (7, 8, 1)),
- (kukja_redica, kukja_kolona))
- answer = breadth_first_graph_search(IstrazuvacInstance)
- print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement