๋ฌธ์ œ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


ํ•ด์ปค ๊น€์ง€๋ฏผ์€ ์ž˜ ์•Œ๋ ค์ง„ ์–ด๋Š ํšŒ์‚ฌ๋ฅผ ํ•ดํ‚นํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํšŒ์‚ฌ๋Š” N๊ฐœ์˜ ์ปดํ“จํ„ฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊น€์ง€๋ฏผ์€ ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๋ฒˆ์˜ ํ•ดํ‚น์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚น ํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

์ด ํšŒ์‚ฌ์˜ ์ปดํ“จํ„ฐ๋Š” ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„์™€, ์‹ ๋ขฐํ•˜์ง€ ์•Š๋Š” ๊ด€๊ณ„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๋ฐ, A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” B๋ฅผ ํ•ดํ‚นํ•˜๋ฉด, A๋„ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์†Œ๋ฆฌ๋‹ค.

์ด ํšŒ์‚ฌ์˜ ์ปดํ“จํ„ฐ์˜ ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ•œ ๋ฒˆ์— ๊ฐ€์žฅ ๋งŽ์€ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

 

 

๐Ÿ’ก ์ž…๋ ฅ


์ฒซ์งธ ์ค„์—, N๊ณผ M์ด ๋“ค์–ด์˜จ๋‹ค. N์€ 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜, M์€ 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ A B์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ๋“ค์–ด์˜ค๋ฉฐ, "A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•œ๋‹ค"๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ปดํ“จํ„ฐ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ํ•˜๋‚˜์”ฉ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค.

 

 

 

 

 

๐Ÿ“Œ ์ถœ๋ ฅ


์ฒซ์งธ ์ค„์—, ๊น€์ง€๋ฏผ์ด ํ•œ ๋ฒˆ์— ๊ฐ€์žฅ ๋งŽ์€ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•˜๋Š” ํ˜•์‹์ด๋ฏ€๋กœ ์ž…๋ ฅ์„ x, y๋กœ ๋ฐ›๊ณ  arr[y].append(x)๋กœ ํ•ด์ฃผ์ž.

bfs๋ฅผ ๋Œ๋ฆด ๋•Œ arr[y]์— ์—ฐ๊ฒฐ๋œ ์• ๋“ค์ด vistited ๋œ์ ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ณ  ์—†์œผ๋ฉด ๋ฐฉ๋ฌธ์„ ์ฐ๊ณ  ๋ฑ์— ์ฒจ๊ฐ€ํ•ด์ฃผ๋Š” ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ

 

from collections import deque

n, m = map(int, input().split())

arr = [[] for _ in range(n+1)]

for _ in range(m):
    x, y = map(int, input().split())
    arr[y].append(x)

def bfs(v):
    q = deque([v])
    visited = [False] * (n + 1)
    visited[v] = True
    count = 1
    while q:
        v = q.popleft()
        for e in arr[v]:
            if not visited[e]:
                q.append(e)
                visited[e] = True
                count += 1
    return count

result = []
max_value = -1

for i in range(1, n+1):
    c = bfs(i)
    if c > max_value:
        result=[i]
        max_value=c
    elif c == max_value:
        result.append(i)
        max_value=c

for e in result:
    print(e, end=" ")

 

 

์‹œ๊ฐ„ ์ดˆ๊ณผ ๋จธ์„ ์ผ์ด๊ตฌ,,,

๋ฌธ์ œ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๋‚˜๋Š” ํ•ด์ถฉ ๋ฐฉ์ง€์— ํšจ๊ณผ์ ์ธ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๋ฅผ ๊ตฌ์ž…ํ•˜๊ธฐ๋กœ ๊ฒฐ์‹ฌํ•œ๋‹ค. ์ด ์ง€๋ ์ด๋Š” ๋ฐฐ์ถ”๊ทผ์ฒ˜์— ์„œ์‹ํ•˜๋ฉฐ ํ•ด์ถฉ์„ ์žก์•„ ๋จน์Œ์œผ๋กœ์จ ๋ฐฐ์ถ”๋ฅผ ๋ณดํ˜ธํ•œ๋‹ค. ํŠนํžˆ, ์–ด๋–ค ๋ฐฐ์ถ”์— ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ๋ผ๋„ ์‚ด๊ณ  ์žˆ์œผ๋ฉด ์ด ์ง€๋ ์ด๋Š” ์ธ์ ‘ํ•œ ๋‹ค๋ฅธ ๋ฐฐ์ถ”๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์–ด, ๊ทธ ๋ฐฐ์ถ”๋“ค ์—ญ์‹œ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ํ•œ ๋ฐฐ์ถ”์˜ ์ƒํ•˜์ขŒ์šฐ ๋„ค ๋ฐฉํ–ฅ์— ๋‹ค๋ฅธ ๋ฐฐ์ถ”๊ฐ€ ์œ„์น˜ํ•œ ๊ฒฝ์šฐ์— ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

ํ•œ๋‚˜๊ฐ€ ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋Š” ๋•…์€ ๊ณ ๋ฅด์ง€ ๋ชปํ•ด์„œ ๋ฐฐ์ถ”๋ฅผ ๊ตฐ๋ฐ๊ตฐ๋ฐ ์‹ฌ์–ด ๋†“์•˜๋‹ค. ๋ฐฐ์ถ”๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ณณ์—๋Š” ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ๋งŒ ์žˆ์œผ๋ฉด ๋˜๋ฏ€๋กœ ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋Š” ๋ฐฐ์ถ”๋“ค์ด ๋ช‡ ๊ตฐ๋ฐ์— ํผ์ ธ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•˜๋ฉด ์ด ๋ช‡ ๋งˆ๋ฆฌ์˜ ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•œ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์ถ”๋ฐญ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ์ตœ์†Œ 5๋งˆ๋ฆฌ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 0์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ์ง€ ์•Š์€ ๋•…์ด๊ณ , 1์€ ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ๋•…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

 

๐Ÿ’ก ์ž…๋ ฅ


์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ์ฒซ์งธ ์ค„์—๋Š” ๋ฐฐ์ถ”๋ฅผ ์‹ฌ์€ ๋ฐฐ์ถ”๋ฐญ์˜ ๊ฐ€๋กœ๊ธธ์ด M(1 ≤ M ≤ 50)๊ณผ ์„ธ๋กœ๊ธธ์ด N(1 ≤ N ≤ 50), ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ์œ„์น˜์˜ ๊ฐœ์ˆ˜ K(1 ≤ K ≤ 2500)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ K์ค„์—๋Š” ๋ฐฐ์ถ”์˜ ์œ„์น˜ X(0 ≤ X ≤ M-1), Y(0 ≤ Y ≤ N-1)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฐฐ์ถ”์˜ ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

 

 

 

๐Ÿ“Œ ์ถœ๋ ฅ


๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ์˜ ๋ฐฐ์ถ”ํฐ์ง€๋ ์ด ๋งˆ๋ฆฌ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


์šฐ์„  ๋ฐฐ์ถ”๋ฐญ์„ 0์œผ๋กœ ๋‹ค ์ฑ„์›Œ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ์™€ ๊ฐ™์€ ํฌ๊ธฐ๋กœ false ์ธ์ง€ true์ธ์ง€๋ฅผ ๋‹ด์•„์ค„ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค. ( ์™ผ์ชฝ ์ƒ๋‹จ์ด (0, 0)์ด ๋˜๊ฒŒ )

array=[[0]*m for _ in range(n)]
visited = [[False]*m for _ in range(n)]

 

๊ทธ๋ฆฌ๊ณ  k๋ฒˆ์”ฉ ๋Œ๋ฉด์„œ x, y ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋ฐ›์•„์ค€๋‹ค.

for _ in range(k):
    y, x = map(int,input().split())
    array[x][y] = 1

 

๊ฐ€๋กœ๋กœ m๊นŒ์ง€ ์„ธ๋กœ๋กœ n๊นŒ์ง€ for๋ฌธ์„ ๋Œ๋ฉด์„œ array์™€ visited๋ฅผ ํ†ตํ•ด ๋ฐฐ์ถ”๋Š” ์‹ฌ์–ด์ ธ์žˆ์ง€๋งŒ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์„ ์ฐพ์•„ ๋ฐฉ๋ฌธํ•ด์ฃผ๋Š” dfs๋‚˜ bfs๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด๋œ๋‹ค.

for i in range(n):
    for j in range(m):
        if array[i][j] and not visited[i][j]:
            dfs(i,j) ๋‚˜ bfs(i,j)

 

 

1. DFS๋กœ ํ’€ ๊ฒฝ์šฐ

์žฌ๊ท€ ์šฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. 

 

ํŒŒ์ด์ฌ์˜ ์žฌ๊ท€ ๊นŠ์ด ์ œํ•œ์€ 1000์œผ๋กœ ๋งค์šฐ ์–•๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ฌธ์ œ๋ฅผ ๊ทธ๋ƒฅ ํ‘ผ๋‹ค๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

ํŒŒ์ด์ฌ์œผ๋กœ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด ํ’€์–ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋ผ๋ฉด setrecursionlimit๋ฅผ ์“ฐ๋Š” ๊ฒƒ์€ ์„ ํƒ์ด ์•„๋‹Œ ํ•„์ˆ˜๋‹ค.

 

  • ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์ •ํ•ด์ฃผ๊ณ  ์ด๋ฅผ for๋ฌธ์œผ๋กœ ๋Œ๋ฆฐ๋‹ค.
  • ๊ฐ ์œ„์น˜์˜ ๊ฐ’์ด 0๋ณด๋‹ค ํฌ๊ณ  ์ตœ๋Œ€ ๊ธธ์ด๋ณด๋‹ค ์ž‘์€์ง€ 1์ฐจ์ ์œผ๋กœ ํ™•์ธ์„ ํ•ด์ฃผ๊ณ , ์ด์— ํ•ด๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด continue๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค์Œ ์œ„์น˜๋กœ ๋„˜๊ฒจ์ฃผ๊ฒŒ for๋ฌธ์œผ๋กœ ๋˜์ง„๋‹ค.
  • ๋งž๋‹ค๋ฉด ์ด์   ํ™•์ธ์„ ํ•ด์ค€๋‹ค. ์ด๊ณณ์— ๋ฐฐ์ถ”๊ฐ€ ์žˆ๊ณ  ๋ฐฉ๋ฌธํ•œ ์  ์ด ์—†๋Š”๊ฐ€?

 

# ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
# ๋งŒ์•ฝ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋ผ๋ฉด setrecursionlimit์„ ์“ฐ๋Š” ๊ฒƒ์€ ์„ ํƒ์ด ์•„๋‹Œ ํ•„์ˆ˜๋‹ค.
# ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ ์žฌ๊ท€ ๊นŠ์ด ์ œํ•œ์€ 1000์œผ๋กœ ๋งค์šฐ ์–•๋‹ค.
# ๋”ฐ๋ผ์„œ ์žฌ๊ท€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๊ฒฝ์šฐ ์ด์ œํ•œ์— ๊ฑธ๋ ค ์—๋Ÿฌ๋ฉ”์„ธ์ง€๋ฅผ ๋ณผ์ˆ˜ ์—†๋‹ค.
# ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.
import sys
sys.setrecursionlimit(100000)

