Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Fri Apr 19 15:27:20 2019
- @author: IVAN
- """
- from utils import Problem
- from uninformed_search import breadth_first_tree_search, depth_first_tree_search, \
- breadth_first_graph_search, depth_first_graph_search, depth_limited_search, \
- iterative_deepening_search
- class Table(Problem):
- def __init__(self, initial, goal):
- super().__init__(initial, goal)
- def successor(self, state):
- successors = dict()
- for i in range(0, n):
- for j in range(0, n):
- new_state = list(list(x) for x in state)
- new_state[i][j] = 1 - new_state[i][j]
- if i != 0:
- new_state[i - 1][j] = 1 - new_state[i - 1][j]
- if i != n - 1:
- new_state[i + 1][j] = 1 - new_state[i + 1][j]
- if j != 0:
- new_state[i][j - 1] = 1 - new_state[i][j - 1]
- if j != n - 1:
- new_state[i][j + 1] = 1 - new_state[i][j + 1]
- successors["x: " + str(i) + ", y: " + str(j)] = tuple(tuple(x) for x in new_state)
- return successors
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- return self.successor(state)[action]
- if __name__ == '__main__':
- n = int(input())
- fields = list(map(int, input().split(',')))
- # solve the problem after this comment line
- goal = [[] for i in range(n)]
- for i in range(0, n):
- for j in range(0, n):
- goal[i].append(1)
- initial_state = [[] for i in range(n)]
- for i in range(0, n):
- for j in range(0, n):
- initial_state[i].append(fields[i * n + j])
- problem = Table(tuple(tuple(x) for x in initial_state), tuple(tuple(x) for x in goal))
- answer = breadth_first_graph_search(problem)
- print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement