/images/jg_02.jpg

SW Expert Academy_D3 10761, 10804, 10912, 10965

D3_10761_신뢰

 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
45
46
47
48
49
50
51
from collections import deque

for T in range(int(input())):
    command = deque(input().split())
    N = command.popleft()
    
    B = deque()
    O = deque()
    order = deque()
    while command:
        i = command.popleft()
        if i == "B":
            B.append(int(command.popleft()))
            order.append("B")
        else:
            O.append(int(command.popleft()))
            order.append("O")

    count = 0
    b, o = 1, 1
    while True:
        if not order:
            break
        count += 1

        BP = "안눌림"
        if B:
            if B[0] > b:
                b += 1
            elif B[0] == b and order[0] == "B":
                B.popleft()
                order.popleft()
                BP = "눌림"
            elif B[0] < b:
                b -= 1
        
        if O:
            if O[0] > o:
                o += 1
            elif O[0] == o and order[0] == "O" and BP == "안눌림":
                O.popleft()
                order.popleft()
            elif O[0] < o:
                o -= 1

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


# 주어진 순서대로 버튼을 눌러야 한다!!!
# 첫번째 테스트의 경우 O에 1이 있으나, B2가 눌리기를 기다려야 한다.
# order 리스트가 필요한 이유

BEAKJOON 2178, 7576, 1697

2178_미로탐색

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

def isSafe(x, y):
    if 0 <= x < N and 0 <= y < M:
        return True

def BFS():
    while q:
        x, y = q.popleft()
        visited[x][y] = 1
        for i in range(4):
            cx = x + dx[i]
            cy = y + dy[i]
            if isSafe(cx, cy) and arr[cx][cy] == 1 and visited[cx][cy] == 0:
                q.append([cx, cy])
                arr[cx][cy] = arr[x][y] + 1
    

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

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

arr = [list(map(int, sys.stdin.readline().rstrip())) for _ in range(N)]
visited = [[0]*(M) for _ in range(N)]
q = deque([[0, 0]])

BFS()

print(arr[N-1][M-1])


# pop을 사용하는데 리스트형태의 자료인 경우
# x, y = [1, 2]의 모습으로도 추출이 가능

BEAKJOON 1260, 2606, 2667, 1012

1260_DFS와 BFS

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

def DFS(V):
    print(V, end=' ')
    DFSv[V] = 1
    for i in range(1, N+1):
        if DFSv[i] == 0 and tree[V][i] == 1:
            DFS(i)
    return


def BFS(V):
    q = deque([V])
    BFSv[V] = 1
    while q:
        p = q.popleft()
        print(p, end=" ")
        for i in range(1, N+1):
            if BFSv[i] == 0 and tree[p][i] == 1:
                q.append(i)
                BFSv[i] = 1
    return
            

N,M,V = map(int, sys.stdin.readline().split())
tree = [[0]*(N+1) for _ in range(N+1)]
DFSv = [0 for _ in range(N+1)]
BFSv = [0 for _ in range(N+1)]

for _ in range(M):
    S, E = map(int, sys.stdin.readline().split())
    tree[S][E] = 1
    tree[E][S] = 1


DFS(V)
print()
BFS(V)

# 기초가 제일 중요하다.
# 혹시 DFS, BFS가 막힐 땐 이 문제를 다시 풀어보자

BEAKJOON 10989, 11650, 2108, 10814

10989_수 정렬하기3

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

# 시간 초과
# from collections import deque
#
# result = deque()
# for T in range(int(sys.stdin.readline().rstrip())):
#     n = int(sys.stdin.readline().rstrip())
#     if len(result) != 0:
#         for i in range(len(result)):
#             if result[i] > n:
#                 result.insert(i, n)
#                 break
#     else:
#         result.append(n)
            
# for i in result:
#     print(i)

# 속도엔 DP지.
arr = [0] * 10001
for T in range(int(sys.stdin.readline().rstrip())):
    n = int(sys.stdin.readline().rstrip())
    arr[n] += 1

for i in range(10001):
    if arr[i] != 0:
        for j in range(arr[i]):
            print(i)

BEAKJOON 2798, 7568, 1018, 1436

2798_블랙잭

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import sys

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

result = 0
for i in range(N):
    for j in range(i+1, N):
        for x in range(j+1, N):
            if cards[i] + cards[j] + cards[x] <= M:
                result = max(result, cards[i] + cards[j] + cards[x])

print(result)

7568_덩치

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

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

# 내가 생각한 무식한 방법...
W = deque()
H = deque()
for _ in range(N):
    x, y = sys.stdin.readline().split()
    W.append(x)
    H.append(y)

for i in range(N):
    w = W.popleft()
    h = H.popleft()

    count = 0
    for j in range(N-1):
        if W[j] > w and H[j] > h:
            count += 1
    print(count+1, end=" ")
    
    W.append(w)
    H.append(h)


# 인터넷에 있던 멋진 방법
# li = deque()

# for _ in range(N):
#     x, y = sys.stdin.readline().split()
#     li.append((x, y))

# for i in li:
#     count = 0
#     for j in li:
#         if i[0] < j [0] and i[1] < j[1]:
#             count += 1
#     print(count+1, end=" ")


# 하지만 내가 짠 코드가 더 빠르다.

BEAKJOON 10872, 10870, 2447, 11729

10872_팩토리얼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import sys

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

def fac(n):
    if n > 1:
        return n * fac(n-1)
    else:
        return 1

print(fac(N))

10870_피보나치 수 5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import sys

def fivo(n):
    if n >= 2:
        return fivo(n-1) + fivo(n-2)
    else:
        return n

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

print(fivo(N))