/images/jg_02.jpg

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))

BEAKJOON 4948, 9020, 3053, 1002

4948_베르트랑 공준

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

while True:
    N = int(sys.stdin.readline().rstrip())

    if N == 0:
        break

    arr = [0,0] + [1] * ((2*N)-1)
    rootN = int((2*N)**0.5)

    for i in range(2, rootN+1):
        if arr[i] == 1:
            for j in range(2*i, 2*N+1, i):
                arr[j] = 0
    
    print(sum(arr[N+1:(2*N)+1]))

# 입출력에 제한이 있다가 0을 입력 받을 때 까지
# 출력을 해야 하는 조건을 달지 않는 것을 주의!