Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bisect
- class Problem:
- def __init__(self, initial, goal=None):
- self.initial = initial
- self.goal = goal
- def successor(self, state):
- raise NotImplementedError
- 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):
- return state == self.goal
- def path_cost(self, c, state1, action, state2):
- return c + 1
- def value(self):
- raise NotImplementedError
- class Node:
- def __init__(self, state, parent=None, action=None, path_cost=0):
- self.state = state
- self.parent = parent
- self.action = action
- self.path_cost = path_cost
- self.depth = 0 # search depth
- if parent:
- self.depth = parent.depth + 1
- def __repr__(self):
- return "<Node %s>" % (self.state,)
- def __lt__(self, node):
- return self.state < node.state
- def expand(self, problem):
- return [self.child_node(problem, action)
- for action in problem.actions(self.state)]
- def child_node(self, problem, action):
- next_state = problem.result(self.state, action)
- return Node(next_state, self, action,
- problem.path_cost(self.path_cost, self.state,
- action, next_state))
- def path_cost(self, c, state1, action, state2):
- return c + 1
- def solution(self):
- return [node.action for node in self.path()[1:]]
- def solve(self):
- return [node.state for node in self.path()[0:]]
- def path(self):
- x, result = self, []
- while x:
- result.append(x)
- x = x.parent
- result.reverse()
- return result
- def __eq__(self, other):
- return isinstance(other, Node) and self.state == other.state
- def __hash__(self):
- return hash(self.state)
- class Queue:
- def __init__(self):
- raise NotImplementedError
- def append(self, item):
- raise NotImplementedError
- def extend(self, items):
- raise NotImplementedError
- def pop(self):
- raise NotImplementedError
- def __len__(self):
- raise NotImplementedError
- def __contains__(self, item):
- raise NotImplementedError
- class Stack(Queue):
- def __init__(self):
- self.data = []
- def append(self, item):
- self.data.append(item)
- def extend(self, items):
- self.data.extend(items)
- def pop(self):
- return self.data.pop()
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return item in self.data
- class FIFOQueue(Queue):
- def __init__(self):
- self.data = []
- def append(self, item):
- self.data.append(item)
- def extend(self, items):
- self.data.extend(items)
- def pop(self):
- return self.data.pop(0)
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return item in self.data
- class PriorityQueue(Queue):
- def __init__(self, order=min, f=lambda x: x):
- assert order in [min, max]
- self.data = []
- self.order = order
- self.f = f
- def append(self, item):
- bisect.insort_right(self.data, (self.f(item), item))
- def extend(self, items):
- for item in items:
- bisect.insort_right(self.data, (self.f(item), item))
- def pop(self):
- if self.order == min:
- return self.data.pop(0)[1]
- return self.data.pop()[1]
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return any(item == pair[1] for pair in self.data)
- def __getitem__(self, key):
- for _, item in self.data:
- if item == key:
- return item
- def __delitem__(self, key):
- for i, (value, item) in enumerate(self.data):
- if item == key:
- self.data.pop(i)
- def tree_search(problem, fringe):
- fringe.append(Node(problem.initial))
- while fringe:
- node = fringe.pop()
- if problem.goal_test(node.state):
- return node
- fringe.extend(node.expand(problem))
- return None
- def breadth_first_tree_search(problem):
- return tree_search(problem, FIFOQueue())
- class Kocki(Problem):
- def __init__(self, n, initial):
- self.n = n
- self.initial = dict()
- x = 0
- for i in range(0, self.n):
- for j in range(0, self.n):
- self.initial[(i, j)] = initial[x]
- x += 1
- self.goal = dict()
- for i in range(0, self.n):
- for j in range(0, self.n):
- self.goal[(i, j)] = 1
- def successor(self, state):
- succ = dict()
- for action in actions(state):
- succ[action] = result(state, action)
- return succ
- def result(self, state, action):
- i, j = action
- stateCopy = dict(state)
- stateCopy[(i, j)] = abs(stateCopy[(i, j)] - 1)
- if (i - 1 >= 0):
- stateCopy[(i - 1, j)] = abs(stateCopy[(i - 1, j)] - 1)
- if (i + 1 < self.n):
- stateCopy[(i + 1, j)] = abs(stateCopy[(i + 1, j)] - 1)
- if (j - 1 >= 0):
- stateCopy[(i, j - 1)] = abs(stateCopy[(i, j - 1)] - 1)
- if (j + 1 < self.n):
- stateCopy[(i, j + 1)] = abs(stateCopy[(i, j + 1)] - 1)
- return stateCopy
- def actions(self, state):
- actions = list()
- for i in range(0, self.n):
- for j in range(0, self.n):
- actions.append((i, j))
- return actions
- def goal_test(self, state):
- for i in range(0, self.n):
- for j in range(0, self.n):
- if (state[(i, j)] != self.goal[(i, j)]):
- return False
- return True
- if __name__ == "__main__":
- n = int(input())
- polinja = list(map(int, input().split(',')))
- k = Kocki(n, polinja)
- x = breadth_first_tree_search(k)
- lists=x.solution()
- empty=list()
- for (x,y) in lists:
- empty.append('x: '+str(x)+', y: '+str(y))
- print(empty)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement