Advertisement
CyberN00b

Untitled

Jan 21st, 2022
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. n, m = map(int, input().split()) # n x m field
  2. field = [[] for y in n] # 0 - dead 1 - active
  3. count_active = 0
  4. for i in range(n):
  5.     for x in input().split():
  6.         if x == 'X':
  7.             count_active += 1
  8.             field[i].append(1)
  9.         else:
  10.             field[i].append(0) # X - dead O - active
  11.        
  12. def get_count(i, j, need, n, m, field):
  13.     count = 0
  14.     if i - 1 >= 0 and field[i - 1][j] == need:
  15.         count += 1
  16.     if j - 1 >= 0 and field[i][j - 1] == need:
  17.         count += 1
  18.     if i + 1 < n and field[i + 1][j] == need:
  19.         count += 1
  20.     if j + 1 < m and field[i][j + 1] == need:
  21.         count += 1
  22.     if i + 1 < n and j + 1 < m and field[i + 1][j + 1] == need:
  23.         count += 1
  24.     if i + 1 < n and j - 1 >= 0 and field[i + 1][j - 1] == need:
  25.         count += 1
  26.     if i - 1 >= 0 and j + 1 < m and field[i - 1][j + 1] == need:
  27.         count += 1
  28.     if i - 1 >= 0 and j - 1 >= 0 and field[i - 1][j - 1] == need:
  29.         count += 1
  30.     return count
  31.  
  32. con = [] # i j k
  33. while count_active != 0:
  34.     for x in con:
  35.         if x[2] == 0:
  36.             count_active -= 1
  37.         field[x[0]][x[1]] = x[2]
  38.     con = []
  39.     for i in range(n):
  40.         for j in range(m):
  41.             if field[i][j] == 0:
  42.                 if get_count(i, j, 1, n, m, field) == 3:
  43.                     con.append([i, j, 1])
  44.             if field[i][j] == 1:
  45.                 t = get_count(i, j, 1, n, m, field)
  46.                 if t >= 4 or t <= 1:
  47.                     con.append([i, j, 0])
  48. for line in field:
  49.     for x in line:
  50.         print('X' if x == 0 else 'O', end=' ')
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement