init · thoongee/algorithm-python@337bad0 · GitHub
Skip to content

Commit 337bad0

Browse files
committed
init
1 parent deeb339 commit 337bad0

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

DivideAndConquer/1629.py

Lines changed: 21 additions & 0 deletions

DivideAndConquer/1780.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
input = sys.stdin.read
3+
4+
def count_paper(x, y, n):
5+
if n == 1:
6+
counts[grid[x][y]] += 1
7+
return
8+
9+
# 현재 종이가 모두 같은 수로 이루어져 있는지 확인
10+
init_val = grid[x][y]
11+
all_same = True
12+
for i in range(x, x + n):
13+
if not all_same:
14+
break
15+
for j in range(y, y + n):
16+
if grid[i][j] != init_val:
17+
all_same = False
18+
break
19+
20+
# 모두 같은 수로 이루어져 있다면 해당 숫자의 카운트를 증가
21+
if all_same:
22+
counts[init_val] += 1
23+
else:
24+
# 같은 수로만 이루어져 있지 않다면 9개로 분할하여 각각 재귀적으로 처리
25+
new_size = n // 3
26+
for i in range(3):
27+
for j in range(3):
28+
count_paper(x + i * new_size, y + j * new_size, new_size)
29+
30+
# 입력 받기
31+
data = sys.stdin.read().split()
32+
N = int(data[0])
33+
grid = [list(map(int, data[i * N + 1: (i + 1) * N + 1])) for i in range(N)]
34+
35+
# 각 숫자 카운트를 저장할 딕셔너리
36+
counts = {-1: 0, 0: 0, 1: 0}
37+
38+
# 전체 종이에 대해 함수 호출
39+
count_paper(0, 0, N)
40+
41+
# 결과 출력
42+
print(counts[-1])
43+
print(counts[0])
44+
print(counts[1])

DivideAndConquer/1992.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
input = sys.stdin.read
3+
4+
def compress(x, y, n):
5+
if n == 1:
6+
return str(grid[x][y])
7+
8+
# 첫 번째 원소를 기준으로 모든 원소가 같은지 확인
9+
initial = grid[x][y]
10+
for i in range(x, x + n):
11+
for j in range(y, y + n):
12+
if grid[i][j] != initial:
13+
# 사분면으로 나누기
14+
next_n = n // 2
15+
top_left = compress(x, y, next_n)
16+
top_right = compress(x, y + next_n, next_n)
17+
bottom_left = compress(x + next_n, y, next_n)
18+
bottom_right = compress(x + next_n, y + next_n, next_n)
19+
return f"({top_left}{top_right}{bottom_left}{bottom_right})"
20+
21+
# 모든 원소가 같은 경우
22+
return str(initial)
23+
24+
# 입력 처리
25+
data = input().strip().split()
26+
N = int(data[0])
27+
grid = [list(map(int, list(data[i + 1]))) for i in range(N)]
28+
29+
# 압축 결과 출력
30+
result = compress(0, 0, N)
31+
print(result)

DivideAndConquer/2447.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
sys.setrecursionlimit(10**6) #10^6으로 최대 재귀 깊이 설정
3+
input = sys.stdin.readline
4+
5+
def star(l):
6+
if l == 1:
7+
return['*']
8+
9+
Stars = star(l//3)
10+
L = []
11+
12+
for s in Stars:
13+
L.append(s*3) # N/3 패턴으로 '둘러싼' -> 둘러싸려면 *3
14+
for s in Stars:
15+
L.append(s+' '*(l//3)+s) # N/3의 공백
16+
for s in Stars:
17+
L.append(s*3) # N/3 패턴으로 '둘러싼' -> 둘러싸려면 *3
18+
return L
19+
20+
n = int(input().strip())
21+
print('\n'.join(star(n)))

DivideAndConquer/2630.py

Lines changed: 52 additions & 0 deletions

0 commit comments

Comments
 (0)