12016 | 2024-11-24 23:50:19 | 42 | Bináris mátrix | pypy3 | Accepted 100/100 | 597ms | 26148 KiB |
#!/usr/bin/env python3
from sys import stdin
input = stdin.readline
inf=10**9
def solv():
input()
N, M = map(int, input().split())
S = ["" for i in range(N)]
for i in range(N):
S[i] = input().strip()
if M < 3:
row = 0
else:
dp11=[inf]*(M-1)
dp00=[inf]*(M-1)
dp10=[inf]*(M-1)
dp01=[inf]*(M-1)
dp11[0]=2
dp00[0]=0
dp10[0]=1
dp01[0]=1
for i in range(M-2):
cur=set()
for j in range(N):
cur.add(S[j][i:i+3])
ok000 = '000' in cur or '111' in cur
ok100 = '100' in cur or '011' in cur
if not ok000:
dp00[i+1]=dp00[i]
dp11[i+1]=1+dp11[i]
if not ok100:
dp00[i+1]=min(dp00[i+1],dp10[i])
dp11[i+1]=min(dp11[i+1],1+dp01[i])
ok010 = '010' in cur or '101' in cur
ok001 = '001' in cur or '110' in cur
if not ok010:
dp10[i+1]=dp01[i]
dp01[i+1]=1+dp10[i]
if not ok001:
dp10[i+1]=min(dp10[i+1],dp11[i])
dp01[i+1]=min(dp01[i+1],1+dp00[i])
row=min(dp00[-1],dp01[-1],dp10[-1],dp11[-1])
if N < 3:
col = 0
else:
dp11=[inf]*(N-1)
dp00=[inf]*(N-1)
dp10=[inf]*(N-1)
dp01=[inf]*(N-1)
dp11[0]=2
dp00[0]=0
dp10[0]=1
dp01[0]=1
for i in range(N-2):
cur=set()
for j in range(M):
cur.add(S[i][j]+S[i+1][j]+S[i+2][j])
ok000 = '000' in cur or '111' in cur
ok100 = '100' in cur or '011' in cur
if not ok000:
dp00[i+1]=dp00[i]
dp11[i+1]=1+dp11[i]
if not ok100:
dp00[i+1]=min(dp00[i+1],dp10[i])
dp11[i+1]=min(dp11[i+1],1+dp01[i])
ok010 = '010' in cur or '101' in cur
ok001 = '001' in cur or '110' in cur
if not ok010:
dp10[i+1]=dp01[i]
dp01[i+1]=1+dp10[i]
if not ok001:
dp10[i+1]=min(dp10[i+1],dp11[i])
dp01[i+1]=min(dp01[i+1],1+dp00[i])
col=min(dp00[-1],dp01[-1],dp10[-1],dp11[-1])
if row+col<inf:
print(row+col)
else:
print(-1)
for test in range(int(input())):
solv()
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 43ms | 21232 KiB | ||||
subtask2 | 9/9 | ||||||
2 | Accepted | 45ms | 21300 KiB | ||||
3 | Accepted | 50ms | 21204 KiB | ||||
4 | Accepted | 48ms | 21204 KiB | ||||
5 | Accepted | 45ms | 21216 KiB | ||||
6 | Accepted | 50ms | 21228 KiB | ||||
7 | Accepted | 50ms | 21288 KiB | ||||
8 | Accepted | 43ms | 21216 KiB | ||||
9 | Accepted | 43ms | 21268 KiB | ||||
10 | Accepted | 48ms | 21164 KiB | ||||
11 | Accepted | 50ms | 21176 KiB | ||||
12 | Accepted | 45ms | 21180 KiB | ||||
13 | Accepted | 45ms | 21344 KiB | ||||
14 | Accepted | 43ms | 21228 KiB | ||||
15 | Accepted | 50ms | 21228 KiB | ||||
16 | Accepted | 43ms | 21304 KiB | ||||
17 | Accepted | 41ms | 21172 KiB | ||||
subtask3 | 12/12 | ||||||
18 | Accepted | 48ms | 21252 KiB | ||||
19 | Accepted | 64ms | 21800 KiB | ||||
20 | Accepted | 64ms | 22236 KiB | ||||
21 | Accepted | 43ms | 21356 KiB | ||||
22 | Accepted | 52ms | 21152 KiB | ||||
23 | Accepted | 74ms | 22248 KiB | ||||
24 | Accepted | 64ms | 21976 KiB | ||||
25 | Accepted | 63ms | 21960 KiB | ||||
subtask4 | 20/20 | ||||||
26 | Accepted | 45ms | 21300 KiB | ||||
27 | Accepted | 50ms | 21204 KiB | ||||
28 | Accepted | 48ms | 21204 KiB | ||||
29 | Accepted | 45ms | 21216 KiB | ||||
30 | Accepted | 50ms | 21228 KiB | ||||
31 | Accepted | 50ms | 21288 KiB | ||||
32 | Accepted | 43ms | 21216 KiB | ||||
33 | Accepted | 43ms | 21268 KiB | ||||
34 | Accepted | 48ms | 21164 KiB | ||||
35 | Accepted | 50ms | 21176 KiB | ||||
36 | Accepted | 45ms | 21180 KiB | ||||
37 | Accepted | 45ms | 21344 KiB | ||||
38 | Accepted | 43ms | 21228 KiB | ||||
39 | Accepted | 50ms | 21228 KiB | ||||
40 | Accepted | 43ms | 21304 KiB | ||||
41 | Accepted | 41ms | 21172 KiB | ||||
42 | Accepted | 43ms | 21164 KiB | ||||
43 | Accepted | 61ms | 21596 KiB | ||||
44 | Accepted | 78ms | 22316 KiB | ||||
45 | Accepted | 43ms | 21172 KiB | ||||
46 | Accepted | 45ms | 21320 KiB | ||||
47 | Accepted | 68ms | 22252 KiB | ||||
48 | Accepted | 71ms | 22116 KiB | ||||
49 | Accepted | 82ms | 22508 KiB | ||||
50 | Accepted | 72ms | 22504 KiB | ||||
51 | Accepted | 43ms | 21324 KiB | ||||
52 | Accepted | 52ms | 21156 KiB | ||||
53 | Accepted | 72ms | 21740 KiB | ||||
subtask5 | 59/59 | ||||||
54 | Accepted | 52ms | 21140 KiB | ||||
55 | Accepted | 45ms | 21300 KiB | ||||
56 | Accepted | 50ms | 21204 KiB | ||||
57 | Accepted | 48ms | 21204 KiB | ||||
58 | Accepted | 45ms | 21216 KiB | ||||
59 | Accepted | 50ms | 21228 KiB | ||||
60 | Accepted | 50ms | 21288 KiB | ||||
61 | Accepted | 43ms | 21216 KiB | ||||
62 | Accepted | 43ms | 21268 KiB | ||||
63 | Accepted | 48ms | 21164 KiB | ||||
64 | Accepted | 50ms | 21176 KiB | ||||
65 | Accepted | 45ms | 21180 KiB | ||||
66 | Accepted | 45ms | 21344 KiB | ||||
67 | Accepted | 43ms | 21228 KiB | ||||
68 | Accepted | 50ms | 21228 KiB | ||||
69 | Accepted | 43ms | 21304 KiB | ||||
70 | Accepted | 41ms | 21172 KiB | ||||
71 | Accepted | 48ms | 21252 KiB | ||||
72 | Accepted | 64ms | 21800 KiB | ||||
73 | Accepted | 64ms | 22236 KiB | ||||
74 | Accepted | 43ms | 21356 KiB | ||||
75 | Accepted | 52ms | 21152 KiB | ||||
76 | Accepted | 74ms | 22248 KiB | ||||
77 | Accepted | 64ms | 21976 KiB | ||||
78 | Accepted | 63ms | 21960 KiB | ||||
79 | Accepted | 43ms | 21164 KiB | ||||
80 | Accepted | 61ms | 21596 KiB | ||||
81 | Accepted | 78ms | 22316 KiB | ||||
82 | Accepted | 43ms | 21172 KiB | ||||
83 | Accepted | 45ms | 21320 KiB | ||||
84 | Accepted | 68ms | 22252 KiB | ||||
85 | Accepted | 71ms | 22116 KiB | ||||
86 | Accepted | 82ms | 22508 KiB | ||||
87 | Accepted | 72ms | 22504 KiB | ||||
88 | Accepted | 43ms | 21324 KiB | ||||
89 | Accepted | 52ms | 21156 KiB | ||||
90 | Accepted | 72ms | 21740 KiB | ||||
91 | Accepted | 597ms | 26148 KiB | ||||
92 | Accepted | 398ms | 25012 KiB | ||||
93 | Accepted | 170ms | 22992 KiB | ||||
94 | Accepted | 136ms | 23192 KiB | ||||
95 | Accepted | 89ms | 22764 KiB | ||||
96 | Accepted | 93ms | 22508 KiB | ||||
97 | Accepted | 82ms | 22252 KiB | ||||
98 | Accepted | 90ms | 22276 KiB |