Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #super raboti ama duri da se iskuca ko so treba...
- def pomesti_levo(atom,obstacles,atom2,atom3):
- x=atom[0]
- y=atom[1]
- temp_atom = []
- temp_atom.append(x)
- temp_atom.append(y)
- while(temp_atom[0]>=1):
- #print(temp_atom)
- temp_atom[0]-=1
- if tuple(temp_atom) in obstacles or tuple(temp_atom)==atom2 or tuple(temp_atom)==atom3:
- temp_atom[0]+=1
- break
- return tuple(temp_atom)
- def pomesti_desno(atom, obstacles, atom2, atom3):
- x = atom[0]
- y = atom[1]
- temp_atom = []
- temp_atom.append(x)
- temp_atom.append(y)
- while (temp_atom[0] <=7):
- # print(temp_atom)
- temp_atom[0] += 1
- if tuple(temp_atom) in obstacles or tuple(temp_atom) == atom2 or tuple(temp_atom) == atom3:
- temp_atom[0] -= 1
- break
- return tuple(temp_atom)
- def pomesti_gore(atom, obstacles, atom2, atom3):
- x = atom[0]
- y = atom[1]
- temp_atom = []
- temp_atom.append(x)
- temp_atom.append(y)
- while (temp_atom[1] <=5):
- # print(temp_atom)
- temp_atom[1] += 1
- if tuple(temp_atom) in obstacles or tuple(temp_atom) == atom2 or tuple(temp_atom) == atom3:
- temp_atom[1] -= 1
- break
- return tuple(temp_atom)
- def pomesti_dolu(atom, obstacles, atom2, atom3):
- x = atom[0]
- y = atom[1]
- temp_atom = []
- temp_atom.append(x)
- temp_atom.append(y)
- while (temp_atom[1] >=1):
- # print(temp_atom)
- temp_atom[1] -= 1
- if tuple(temp_atom) in obstacles or tuple(temp_atom) == atom2 or tuple(temp_atom) == atom3:
- temp_atom[1] += 1
- break
- return tuple(temp_atom)
- class Water(Problem):
- def __init__(self, initial, house=None):
- super().__init__(initial, house)
- def goal_test(self, state):
- hydrogen1 = state[0]
- hydrogen2 = state[1]
- oxygen = state[2]
- return hydrogen1[1]==oxygen[1] and hydrogen2[1]==oxygen[1] and hydrogen1[0]+1==oxygen[0] and oxygen[0]+1==hydrogen2[0]
- def successor(self, state):
- succ = {}
- hydrogen1 = state[0]
- hydrogen2 = state[1]
- oxygen = state[2]
- obstacles= state[3]
- new_hyrogen1= pomesti_levo(hydrogen1,obstacles,oxygen,hydrogen2)
- if new_hyrogen1!=hydrogen1:
- succ["Pomesti H1 levo"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
- new_hydrogen2= pomesti_levo(hydrogen2,obstacles,hydrogen1,oxygen)
- if new_hydrogen2!=hydrogen2:
- succ["Pomesti H2 levo"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
- new_oxygen= pomesti_levo(oxygen,obstacles,hydrogen1,hydrogen2)
- if new_oxygen!=oxygen:
- succ["Pomesti O levo"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
- new_hyrogen1= pomesti_desno(hydrogen1,obstacles,oxygen,hydrogen2)
- if new_hyrogen1!=hydrogen1:
- succ["Pomesti H1 desno"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
- new_hydrogen2= pomesti_desno(hydrogen2,obstacles,hydrogen1,oxygen)
- if new_hydrogen2!=hydrogen2:
- succ["Pomesti H2 desno"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
- new_oxygen= pomesti_desno(oxygen,obstacles,hydrogen1,hydrogen2)
- if new_oxygen!=oxygen:
- succ["Pomesti O desno"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
- new_hyrogen1= pomesti_gore(hydrogen1,obstacles,oxygen,hydrogen2)
- if new_hyrogen1!=hydrogen1:
- succ["Pomesti H1 gore"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
- new_hydrogen2= pomesti_gore(hydrogen2,obstacles,hydrogen1,oxygen)
- if new_hydrogen2!=hydrogen2:
- succ["Pomesti H2 gore"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
- new_oxygen= pomesti_gore(oxygen,obstacles,hydrogen1,hydrogen2)
- if new_oxygen!=oxygen:
- succ["Pomesti O gore"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
- new_hyrogen1= pomesti_dolu(hydrogen1,obstacles,oxygen,hydrogen2)
- if new_hyrogen1!=hydrogen1:
- succ["Pomesti H1 dolu"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
- new_hydrogen2= pomesti_dolu(hydrogen2,obstacles,hydrogen1,oxygen)
- if new_hydrogen2!=hydrogen2:
- succ["Pomesti H2 dolu"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
- new_oxygen= pomesti_dolu(oxygen,obstacles,hydrogen1,hydrogen2)
- if new_oxygen!=oxygen:
- succ["Pomesti O dolu"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
- return succ
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- return self.successor(state)[action]
- if __name__ == '__main__':
- hydrogen1= (2,1)
- hydrogen2= (2,6)
- oxygen= (7,2)
- obstacles= ((0,1),(1,1),(3,1),(6,1),(4,2),(6,2),(1,3),(6,3),(7,3),(2,5),(8,5),(3,6),(5,6),(7,6))
- water = Water((hydrogen1,hydrogen2,oxygen,obstacles))
- answer = breadth_first_graph_search(water)
- if answer is None:
- print("None")
- else:
- print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement