题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
python 代码
import sys
import collections
from copy import deepcopy
n = int(input().split()[0])
#print(n)
moves = [(0,0),(0,1),(1,0),(0,-1),(-1,0)]
def turn(nums, i,j):
for x,y in moves:
newx, newy = i + x, j + y
if 0 <= newx < 5 and 0<= newy < 5:
nums[newx][newy] ^= 1
def cal(lights, k):
res = 0
for i in range(5):
if 1 == k >> i & 1: # 枚举状态 k 右移i位和1& 这句话的作用就是判断当前位是否是1 1表示当前位要操作 0 表示不操作 这个操作的含义就是点击, 要区别亮灭的含义
turn(lights, 0, i)
res += 1 # ok确定好第一行了
for i in range(1, 5): # 依据第一行来,从第二行开始
for j in range(5):
if lights[i-1][j] == 0:
turn(lights, i, j)
res += 1
for j in range(5):
if lights[-1][j] == 0: return 7
return res
'''
nums = []
for _ in range(6): # read data
tmp = []
arr = sys.stdin.readline().split()
print(arr)
if not arr: continue
else:
arr = arr[0]
for j in range(5):
tmp.append(int(arr[j]))
nums.append(tmp)
print(nums)
'''
while n > 0:
res = float("inf")
nums = []
for _ in range(5): # read data
tmp = []
arr = sys.stdin.readline().split()
if not arr: continue
else:
arr = arr[0]
for j in range(5):
tmp.append(int(arr[j]))
nums.append(tmp)
for k in range(1 << 5): # 枚举第一行按还是不按的状态
count = cal(deepcopy(nums), k)
res = min(res, count)
a = sys.stdin.readline().split()
if res <=6 : print(res)
else:
print(-1)
n -=1