-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1014.py
More file actions
42 lines (38 loc) · 1016 Bytes
/
Copy path1014.py
File metadata and controls
42 lines (38 loc) · 1016 Bytes
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
import sys
rl = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(rl())
def makeRow(pre, h):
seat = [1 for _ in range(W)]
for i in range(W):
if pre[i] == 1:
if i != 0: seat[i-1] = 0
if i !=W-1: seat[i+1] = 0
if m[h][i] == "x": seat[i] = 0
out = []
def dfs(n,l):
if n==W: out.append(l); return
if not l:
if seat[n]: dfs(n+1,l+[1])
elif l[-1]==0 and seat[n]: dfs(n+1,l+[1])
dfs(n+1,l+[0])
dfs(0,[])
return out
def makeV(l):
out = 0
for i, x in enumerate(l):
if x: out += 2**i
return out
def cal(pre, v, h):
if h == H: return 0
if dp[h][v] != -1: return dp[h][v]
dp[h][v] = 0
q = makeRow(pre,h)
for qq in q:
dp[h][v] = max(dp[h][v], cal(qq,makeV(qq),h+1)+sum(qq))
return dp[h][v]
for _ in range(N):
H, W = map(int, input().split())
m = [list(input()) for _ in range(H)]
dp = [[-1]*(2**W) for _ in range(H)]
print(cal([0]*W, 0, 0))