-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path289.py
More file actions
61 lines (53 loc) · 2.22 KB
/
289.py
File metadata and controls
61 lines (53 loc) · 2.22 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/5/7 19:05
# @Author : Fhh
# @File : 289.py
# Good good study,day day up!
""""""
from typing import List
class Solution:
def gameOfLife(self, board) -> None:
"""
Do not return anything, modify board in-place instead.
"""
# 把board转化成3位2进制数第一位表示此次扫描是否变化过,第二位表示前一时刻状态,第三位表示当前时刻状态
"初始化"
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == 0:
board[i][j] = '000'
else:
board[i][j] = '001'
# print(board)
def chage(i, j, board):
res = 0
for x in range(i - 1, i + 2):
for y in range(j - 1, j + 2):
if 0 <= x < len(board) and 0 <= y < len(board[0]):
key = board[x][y][2] if board[x][y][0] == '0' else board[x][y][1]
if key == '1':
res += 1
if board[i][j][2] == '1':#如果该位置为存活则活细胞数-1(由于上面统计的是9个格子的活细胞数)
res -= 1
if board[i][j][2] == '1' and (res < 2 or res > 3): # board[i][j]是活细胞且周围细胞小于2或大于3 则死亡
board[i][j] = '110'
elif board[i][j][2] == '1' and (res == 2 or res == 3): # board[i][j]是活细胞且周围细胞为2或3 则仍然存活
board[i][j] = '111'
elif board[i][j][2] == '0' and res == 3: # board[i][j]是死细胞且周围细胞为3个 则死细胞复活
board[i][j] = '101'
# else:
# board[i][j] = '1' + board[i][j][2] * 2
# print('board[i][j]={},res={}'.format(board[i][j],res))
for i in range(len(board)):
for j in range(len(board[0])):
chage(i, j, board)
for i in range(len(board)):
for j in range(len(board[0])):
board[i][j] = int(board[i][j][-1])
# print(board)
s = Solution()
board=[[0, 1, 0],
[0, 0, 1],
[1, 1, 1],
[0, 0, 0]]