๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2023.12.26 ์˜ค๋Š˜์˜ ๋ฐฑ์ค€ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2023๋…„ 12์›” 26์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
2/34
post-thumbnail

1926๋ฒˆ - ๊ทธ๋ฆผ

1๋กœ ๊ตฌ๋ณ„๋œ ๊ทธ๋ฆผ์˜ ๊ฐœ์ˆ˜์™€ ๊ทธ๋ฆผ์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
BFS๋ฅผ ํ™œ์šฉํ•˜๋ฉด ํ’€์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

N,M = map(int,input().split())
graph = []
que = deque()
for _ in range(N):
    graph.append(list(map(int,input().split())))

๋จผ์ € ๊ฐ€๋กœ ์„ธ๋กœ์™€ BFS ์‚ฌ์šฉ์„ ์œ„ํ•œ ํ ์„ ์–ธ ๋ฐ ๊ทธ๋ž˜ํ”„์˜ ํฌ๊ธฐ ๋“ฑ์„ ๋ฐ›์•„๋‚ด๊ฒ ์Šต๋‹ˆ๋‹ค.

ans_1,ans_2 = 0,0
for i in range(N):
    for j in range(M):
        if graph[i][j] == 1:
            a,b = bfs(i,j)
            ans_1 += a
            if b>ans_2:
                ans_2 = b

์ •๋‹ต์„ ๋ฐ›์„ ๋ณ€์ˆ˜ 1,2๋ฅผ ์„ ์–ธํ•˜๊ณ  bfs๋ฅผ ๋Œ๋ ค ๋ณ€์ˆ˜1์€ ๊ทธ๋ฆผ์˜ ๊ฐœ์ˆ˜์ด๋ฏ€๋กœ ๋ง์…ˆ๋ฐฉ์‹์œผ๋กœ, ๋ณ€์ˆ˜2๋Š” ๊ทธ๋ฆผ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ๋‹ด์•„๋‚ผ ๊ฒƒ์ด๋ฏ€๋กœ maxํ•จ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๋ฐ›์•„์ค์‹œ๋‹ค.

from collections import deque
dx = [-1,1,0,0]
dy = [0,0,1,-1]
def bfs(x,y):
    max_draw = 0
    sum = 0
    boolean = False
    que.append([x,y])
    while que:

ํ๋ฅผ ์„ ์–ธํ•˜๊ณ  BFSํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

        x,y = que.popleft()
        for i in range(4):
            nx = dx[i] + x
            ny = dy[i] + y

            if nx<0 or ny<0 or nx>=N or ny>=M:
                continue 

์ƒํ•˜์ขŒ์šฐ 4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ์‹ค์‹œํ•ด์ฃผ๊ณ 

            if graph[nx][ny] == 1:
                max_draw += 1
                que.append([nx,ny])
                graph[nx][ny] = 2
                boolean = True

1์ด ์กด์žฌํ•œ๋‹ค๋ฉด ๊ทธ๋ฆผ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 2๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ๋‹ค์Œ ํƒ์ƒ‰ ํ›„๋ณด์— ์ถ”๊ฐ€ํ•ด์ค์‹œ๋‹ค. boolean๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ํƒ์ƒ‰์„ ํ–ˆ๋Š”์ง€ ์•ˆํ–ˆ๋Š”์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž‘์„ฑํ–ˆ์”๋‹ˆ๋‹ค.

    sum += 1
    if boolean == False:
        max_draw += 1
    return sum,max_draw

๋ชจ๋“  ํƒ์ƒ‰์ด ๋๋‚ฌ์œผ๋ฉด ๊ทธ ์ฃผ๋ณ€ ๊ทธ๋ฆผ์€ ๋‹ค ํƒ์ƒ‰ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ sum๊ฐ’์„ 1 ๋†’์—ฌ์ฃผ๊ณ  boolean์ด ํƒ์ƒ‰์„ ํ•œ๋ฒˆ๋„ ํ•œ์ ์ด ์—†๋Š” False๊ฐ’์ด๋ผ๋ฉด ๊ทธ๋ฆผ์ด ๋”ฑ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜๋ฏ€๋กœ max_draw์— 1์„ ๋”ํ•ด์ค์‹œ๋‹ค.