def dfs(x,y):
    visited[x][y] = True
    directions = [ (-1, 0), (1, 0), (0, -1), (0, 1)]
    # ์ƒํ•˜ ์ขŒ์šฐ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•ด์ฃผ๊ณ  ์—ฌ๊ธฐ์„œ for๋ฌธ์„ ๋Œ๋ฆฐ๋‹ค.
    for dx, dy in directions:
        nx, ny = dx + x, dy+y
        if nx < 0 or nx >=n or ny<0 or ny>=m:
            continue
        if array[nx][ny] and not visited[nx][ny]:
            dfs(nx,ny)

for _ in range(int(input())):
    m, n, k = map(int,input().split())
    array=[[0]*m for _ in range(n)]
    visited = [[False]*m for _ in range(n)]
    for _ in range(k):
        y, x = map(int,input().split())
        array[x][y] = 1
    result = 0
    for i in range(n):
        for j in range(m):
            if array[i][j] and not visited[i][j]:
                dfs(i,j)
                result += 1
    print(result)

 

 

 

 

2. BFS๋กœ ํ’€์–ด๋ณด๋ฉด,,,

BFS๋Š” ๋ฑ..deque 

 

dfs์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ์ƒํ•˜์ขŒ์šฐ์˜ ์œ„์น˜๋ฅผ ๋‹ด์•„ for๋ฌธ์„ ๋Œ๋ ค์ค„ ๊ฒƒ์ด๋‹ค. 

from collections import deque

def bfs(x,y):
    q = deque()
    q.append((x,y))
    visited[x][y] = True
    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while q:
        x, y = q.popleft()
        for dx, dy in directions:
            nx, ny = x+dx, y+dy
            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if array[nx][ny] and not visited[nx][ny]:
                q.append((nx,ny))
                visited[nx][ny]=True


for _ in range(int(input())):
    m, n, k = map(int,input().split())
    array=[[0]*m for _ in range(n)]
    visited = [[False]*m for _ in range(n)]
    for _ in range(k):
        y, x = map(int,input().split())
        array[x][y] = 1
    result = 0
    for i in range(n):
        for j in range(m):
            if array[i][j] and not visited[i][j]:
                bfs(i,j)
                result += 1
    print(result)

 

๋ฌธ์ œ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


์‹ ์ข… ๋ฐ”์ด๋Ÿฌ์Šค์ธ ์›œ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ „ํŒŒ๋œ๋‹ค. ํ•œ ์ปดํ“จํ„ฐ๊ฐ€ ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ฆฌ๋ฉด ๊ทธ ์ปดํ“จํ„ฐ์™€ ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ์ปดํ“จํ„ฐ๋Š” ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด 7๋Œ€์˜ ์ปดํ“จํ„ฐ๊ฐ€ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜์ž. 1๋ฒˆ ์ปดํ“จํ„ฐ๊ฐ€ ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ฆฌ๋ฉด ์›œ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” 2๋ฒˆ๊ณผ 5๋ฒˆ ์ปดํ“จํ„ฐ๋ฅผ ๊ฑฐ์ณ 3๋ฒˆ๊ณผ 6๋ฒˆ ์ปดํ“จํ„ฐ๊นŒ์ง€ ์ „ํŒŒ๋˜์–ด 2, 3, 5, 6 ๋„ค ๋Œ€์˜ ์ปดํ“จํ„ฐ๋Š” ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ 4๋ฒˆ๊ณผ 7๋ฒˆ ์ปดํ“จํ„ฐ๋Š” 1๋ฒˆ ์ปดํ“จํ„ฐ์™€ ๋„คํŠธ์›Œํฌ์ƒ์—์„œ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

์–ด๋Š ๋‚  1๋ฒˆ ์ปดํ“จํ„ฐ๊ฐ€ ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ ธ๋‹ค. ์ปดํ“จํ„ฐ์˜ ์ˆ˜์™€ ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •๋ณด๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, 1๋ฒˆ ์ปดํ“จํ„ฐ๋ฅผ ํ†ตํ•ด ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ฆฌ๊ฒŒ ๋˜๋Š” ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

 

 

๐Ÿ’ก ์ž…๋ ฅ


์ฒซ์งธ ์ค„์—๋Š” ์ปดํ“จํ„ฐ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋Š” 100 ์ดํ•˜์ด๊ณ  ๊ฐ ์ปดํ“จํ„ฐ์—๋Š” 1๋ฒˆ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ปดํ“จํ„ฐ ์Œ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์„œ ๊ทธ ์ˆ˜๋งŒํผ ํ•œ ์ค„์— ํ•œ ์Œ์”ฉ ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ ์Œ์ด ์ฃผ์–ด์ง„๋‹ค.

 

 

 

 

๐Ÿ“Œ ์ถœ๋ ฅ


1๋ฒˆ ์ปดํ“จํ„ฐ๊ฐ€ ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ ธ์„ ๋•Œ, 1๋ฒˆ ์ปดํ“จํ„ฐ๋ฅผ ํ†ตํ•ด ์›œ ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ฆฌ๊ฒŒ ๋˜๋Š” ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋ฅผ ์ฒซ์งธ ์ค„์— ์ถœ๋ ฅํ•œ๋‹ค.

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


com = int(input())
cnt = 0
adj = [[] for _ in range(com+1)]
visited = [False] * (com+1)

for i in range(int(input())):
  x, y = map(int, input().split())
  adj[x].append(y)
  adj[y].append(x)

def dfs(v):
  visited[v] = True
  for e in adj[v]:
    if not visited[e]:
      dfs(e)
  
dfs(1)

for i in range(len(visited)):
  if visited[i] == True:
    cnt += 1
    
print(cnt-1)

 

 

2. BFS๋กœ ํ’€์–ด๋ณด๋ฉด,,,

 

from collections import deque

com = int(input())
cnt = 0
adj = [[] for _ in range(com+1)]
visited = [False] * (com+1)

for i in range(int(input())):
  x, y = map(int, input().split())
  adj[x].append(y)
  adj[y].append(x)

def bfs(v):
  q = deque([v])
  visited[v] = True
  while q:
    v = q.popleft()
    for e in adj[v]:
      if not visited[e]:
        q.append(e)
        visited[e]= True

  
bfs(1)

for i in range(len(visited)):
  if visited[i] == True:
    cnt += 1

print(cnt-1)

 

๋ฌธ์ œ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


๋„คํŠธ์›Œํฌ๋ž€ ์ปดํ“จํ„ฐ ์ƒํ˜ธ ๊ฐ„์— ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐ๋œ ํ˜•ํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ปดํ“จํ„ฐ A์™€ ์ปดํ“จํ„ฐ B๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๊ณ , ์ปดํ“จํ„ฐ B์™€ ์ปดํ“จํ„ฐ C๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ๋•Œ ์ปดํ“จํ„ฐ A์™€ ์ปดํ“จํ„ฐ C๋„ ๊ฐ„์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ปดํ“จํ„ฐ A, B, C๋Š” ๋ชจ๋‘ ๊ฐ™์€ ๋„คํŠธ์›Œํฌ ์ƒ์— ์žˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ปดํ“จํ„ฐ์˜ ๊ฐœ์ˆ˜ n, ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด computers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋„คํŠธ์›Œํฌ์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

 

์ œํ•œ์‚ฌํ•ญ

  • ์ปดํ“จํ„ฐ์˜ ๊ฐœ์ˆ˜ n์€ 1 ์ด์ƒ 200 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ ์ปดํ“จํ„ฐ๋Š” 0๋ถ€ํ„ฐ n-1์ธ ์ •์ˆ˜๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • i๋ฒˆ ์ปดํ“จํ„ฐ์™€ j๋ฒˆ ์ปดํ“จํ„ฐ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด computers[i][j]๋ฅผ 1๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • computer[i][i]๋Š” ํ•ญ์ƒ 1์ž…๋‹ˆ๋‹ค.

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


DFS์™€ BFS๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋‹ค.

๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ํ•ด๋ณด์ž!

 

1. DFS๋กœ ํ’€์–ด๋ณด๋ฉด,,,

  • ๋ฐฉ๋ฌธ์„ ํ–ˆ๋Š”์ง€ ํ™•์ธ์„ ๋„์™€์ค„ visited ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

def dfs(n, computers, com, visited):
  # dfs ๋Œ๊ธฐ ์œ„ํ•œ ์ปดํ“จํ„ฐ๋Š” ์šฐ์„  ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ์ฐ๋Š”๋‹ค.
  visited[com] = True
  
  # ์ปดํ“จํ„ฐ๋“ค ๊ฐœ์ˆ˜๋งŒํผ for๋ฌธ ๋Œ๋ ค์„œ com๊ณผ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๊ณ  ๋ฐฉ๋ฌธํ•œ์  ์—†๋Š” ๊ฒƒ์„ ์ฐพ๋Š”๋‹ค.
  # ์ฐพ์œผ๋ฉด ์–˜๋„ dfs๋Œ๋ฆฐ๋‹ค.
  for connect in range(n):
    if connect != com and computers[com][connect] == 1:
      if visited[connect] == False:
        dfs(n, computers, connect, visited)


def solution(n, computers):
  answer = 0
  # ๋ฐฉ๋ฌธํ•œ ๊ธฐ๋ก์„ ๋‚จ๊ธฐ๋Š” visited
  # ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†์œผ๋ฉด False๋‹ค.
  visited = [False] * n
  
  # ์ปดํ“จํ„ฐ ํ•œ๋Œ€์”ฉ ๋Œ๋ ค๋ณด๋ฉฐ ๋ฐฉ๋ฌธํ•œ ์  ์—†๋Š” ์ปดํ“จํ„ฐ๋ฅผ ์ฐพ์•„
  # dfsํ•ด์ฃผ๊ณ , ํ•œ๋ฒˆ ๋๋‚˜๋ฉด answer์— 1์„ ๋”ํ•ด์ค€๋‹ค.
  for com in range(n):
    if visited[com] == False:
      dfs(n, computers, com, visited)
      answer += 1
      
  return answer;

 

 

2. BFS๋กœ ํ’€์–ด๋ณด๋ฉด,,,

dfs์™€ ๋‹ฌ๋ฆฌ deque ๋ฑ์„ import ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

from collections import deque

def bfs(n, computers, com, visited):
  # ๋ฑ์— ํ˜„์žฌ com ๋ฒˆํ˜ธ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
  q = deque([com])
  
  # ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ‘œ์‹œํ•ด
  visited[com] = True
  
  # q๋ฅผ ๋Œ๋ฆฌ๋ฉด์„œ popleft ํ•ด์ฃผ๊ณ ... ๋“ค๋ ค์ค˜์•ผํ•˜๋Š” ์• ๋ฉด appendํ•ด์ค€๋‹ค
  while q:
    com = q.popleft()
    for connect in range(n):
      if connect!=com and computers[com][connect]==1 and not visited[connect]:
        q.append(connect)
        visited[connect] = True

