Skip to content

Commit 11a79a8

Browse files
committed
solved(python): baekjoon 1014
1 parent eb31108 commit 11a79a8

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

baekjoon/python/1014/__init__.py

Whitespace-only changes.

baekjoon/python/1014/main.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import itertools
2+
import sys
3+
from collections import defaultdict
4+
5+
read = lambda: sys.stdin.readline().rstrip()
6+
7+
8+
class Problem:
9+
def __init__(self):
10+
self.n, self.m = map(int, read().split())
11+
self.grid = [list(map(lambda x: int(x == "."), list(read()))) for _ in range(self.n)]
12+
13+
def solve(self) -> None:
14+
dp = [defaultdict(int) for _ in range(self.n)]
15+
for state in self.find_states(0):
16+
dp[0][state] = self.count_one(state)
17+
18+
for row in range(1, self.n):
19+
for current, previous in itertools.product(self.find_states(row), dp[row - 1]):
20+
if (current & (previous << 1)) == (current & (previous >> 1)) == 0:
21+
dp[row][current] = max(dp[row][current], dp[row - 1][previous] + self.count_one(current))
22+
23+
print(max([x for x in dp[-1].values()]))
24+
25+
def find_states(self, row: int) -> list[int]:
26+
blocked = 0
27+
for col in range(self.m):
28+
if self.grid[row][col] == 0:
29+
blocked |= 1 << col
30+
31+
return [state for state in range(1 << self.m) if (state & blocked) == 0 and (state & (state >> 1)) == 0]
32+
33+
def count_one(self, num: int):
34+
return bin(num).count("1")
35+
36+
37+
if __name__ == "__main__":
38+
for _ in range(int(read())):
39+
Problem().solve()

baekjoon/python/1014/sample.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"input": [
4+
"4",
5+
"2 3",
6+
"...",
7+
"...",
8+
"2 3",
9+
"x.x",
10+
"xxx",
11+
"2 3",
12+
"x.x",
13+
"x.x",
14+
"10 10",
15+
"....x.....",
16+
"..........",
17+
"..........",
18+
"..x.......",
19+
"..........",
20+
"x...x.x...",
21+
".........x",
22+
"...x......",
23+
"........x.",
24+
".x...x...."
25+
],
26+
"expected": [
27+
"4",
28+
"1",
29+
"2",
30+
"46"
31+
]
32+
}
33+
]

baekjoon/python/1014/test_main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
import os.path
3+
import sys
4+
import unittest
5+
from io import StringIO
6+
from unittest.mock import patch
7+
8+
from parameterized import parameterized
9+
10+
from main import Problem
11+
12+
13+
def load_sample(filename: str):
14+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
15+
16+
with open(path, "r") as file:
17+
return [(case["input"], case["expected"]) for case in json.load(file)]
18+
19+
20+
class TestCase(unittest.TestCase):
21+
@parameterized.expand(load_sample("sample.json"))
22+
def test_case(self, case: str, expected: list[str]):
23+
# When
24+
with (
25+
patch("sys.stdin.readline", side_effect=case),
26+
patch("sys.stdout", new_callable=StringIO) as output,
27+
):
28+
for _ in range(int(sys.stdin.readline().rstrip())):
29+
Problem().solve()
30+
31+
result = output.getvalue().rstrip()
32+
33+
# Then
34+
self.assertEqual("\n".join(expected), result)
35+
36+
37+
if __name__ == "__main__":
38+
unittest.main()

0 commit comments

Comments
 (0)