print(ans_1)
#๊ทธ๋ฆผ์ด ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ
if ans_1 == 0:
    print(0)
else:
    print(ans_2)

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ๋ฆผ์ด ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ๋‹ต์„ ๋„์ถœํ•ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


1743๋ฒˆ - ์Œ์‹๋ฌผ ํ”ผํ•˜๊ธฐ

๋น„์Šทํ•œ ์œ ํ˜•์˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์Œ์‹๋ฌผ์ด ๊ฐ€์žฅ ๋งŽ์ด ๋ญ‰์ณ์žˆ๋Š” ๋ถ€์œ„์— ์Œ์‹๋ฌผ์ด ๋ช‡๊ฐœ๋‚˜ ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

N,M,K = map(int,input().split())
graph = [[0]*M for _ in range(N)]
visited = [[0]*M for _ in range(N)]
que = deque()
for _ in range(K):
    a,b = map(int,input().split())
    graph[a-1][b-1] = 1

BFS์กฐ์‚ฌ๋ฅผ ์œ„ํ•ด์„œ ๊ทธ๋ž˜ํ”„์™€ ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ๋“ฑ์„ ์„ ์–ธํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

max = 0
for i in range(N):
    for j in range(M):
        if graph[i][j] == 1:
            a = bfs(i,j)
            if max < a:
                max = a

๊ทธ๋ž˜ํ”„๋ฅผ ๋‹ค ์‚ดํŽด๋ด์„œ ๋งŒ์•ฝ ํ•ด๋‹น ์ž๋ฆฌ์— ์Œ์‹๋ฌผ์ด ์กด์žฌํ•œ๋‹ค๋ฉด BFS ํƒ์ƒ‰์„ ๋Œ๋ ค์ค„๊ฒ๋‹ˆ๋‹ค.

from collections import deque
dx = [-1,1,0,0]
dy = [0,0,1,-1]
def bfs(x,y):
    sum = 0
    visited[x][y] = 1
    que.append([x,y])

๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ๊ธฐ์žฌํ•ด์ฃผ๊ณ  que๋ฅผ ํ•˜๋‚˜ ๋ฝ‘์•„์„œ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    while que:
        x,y = que.popleft()
        for i in range(4):
            nx = dx[i] + x
            ny = dy[i] + y
            if nx<0 or ny<0 or nx>=N or ny>=M:
                continue

4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ๋Œ๋ฆฌ๊ณ 

            if graph[nx][ny] == 1 and not visited[nx][ny]:
                visited[nx][ny] = 1
                sum += 1
                que.append([nx,ny])

๋ฐฉ๋ฌธ์•ˆํ–ˆ๊ณ  ์Œ์‹๋ฌผ์ด ์žˆ๋Š” ๋ถ€์œ„๋ผ๋ฉด ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ‘œ๊ธฐํ•ด์ฃผ๊ณ  sum๊ฐ’์„ 1 ๋†’์—ฌ์ค€ ๋‹ค์Œ ๊ทธ๋‹ค์Œ ํƒ์ƒ‰ํ›„๋ณด์— ์˜ฌ๋ ค๋‘ก์‹œ๋‹ค.

    return sum

sum๊ฐ’์„ return ํ•ด์ฃผ๊ณ 

print(max+1)

์–ด์งœํ”ผ ์ฒซ ์กฐ์‚ฌ๋Š” ์Œ์‹๋ฌผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ–ˆ๊ณ  ๋ฐฉ๋ฌธํ•œ๊ณณ์€ ๋‹ค์‹œ ๋ฐฉ๋ฌธ์„ ์•ˆํ•˜๋‹ˆ๊นŒ ์ฒซ ์กฐ์‚ฌ ๊ตฌ์—ญ+์Œ์‹๋ฌผ์ด ๊ฐ€์žฅ ๋งŽ์ด ๋ญ‰์ณ์žˆ๋Š” ๊ณณ ํƒ์ƒ‰ ๋ˆ ๊ฒƒ์„ ํ•ฉํ•ด์ฃผ๋ฉด ๋‹ต์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.


profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€