def solution(n, computers):
  answer = 0
  visited = [False] * n
  for com in range(n):
    if visited[com] == False:
      bfs(n, computers, com, visited)
      answer += 1

  return answer;

๋ฌธ์ œ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


 

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


1. ๋‹ค๋ฆฌ ์œ„์— ์žˆ๋Š” ํŠธ๋Ÿญ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์ž.

trucks_on_bridge -> ์—ฌ๊ธฐ์„œ len๋Š” bridge_length๋กœ ์ค€๋‹ค. ๊ทธ ๊ธธ์ด๋งŒํผ๋งŒ ํŠธ๋Ÿญ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ

 

2. ์ด์ œ ๋‹ค๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ฐจ ๋Œ€์ˆ˜๋งŒํผ while๋ฌธ์„ ๋นŒ๋ฆฌ๋ฉด์„œ ์ง„ํ–‰ํ•œ๋‹ค.

๋‹ค๋ฆฌ์— ์˜ฌ๋ผ์™€ ์žˆ๋Š” ํŠธ๋Ÿญ์ด ์žˆ์œผ๋ฉด ๋งจ ์•ž์— ์žˆ๋Š” ํŠธ๋Ÿญ์„ ๋นผ์ฃผ๊ณ 

ํ˜„์žฌ ๋‹ค๋ฆฌ์— ์žˆ๋Š” ํŠธ๋Ÿญ๋“ค์˜ ํ•ฉ๊ณผ ์ƒˆ๋กœ ํˆฌ์ž…๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” truck_weights[0]์„ ํ•ฉํ–ˆ์„ ๋•Œ ๊ฒฌ๋”œ ์ˆ˜ ์žˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค.

๋œ๋‹ค๋ฉด ๋‹ค๋ฆฌ์— ์˜ฌ๋ ค์ฃผ๊ณ  ๋Œ€๊ธฐ ๋ชฉ๋ก์—์„œ ๋นผ์ค€๋‹ค.

์•ˆ๋œ๋‹ค๋ฉด ๊ทธ๋ƒฅ 0์„ ๋„ฃ์–ด์„œ ์˜ฌ๋ฆฌ์ง€ ๋ชปํ–ˆ์Œ์„ ๋ช…์‹œํ•œ๋‹ค.

 

 

 

 

def solution(bridge_length, weight, truck_weights):
    answer = 0
    trucks_on_bridge = [0] * bridge_length
    while len(trucks_on_bridge):
        answer += 1
        trucks_on_bridge.pop(0)
        if truck_weights:
            if sum(trucks_on_bridge) + truck_weights[0] <= weight:
                trucks_on_bridge.append(truck_weights.pop(0))
            else:
                trucks_on_bridge.append(0)
    return answer

 

 

<์ฐธ๊ณ ํ• ๋งŒํ•œ ๋‹ต์•ˆ>

import collections

DUMMY_TRUCK = 0


class Bridge(object):

    def __init__(self, length, weight):
        self._max_length = length
        self._max_weight = weight
        self._queue = collections.deque()
        self._current_weight = 0

    def push(self, truck):
        next_weight = self._current_weight + truck
        if next_weight <= self._max_weight and len(self._queue) < self._max_length:
            self._queue.append(truck)
            self._current_weight = next_weight
            return True
        else:
            return False

    def pop(self):
        item = self._queue.popleft()
        self._current_weight -= item
        return item

    def __len__(self):
        return len(self._queue)

    def __repr__(self):
        return 'Bridge({}/{} : [{}])'.format(self._current_weight, self._max_weight, list(self._queue))


def solution(bridge_length, weight, truck_weights):
    bridge = Bridge(bridge_length, weight)
    trucks = collections.deque(w for w in truck_weights)

    for _ in range(bridge_length):
        bridge.push(DUMMY_TRUCK)

    count = 0
    while trucks:
        bridge.pop()

        if bridge.push(trucks[0]):
            trucks.popleft()
        else:
            bridge.push(DUMMY_TRUCK)

        count += 1

    while bridge:
        bridge.pop()
        count += 1

    return count


def main():
    print(solution(2, 10, [7, 4, 5, 6]), 8)
    print(solution(100, 100, [10]), 101)
    print(solution(100, 100, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]), 110)


if __name__ == '__main__':
    main()

BOJ 14719

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


2์ฐจ์› ์„ธ๊ณ„์— ๋ธ”๋ก์ด ์Œ“์—ฌ์žˆ๋‹ค. ๋น„๊ฐ€ ์˜ค๋ฉด ๋ธ”๋ก ์‚ฌ์ด์— ๋น—๋ฌผ์ด ๊ณ ์ธ๋‹ค.๋น„๋Š” ์ถฉ๋ถ„ํžˆ ๋งŽ์ด ์˜จ๋‹ค. ๊ณ ์ด๋Š” ๋น—๋ฌผ์˜ ์ด๋Ÿ‰์€ ์–ผ๋งˆ์ผ๊นŒ?

 

 

๐Ÿ’ก ์ž…๋ ฅ


์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” 2์ฐจ์› ์„ธ๊ณ„์˜ ์„ธ๋กœ ๊ธธ์ด H๊ณผ 2์ฐจ์› ์„ธ๊ณ„์˜ ๊ฐ€๋กœ ๊ธธ์ด W๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ H, W ≤ 500)

๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ๋ธ”๋ก์ด ์Œ“์ธ ๋†’์ด๋ฅผ ์˜๋ฏธํ•˜๋Š” 0์ด์ƒ H์ดํ•˜์˜ ์ •์ˆ˜๊ฐ€ 2์ฐจ์› ์„ธ๊ณ„์˜ ๋งจ ์™ผ์ชฝ ์œ„์น˜๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ W๊ฐœ ์ฃผ์–ด์ง„๋‹ค.

๋”ฐ๋ผ์„œ ๋ธ”๋ก ๋‚ด๋ถ€์˜ ๋นˆ ๊ณต๊ฐ„์ด ์ƒ๊ธธ ์ˆ˜ ์—†๋‹ค. ๋˜ 2์ฐจ์› ์„ธ๊ณ„์˜ ๋ฐ”๋‹ฅ์€ ํ•ญ์ƒ ๋ง‰ํ˜€์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์—ฌ๋„ ์ข‹๋‹ค.

 

 

 

 

๐Ÿ“Œ ์ถœ๋ ฅ


2์ฐจ์› ์„ธ๊ณ„์—์„œ๋Š” ํ•œ ์นธ์˜ ์šฉ๋Ÿ‰์€ 1์ด๋‹ค. ๊ณ ์ด๋Š” ๋น—๋ฌผ์˜ ์ด๋Ÿ‰์„ ์ถœ๋ ฅํ•˜์—ฌ๋ผ.

๋น—๋ฌผ์ด ์ „ํ˜€ ๊ณ ์ด์ง€ ์•Š์„ ๊ฒฝ์šฐ 0์„ ์ถœ๋ ฅํ•˜์—ฌ๋ผ.

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


๋น—๋ฌผ์ด ๊ณ ์ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๋‘ฅ์ด ํ•„์š”ํ•˜๋‹ค. 

์ด ๊ธฐ๋‘ฅ์ด ์–ด๋–ป๊ฒŒ ์ƒ๊ธฐ๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณด์ž.

 

์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด max์ธ 4๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์šฐ์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’์ธ 3๊ณผ 2์˜ ๊ฐ’์ด ์ค‘์š”ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์™ผ์ชฝ์€ 3์„ ๊ธฐ์ค€์œผ๋กœ ๋น—๋ฌผ์ด ๋ฐ›์•„์ง€๊ณ  ์˜ค๋ฅธ์ชฝ์€ 2๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋น—๋ฌผ์ด ๋ฐ›์•„์ง€๊ธฐ ๋•Œ๋ฌธ~

 

๋” ์ž์„ธํ•˜๊ฒŒ ๋ณด์ž๋ฉด..

๊ฐ ์ž๋ฆฌ๋ฅผ i๋กœ ๋ณด๊ณ  0๋ถ€ํ„ฐ 7๊นŒ์ง€ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ฒผ์„ ๋•Œ 2๋ฒˆ์งธ ๋ธ”๋Ÿญ์„ ๋ณด์ž.

i=2๋ฒˆ์งธ ๋ธ”๋Ÿญ์€ 2๊ฐœ๋กœ ์ด ๋ธ”๋Ÿญ์„ ๊ธฐ์ค€์œผ๋กœ ๋น—๋ฌผ ๊ธฐ๋‘ฅ์ด ๋˜์–ด์ค„ ์ขŒ์šฐ์˜ max ์ˆซ์ž๋ฅผ ์‚ดํŽด๋ณด๋ฉด 3๊ณผ 4์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์—ฌ๊ธฐ์„œ max์ค‘ ์ž‘์€ ์ˆ˜๊ฐ€ ๊ธฐ์ค€์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— 3์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ์ด์šฉํ•ด์ฃผ๊ฒŒ ๋œ๋‹ค.

 

 

์ •๋ฆฌํ•˜์ž๋ฉด~

  • ์–‘์ชฝ์— ๋” ๋†’์€ ๋ธ”๋Ÿญ์ด ์กด์žฌํ•˜๋ฉด ๋น—๋ฌผ์ด ๊ณ ์ž„
  • ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ํ˜„์žฌ์˜ ๋ธ”๋Ÿญ ๊ธฐ์ค€ left_max์™€ right_max๋ฅผ ๊ตฌํ•˜๊ณ  ์ด ๋‘ ๊ฐ’์ค‘ ์ž‘์€ ์ˆ˜๋ฅผ ๊ตฌํ•จ
    • ๊ตฌํ•ด์ง„ ์ˆ˜ - ํ˜„์žฌ ๋ธ”๋Ÿญ์˜ ์ˆ˜ ๊ฐ€ ๋น—๋ฌผ์ด ๋‹ด๊ธธ ์นธ์ˆ˜๋‹ค.
    • ์ฒซ ๋ธ”๋Ÿญ๊ณผ ๋งˆ์ง€๋ง‰ ๋ธ”๋Ÿญ์€ ๋ณผํ•„์š” ์—†๋‹ค. (๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์˜ˆ์‹œ๋ฅผ ์ด์šฉํ•˜์ž๋ฉด, i=0์ผ ๋•Œ์™€ i=7์ผ ๋•Œ)

 

# ๋น—๋ฌผ

# ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ค€๋‹ค.
H, W = map(int, input().split())
block = list(map(int, input().split()))
answer = 0

# ๋งจ ์•ž๋’ค๋ฅผ ์ œ์™ธํ•œ for๋ฌธ์„ ๋Œ๋ฉด์„œ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๋Š”๋‹ค.
for i in range(1, W-1):
    left_max = max(block[:i]) # ํ˜„์žฌ ๋ธ”๋Ÿญ์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์—์„œ max ๊ฐ’ ์ฐพ๊ธฐ
    right_max = max(block[i+1:]) # ํ˜„์žฌ ๋ธ”๋Ÿญ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฅธ์ชฝ์—์„œ max ๊ฐ’ ์ฐพ๊ธฐ
    m = min(left_max, right_max) #left_max์™€ right_max ์ค‘ ์ž‘์€ ๊ฐ’์„ ๋ณ€์ˆ˜์— ๋‹ด์ž.
    
    # ํ˜„์žฌ ๋ธ”๋Ÿญ์ด ์ง€์ •ํ•œ ๊ธฐ์ค€๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋น—๋ฌผ์ด ๊ณ ์ธ๋‹ค.
    if block[i] < m:
        answer += m - block[i]

print(answer)

 

 

BOJ 1149

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


RGB๊ฑฐ๋ฆฌ์—๋Š” ์ง‘์ด N๊ฐœ ์žˆ๋‹ค. ๊ฑฐ๋ฆฌ๋Š” ์„ ๋ถ„์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ , 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ N๋ฒˆ ์ง‘์ด ์ˆœ์„œ๋Œ€๋กœ ์žˆ๋‹ค.

์ง‘์€ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘ ์ค‘ ํ•˜๋‚˜์˜ ์ƒ‰์œผ๋กœ ์น ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ๊ทœ์น™์„ ๋งŒ์กฑํ•˜๋ฉด์„œ ๋ชจ๋“  ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.

  • 1๋ฒˆ ์ง‘์˜ ์ƒ‰์€ 2๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • N๋ฒˆ ์ง‘์˜ ์ƒ‰์€ N-1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • i(2 ≤ i ≤ N-1)๋ฒˆ ์ง‘์˜ ์ƒ‰์€ i-1๋ฒˆ, i+1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

 

 

๐Ÿ’ก ์ž…๋ ฅ


์ฒซ์งธ ์ค„์— ์ง‘์˜ ์ˆ˜ N(2 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

 

 

๐Ÿ“Œ ์ถœ๋ ฅ


  • ์ฒซ์งธ ์ค„์— ๋ชจ๋“  ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ์ ‘๊ทผํ•ด์„œ Memoization์„ ์‚ฌ์šฉํ•˜์ž.

 

2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ๊ฐ ์ž๋ฆฌ๋งˆ๋‹ค RGB์˜ ๋น„์šฉ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์ž.

just like... -> [ [ 1๋ฒˆ ์ง‘์˜ R๋น„์šฉ, 1๋ฒˆ ์ง‘์˜ G๋น„์šฉ, 1๋ฒˆ ์ง‘์˜ B๋น„์šฉ ] , [ 2๋ฒˆ ์ง‘์˜ R๋น„์šฉ, ....

 

์ ํ™”์‹์„ ๋– ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ i๋ฒˆ์งธ์˜ ์ง‘๊ณผ ๊ทธ ์•ž๋’ค์˜ i-1๋ฒˆ์งธ ์ง‘๊ณผ i+1๋ฒˆ์งธ ์ง‘์„ ์ƒ๊ฐํ•ด๋ณด์ž.

i+1๋ฒˆ์งธ ์ง‘์€ i๋ฒˆ์งธ ์ง‘์˜ ์ƒ‰์— ์˜ํ–ฅ์„ ๋ฐ›์ง€๋งŒ i-1๋ฒˆ์งธ ์ง‘์˜ ์ƒ‰์—๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

 

 

์ด ๊ฐ™์€ ์ƒํ™ฉ์„ ์ ํ™”์‹์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด์ž. i์™€ i+1๋งŒ ์ƒ๊ฐํ•ด์ฃผ๋ฉด๋œ๋‹ค~

  1. i+1๋ฒˆ์งธ ์ง‘์ด R์ผ ๋•Œ, i-1์€์€ G๋‚˜ B ์ค‘ ๋” ์ ์€ ๋น„์šฉ์„ ๊ฐ–๋Š” ๊ฒƒ์„ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.
  2. i+1๋ฒˆ์งธ ์ง‘์ด G์ผ ๋•Œ, i-1์€์€ R๋‚˜ B ์ค‘ ๋” ์ ์€ ๋น„์šฉ์„ ๊ฐ–๋Š” ๊ฒƒ์„ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.
  3. i+1๋ฒˆ์งธ ์ง‘์ด B์ผ ๋•Œ, i-1์€์€ R๋‚˜ G ์ค‘ ๋” ์ ์€ ๋น„์šฉ์„ ๊ฐ–๋Š” ๊ฒƒ์„ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

# RGB

# n : ์ง‘์˜ ์ˆ˜
n = int(input())

# ์ดˆ๊ธฐ์„ธํŒ… 0์œผ๋กœ ๋‹ค ๊น”์•„๋†“๊ฑฐ๋ผ
dp = [[0, 0, 0] for _ in range(n)]

for i in range(n):
    # ์šฐ์„  ๋จผ์ € ์ž…๋ ฅ๋ฐ›์Œ
    cost = list(map(int, input().split()))

    # ์ฒซ ์ž…๋ ฅ์ธ ๊ฒฝ์šฐ, ๊ทธ๋ƒฅ cost๋ฅผ ๋‹ด์•„์ฃผ์„ธ์š”.
    if i == 0:
        dp[0] = cost
    else:
        # ์ ํ™”์‹ ๋„ฃ์–ด์ฃผ์„ธํšจ
        dp[i][0] = min(dp[i-1][1], dp[i-1][2]) + cost[0] # i๋ฒˆ์งธ์ง‘์ด R์ผ๊ฒฝ์šฐ
        dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + cost[1] # i๋ฒˆ์งธ์ง‘์ด G์ผ๊ฒฝ์šฐ
        dp[i][2] = min(dp[i-1][0], dp[i-1][1]) + cost[2] # i๋ฒˆ์งธ์ง‘์ด B์ผ๊ฒฝ์šฐ

# dp ๋ฆฌ์ŠคํŠธ์˜ n-1๋ฒˆ์งธ์— ์žˆ๋Š” ๋ˆ„์ ๋œ RGB ๋น„์šฉ ์ค‘ ๊ฐ€์žฅ ์ตœ์†Œ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
print(min(dp[n-1]))

46. Permutations

๐Ÿ•ต๏ธ‍โ™€๏ธ Question


Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

 

 

๐Ÿ’ก Example


1:

Input: nums = [1,2,3]

Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

 

2:

Input: nums = [0,1]

Output: [[0,1],[1,0]]

 

3:

Input: nums = [1]

Output: [[1]]

 

 

 

๐Ÿ“Œ Constraints


  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • All the integers of nums are unique.

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


์„œ๋กœ ๋‹ค๋ฅธ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ˆ˜์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์ˆœ์—ด ๋ฌธ์ œ๋‹ค.

๋‘๊ฐ€์ง€์˜ ํ’€์ด๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

version1 : DFS๋ฅผ ํ™œ์šฉํ•œ ์ˆœ์—ด ์ƒ์„ฑ

๋ฐฑํŠธ๋ž˜ํ‚น(backtracking)์ด๋ž€? :

ํ•ด๋ฅผ ์ฐพ๋Š” ๋„์ค‘ ํ•ด๊ฐ€ ์•„๋‹ˆ์–ด์„œ ๋ง‰ํžˆ๋ฉด, ๋˜๋Œ์•„๊ฐ€์„œ ๋‹ค์‹œ ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ธฐ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

์ตœ์ ํ™” ๋ฌธ์ œ์™€ ๊ฒฐ์ • ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์ด ๋ฉ๋‹ˆ๋‹ค.

์ˆœ์—ด์ด๋ž€ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ๊ทธ๋ž˜ํ”„ ํ˜•ํƒœ๋กœ ๋‚˜์—ดํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. -> ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

The graph of Permutation with backtracking

# Runtime: 69 ms
# Memory Usage: 14.4 MB

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        results = []
        prev_elements = []
        
        def dfs(elements) :
            #๋ฆฌํ”„๋…ธ๋“œ์ผ๋•Œ
            if len(elements) == 0 :
                results.append(prev_elements[:])
                
            #์ˆœ์—ด ์ƒ์„ฑ ์žฌ๊ท€ ํ˜ธ์ถœ
            for e in elements:
                next_elements = elements[:]
                next_elements.remove(e)
               
                prev_elements.append(e)
                # print("perv",prev_elements)
                # print("next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„",next_elements)
                dfs(next_elements)
                prev_elements.pop()
                # print("**๋ฐฑํŠธ๋ž˜ํ‚น",prev_elements)
    
        dfs(nums) 
        return results
# input์ด [1,2,3] ๋ฉด ๋‚˜์˜ค๋Š” ์ถœ๋ ฅ,,,
perv [1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [2, 3]
perv [1, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [3]
perv [1, 2, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [1, 2]
**๋ฐฑํŠธ๋ž˜ํ‚น [1]
perv [1, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [2]
perv [1, 3, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [1, 3]
**๋ฐฑํŠธ๋ž˜ํ‚น [1]
**๋ฐฑํŠธ๋ž˜ํ‚น []
perv [2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1, 3]
perv [2, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [3]
perv [2, 1, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [2, 1]
**๋ฐฑํŠธ๋ž˜ํ‚น [2]
perv [2, 3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1]
perv [2, 3, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [2, 3]
**๋ฐฑํŠธ๋ž˜ํ‚น [2]
**๋ฐฑํŠธ๋ž˜ํ‚น []
perv [3]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1, 2]
perv [3, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [2]
perv [3, 1, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [3, 1]
**๋ฐฑํŠธ๋ž˜ํ‚น [3]
perv [3, 2]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1]
perv [3, 2, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [3, 2]
**๋ฐฑํŠธ๋ž˜ํ‚น [3]
**๋ฐฑํŠธ๋ž˜ํ‚น []
=========
perv [0]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [1]
perv [0, 1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [0]
**๋ฐฑํŠธ๋ž˜ํ‚น []
perv [1]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ [0]
perv [1, 0]
next์žฌ๊ท€๋Œ๋Ÿฌ๊ฐ€์ฆˆ์•„ []
**๋ฐฑํŠธ๋ž˜ํ‚น [1]
**๋ฐฑํŠธ๋ž˜ํ‚น []

 

 

 

version2. ํŒŒ์ด์ฌ์˜ itertools ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค.

itertools 
๋ฐ˜๋ณต์ž ์ƒ์„ฑ์— ์ตœ์ ํ™”๋œ ํšจ์œจ์ ์ธ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณต.
์ด๋ฏธ ์ž˜ ๊ตฌํ˜„๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ ๋ฒ„๊ทธ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์ด ๋‚ฎ๊ณ  ํšจ์œจ์ ์œผ๋กœ ์„ค๊ณ„๋œ C ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ ์†๋„๋„ ๋น ๋ฆ„
๐Ÿ“ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์„์œผ๋กœ "# ๊ตฌํ˜„์˜ ํšจ์œจ์„ฑ, ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ๋‹ค" ๋ผ๊ณ  ๋‹ฌ์•„์ฃผ๋ฉด ์ต์˜ค~

permutation( ) ํ•จ์ˆ˜๊ฐ€ ํŠœํ”Œ ๋ชจ์Œ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌํŠธ์ฝ”๋“œ ๋ฌธ์ œ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€ํ™˜์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

# Runtime: 28 ms
# Memory Usage: 14.3 MB

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        import itertools
        return list(map(list, itertools.permutations(nums)))

+ Recent posts