Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. def step5(grid, n):
  2.     for i in range(n):
  3.         for j in range(n):
  4.             mine_found = False
  5.             if grid[i][j] != -1:
  6.                 a = [1, 1, -1, -1]
  7.                 b = [1, -1, 1, -1]
  8.  
  9.                 for x in range(len(a)):
  10.                     if i + a[x] >= 0 and j + b[x] >= 0 and i + a[x] < n and j + b[x] < n:
  11.                         if grid[i + a[x]][j + b[x]] == -1:
  12.                             mine_found = True
  13.             if mine_found is True:
  14.                 grid[i][j] *= 2
  15.  
  16. def solve_minesweeper(puzzle_array):
  17.     n = len(puzzle_array)
  18.  
  19.     mines = list()
  20.     rows_mines = set()
  21.     ret = [None] * n
  22.  
  23.     for i in range(n):
  24.         ret[i] = [0] * n
  25.         for j in range(n):
  26.             if puzzle_array[i][j] == 'm':
  27.                 mines.append((i, j))
  28.                 ret[i][j] = -1
  29.                 if i % 2 != 0:
  30.                     rows_mines.add(i)
  31.             else:
  32.                 ret[i][j] = 0
  33.  
  34.     for i, j in mines:
  35.         for idx in range(-1, 2):
  36.             for jdx in range(-1, 2):
  37.                 if not (idx == 0 and jdx == 0):
  38.                     ri = i + idx
  39.                     rj = j + jdx
  40.  
  41.                     if ri >= 0 and rj >= 0 and ri < n and rj < n:
  42.                         if ret[ri][rj] != -1:
  43.                             ret[ri][rj] += 1
  44.  
  45.                         if idx == 1 and jdx == 0:
  46.                             if ret[ri][rj] != -1:
  47.                                 ret[ri][rj] = 2
  48.  
  49.                         if idx == 0 and jdx == 1:
  50.                             if ret[ri][rj] != -1:
  51.                                 ret[ri][rj] = 0
  52.  
  53.     for row in rows_mines:
  54.         for j in range(n):
  55.             if ret[row][j] != -1:
  56.                 ret[row][j] *= 3
  57.  
  58.     step5(ret, n)
  59.     print(type(ret[0]))
  60.     return ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement