-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_simulation_nm.py
35 lines (33 loc) · 1.11 KB
/
AC_simulation_nm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_simulation_nm.py
# Create Date: 2015-10-28 16:30:24
class Solution(object):
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""
dx = [-1, 0, 1, -1, 1, -1, 0, 1]
dy = [-1, -1, -1, 0, 0, 1, 1, 1]
n, m = len(board), len(board[0])
# compute the result to second last bit
for i in xrange(n):
for j in xrange(m):
neighbors = 0
for k in xrange(8):
x, y = i + dx[k], j + dy[k]
if 0 <= x < n and 0 <= y < m and (board[x][y] & 1):
neighbors += 1
if (neighbors == 2 and (board[i][j] & 1)) or (neighbors == 3):
board[i][j] |= 2
# restore the state
for i in xrange(n):
for j in xrange(m):
board[i][j] >>= 1
# test
s = Solution()
b = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
s.gameOfLife(b)
print(b)