/images/jg_02.jpg

BEAKJOON 2630, 1992

2630_색종이 만들기

 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
import sys

def DFS(x, y, N):
    global W, B

    color = arr[x][y]
    for cx in range(x, x+N):
        for cy in range(y, y+N):
            if arr[cx][cy] != color:
                DFS(x, y, N//2)
                DFS(x, N//2+y, N//2)
                DFS(N//2+x, y, N//2)
                DFS(N//2+x, N//2+y, N//2)
                return		# 이걸 안해주면 쓸모 없는 DFS에 더 들어가게 된다.
                
    if color == 0:
        W += 1
        return
    else:
        B += 1
        return

N = int(sys.stdin.readline().rstrip())

arr = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

W, B = 0, 0 

DFS(0, 0, N)
print(W)
print(B)

# 분할한 구역에도 동일한 규칙을 적용할 수 있는 
# 알고리즘을 만드는 것이 기본 같다.

BEAKJOON 5052, 9372

5052_전화번호 목록

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys

for T in range(int(sys.stdin.readline().rstrip())):
    N = int(sys.stdin.readline().rstrip())

    Numbers = []
    for _ in range(N):
        Numbers.append(sys.stdin.readline().rstrip())

    Numbers.sort()

    result = "YES"
    for i in range(len(Numbers)-1):
        if Numbers[i+1].find(Numbers[i], 0, len(Numbers[i])) != -1:
            result = 'NO'
            break

    print(result)

    
# 흠.. 다른 사람들은 트리로 풀었나..?
# 이게 왜 트리에 있지...

BEAKJOON 1991, 11725, 1967, 1167

1991_트리 순회

 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
43
44
class Node:
    def __init__(self, data, L, R):
        self.data = data
        self.left = L
        self.right = R

def preorder(node):     # 전위 순회
    print(node.data, end="")
    if node.left != ".":
        preorder(tree[node.left])
    if node.right != ".":
        preorder(tree[node.right])

def inorder(node):      # 중위 순회
    if node.left != ".":
        inorder(tree[node.left])
    print(node.data, end="")
    if node.right != ".":
        inorder(tree[node.right])

def postorder(node):      # 후위 순회
    if node.left != ".":
        postorder(tree[node.left])
    if node.right != ".":
        postorder(tree[node.right])
    print(node.data, end="")

N = int(input())

tree = {}
for _ in range(N) :
    data, L, R = input().split()
    tree[data] = Node(data, L, R)


preorder(tree['A'])
print()
inorder(tree['A'])
print()
postorder(tree['A'])

# 트리 구조에 대한 이해가 부족하다.
# 트리문제를 많이 풀어봐야 겠다.
# 해보니 복잡?하지는 않은데 익숙하지 않은 느낌이다.

SW Expert Academy_D4 3143, 10966, 11592, 11545

D4_3143_가장 빠른 문자열 타이핑

1
2
3
4
5
6
7
for T in range(int(input())):
    A, B = input().split()
    result = len(A) - (A.count(B)*(len(B)-1))
            
    print(f'#{T+1} {result}')
    
# 왜 D4 인가.

D4_10966_물놀이를 가자

 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
from collections import deque

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

for T in range(int(input())):
    N, M = map(int, input().split())
    arr = [input() for _ in range(N)]
    visited = [[-1 for _ in range(M)] for _ in range(N)]

    q = deque([])
    for i in range(N):
        for j in range(M):
            if arr[i][j] == "W":
                q.append([i,j])
                visited[i][j] = 0
    
    total = 0
    while q:
        x, y = q.popleft()
        for i in range(4):
            cx = x + dx[i]
            cy = y + dy[i]            
            if 0<= cx <N and 0<= cy <M and visited[cx][cy] == -1:
                visited[cx][cy] = visited[x][y] + 1
                q.append([cx, cy])
                total += visited[cx][cy]

    print(f'#{T+1} {total}')

# 오랜만에 다중 시작점 BFS

BEAKJOON 15649, 15650

15649_N과 M (1)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
N, M = map(int, input().split())

def DFS(count):
    if count == M:
        print(*arr)
        return

    for i in range(N):
        if visited[i] == True:
            continue

        visited[i] = True
        arr.append(num_list[i])
        DFS(count+1)
        arr.pop()
        visited[i] = False

num_list = [i + 1 for i in range(N)]
visited = [False] * N
arr = []

DFS(0)

15650_N과 M (2)

 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
import sys

def DFS(count):
    if count == M:
        print(*arr)
        return

    for i in range(N):
        if visited[i] == True:
            continue

        visited[i] = True
        arr.append(num_list[i])
        DFS(count+1)
        arr.pop()

        for j in range(i+1, N):
            visited[j] = False

N, M = map(int, sys.stdin.readline().split())

num_list = [i+1 for i in range(N)]
visited = [False] * N
arr = []

DFS(0)

SW Expert Academy_D4 1258, 1249, 1238, 4261

D4_1258_행렬찾기

 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
def check(x, y):
    dx, dy = 0, 0
    while x + dx < N and arr[x+dx][y]:
        dx += 1
    while y + dy < N and arr[x][y+dy]:
        dy += 1
    result.append([dx*dy, dx, dy])
            
    for i in range(x, x+dx):
        for j in range(y, y+dy):
            arr[i][j] = 0

for T in range(int(input())):
    N = int(input())
    arr = [list(map(int, input().split())) for _ in range(N)]

    result = []
    for i in range(N):
        for j in range(N):
            if arr[i][j] != 0:
                check(i, j)
    
    aws = sorted(result, key=lambda x: [x[0], x[1]])
    print(f'#{T+1} {len(aws)}', *[str(i[1]) + ' ' + str(i[2]) for i in aws])

    
# DFS로 구현해보려 했는데.
# 내가 원하는 위치값을 반환하고 바로 종료시키는 것이
# 불가능해서. 최소한의 값인 가로 세로 이동값만을 뽑아
# 정리하였다.
# 마지막에 정렬이 더 복잡했다..