AcWing 95. 【python】费解的开关
原题链接
中等
作者:
tt2767
,
2019-12-08 15:05:33
,
所有人可见
,
阅读 933
import sys
import re
from copy import deepcopy
lights = [[0] * 5 for _ in range(5)]
def read(raw):
sbuff = list()
for s in raw:
if re.match('\s', s) is None:
sbuff.append(s)
elif len(sbuff) > 0:
yield ''.join(sbuff)
sbuff = list()
yield ''.join(sbuff)
dx = [0, 0, 0, 1, -1]
dy = [0, 1, -1 ,0 ,0]
def filp(lights, x, y):
for i in range(5):
nx = x + dx[i]
ny = y + dy[i]
if 0<= nx <5 and 0 <= ny < 5:
lights[nx][ny] ^= 1
def calc(lights, s):
res = 0
for i in range(5):
if 1 == s >> i & 1:
filp(lights, 0, i)
res += 1
for i in range(1, 5):
for j in range(5):
if lights[i-1][j] == 0:
filp(lights, i, j)
res += 1
for j in range(5):
if lights[4][j] == 0:
return 7
return res
def work(lights):
ans = float('inf')
for s in range(1 << 5):
ans = min(ans, calc(deepcopy(lights), s))
return ans
raw = sys.stdin.read()
pin = read(raw)
n = int(pin.next())
for _ in range(n):
for i in range(5):
lights[i] = map(int, pin.next())
min_step = work(lights)
print min_step if min_step <=6 else -1
为何不写成这样:
[[0]*5]*5