Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Apr 19 15:27:20 2019
  4.  
  5. @author: IVAN
  6. """
  7. from utils import Problem
  8. from uninformed_search import breadth_first_tree_search, depth_first_tree_search, \
  9.     breadth_first_graph_search, depth_first_graph_search, depth_limited_search, \
  10.     iterative_deepening_search
  11.    
  12.  
  13. class Table(Problem):
  14.     def __init__(self, initial, goal):
  15.         super().__init__(initial, goal)
  16.  
  17.     def successor(self, state):
  18.         successors = dict()
  19.         for i in range(0, n):
  20.             for j in range(0, n):
  21.                 new_state = list(list(x) for x in state)
  22.                 new_state[i][j] = 1 - new_state[i][j]
  23.                 if i != 0:
  24.                     new_state[i - 1][j] = 1 - new_state[i - 1][j]
  25.                 if i != n - 1:
  26.                     new_state[i + 1][j] = 1 - new_state[i + 1][j]
  27.                 if j != 0:
  28.                     new_state[i][j - 1] = 1 - new_state[i][j - 1]
  29.                 if j != n - 1:
  30.                     new_state[i][j + 1] = 1 - new_state[i][j + 1]
  31.                 successors["x: " + str(i) + ", y: " + str(j)] = tuple(tuple(x) for x in new_state)
  32.         return successors
  33.  
  34.     def actions(self, state):
  35.         return self.successor(state).keys()
  36.  
  37.     def result(self, state, action):
  38.         return self.successor(state)[action]
  39.  
  40.  
  41. if __name__ == '__main__':
  42.     n = int(input())
  43.     fields = list(map(int, input().split(',')))
  44.     # solve the problem after this comment line
  45.     goal = [[] for i in range(n)]
  46.     for i in range(0, n):
  47.         for j in range(0, n):
  48.             goal[i].append(1)
  49.     initial_state = [[] for i in range(n)]
  50.     for i in range(0, n):
  51.         for j in range(0, n):
  52.             initial_state[i].append(fields[i * n + j])
  53.  
  54.     problem = Table(tuple(tuple(x) for x in initial_state), tuple(tuple(x) for x in goal))
  55.     answer = breadth_first_graph_search(problem)
  56.     print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement