Advertisement
Guest User

Black - White [AI]

a guest
Apr 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. class CrnoBelo(Problem):
  2.     def __init__(self, initial, goal):
  3.         self.initial = str(initial)
  4.         self.goal = str(goal)
  5.  
  6.     def goal_test(self, state):
  7.         return state == self.goal
  8.  
  9.     def successor(self, state):
  10.         successors = dict()
  11.         dx = [-1, 0, 1, 0]
  12.         dy = [0, 1, 0, -1]
  13.         for x in range(0, N):
  14.             for y in range(0, N):
  15.                 Matrix = eval(state)
  16.                 Matrix[x][y] = 1 - Matrix[x][y]
  17.                 for k in range(4):
  18.                     newX = x + dx[k]
  19.                     newY = y + dy[k]
  20.                     if newX >= 0 and newX < N and newY >= 0 and newY < N:
  21.                         Matrix[newX][newY] = (Matrix[newX][newY] + 1) % 2
  22.                 stateNew = Matrix
  23.                 akcija = "x: " + str(x) + ", y: " + str(y)
  24.                 successors[akcija] = str(stateNew)
  25.  
  26.         return successors
  27.  
  28.     def actions(self, state):
  29.         return self.successor(state).keys()
  30.  
  31.     def result(self, state, action):
  32.         possible = self.successor(state)
  33.         return possible[action]
  34.  
  35.  
  36. if __name__ == '__main__':
  37.     N = int(input())
  38.     polinja = map(int, input().split(','))
  39.     polinjaList = list(polinja)
  40.     InitialMatrix = [[0 for x in range(N)] for y in range(N)]
  41.     for i in range(0, N):
  42.         for j in range(0, N):
  43.             InitialMatrix[i][j] = polinjaList[i * N + j]
  44.     GoalMatrix = [[1 for x in range(N)] for y in range(N)]
  45.  
  46.     crnoBeloinstance = CrnoBelo(InitialMatrix, GoalMatrix)
  47.     answer = breadth_first_graph_search(crnoBeloinstance).solution()
  48.     print(answer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement