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

์œ ๋ น๊ฐœยท2024๋…„ 1์›” 18์ผ
0

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

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

23352๋ฒˆ - ๋ฐฉํƒˆ์ถœ


๋„๋„ํ•œ ์‹œ๊ฐ„์ œํ•œ

์ด๋ฒˆ๋ฌธ์ œ๋Š” ์ฝ”๋“œ๊ฐ€ ์ƒ๋‹นํžˆ ๋”ํ‹ฐํ•ฉ๋‹ˆ๋‹ค.
๊ทธ ์ด์œ ๋Š”

์ด๋ ‡๊ฒŒ ๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ 50 X 50 ์ด๋ผ๋Š” ๋„‰๋„‰ํ•œ ์‚ฌ์ด์ฆˆ๋กœ ์ฃผ์…จ๊ธฐ์—
์‹œ๊ฐ„์ œํ•œ์„ ์—ผ๋‘ํ•  ํ•„์š” ์—†์ด ๋ฐฐ์—ด์˜ ๋ณต์‚ฌ ๋ฐ ํƒ์ƒ‰์„ ๊ฑฐ๋ฆฌ๋‚Œ์—†์ด ์‚ฌ์šฉํ•˜์—ฌ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๋ฌผ๋ก  ๊น”๋”ํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ํ‘ผ๋‹ค๋ฉด ์ข‹๊ฒ ์ง€๋งŒ ๊ทธ๊ฑด ์‹ค๋ ฅ์ด ๋˜๋Š” ์‚ฌ๋žŒ๋“ค ์ด์•ผ๊ธฐ๊ณ  ์ €๋Š” ๊ณจ๋“œ๋ฌธ์ œ ํ‘ธ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ๊ฐ์ง€๋•์ง€...


BFS

BFS๋Š” ์–ด๋ ค์šธ ๊ฒƒ ์—†์ด ํ•ญ์ƒ ๋จน๋˜ ๊ทธ ๋ง›์ž…๋‹ˆ๋‹ค.
๊ทธ๋ž˜ํ”„๋ฅผ ๋ชจ๋‘ 1๋กœ ๋ฐ”๊ฟจ์œผ๋‹ˆ ํ˜„์žฌ๊ฑฐ๋ฆฌ + 1์„ ๋‹ค์Œ ๊ฑฐ๋ฆฟ๊ฐ’์— ๋Œ€์ž…ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

from collections import deque
import sys
import copy

input = sys.stdin.readline

que,sys,copy๋“ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ ์–ธํ•˜๊ณ 

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

์ƒํ•˜์ขŒ์šฐ ์ง€์ •ํ•œ ๋‹ค์Œ์—

def bfs(x,y):
    que.append([x,y])
    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 

์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰ ์ค‘ ๊ทธ๋ž˜ํ”„๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด continue๋กœ ์Šคํ‚ตํ•ด์ฃผ๊ณ 

            if graph[nx][ny] == 1:
                graph[nx][ny] = graph[x][y] + 1
                que.append([nx,ny])

๋งŒ์•ฝ ๋‹ค์Œ ํƒ์ƒ‰์ง€์ ์ด 1์ด๋ผ๋ฉด ํ˜„์žฌ ํƒ์ƒ‰์ง€์  + 1์„ ๋‹ค์Œ ํƒ์ƒ‰์ง€์ ์— ๋„ฃ์–ด์ฃผ๊ณ  ๋‹ค์Œ ํƒ์ƒ‰์ง€์ ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
๋‹ค๋งŒ ์ด๋Ÿฐ์”ฉ์œผ๋กœ ์ž‘์„ฑํ•˜๋ฉด ์ฒซ ์‹œ์ž‘์ง€์ ์ด ๋‹ค์‹œ ์ฐธ์กฐ๋˜์–ด ๋ณ€์ˆ˜๊ฐ€ ์˜ค์—ผ๋˜๋ฏ€๋กœ

   if graph[i][j] != 0:
        bfs(i,j)
        graph[i][j] = 1

์•„๊นŒ ์œ„์—์„œ graph[i][j]๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€ ์ด์œ ๊ฐ€ ์ด๋Ÿฐ ์—ฐ์œ ์ž…๋‹ˆ๋‹ค.

    return [x,y]

๋งˆ์ง€๋ง‰์œผ๋กœ ์ข…์ ๊ฐ’์„ ๋ฆฌํ„ด๊ฐ’์œผ๋กœ ์žก์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


๊ธฐ๋ณธ ์„ ์–ธ

์ด๋ฒˆ๋ฌธ์ œ ์ ‘๊ทผ ํ˜•์‹์„ ๊ฐ„๋‹จํžˆ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.
๋จผ์ € BFS๋กœ ์‹œ์ž‘์ ~์ข…์ ๊นŒ์ง€ ๋‹ค ํƒ์ƒ‰ํ•œ ํ›„, ๊ทธ ๊ฑฐ๋ฆฌ๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค.
ํ•ด๋‹น ๊ฑฐ๋ฆฌ๊ฐ’์ด ๊ฐ€์žฅ ํฌ๋‹ค๋ฉด ์‹œ์ž‘๊ณผ ์ข…์ ์— ํ•ด๋‹น๋˜๋Š” ์ธ๋ฑ์Šค ๊ฐ’์„ ๋”ํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๋ฉด ๋์ž…๋‹ˆ๋‹ค.

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

์ผ๋‹จ BFS ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ ๊ธฐ๋ณธ์ ์ธ que์™€ ๊ทธ ์™ธ ๊ฒƒ๋“ค์„ ๋ฐ›์•„์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

for i in range(N):
    for j in range(M):
        if graph[i][j] != 0:
            graph[i][j] = 1

๊ทธ๋ฆฌ๊ณ  ์‹œ์ž‘์ ~์ข…์  ๊ฑฐ๋ฆฟ๊ฐ’ ํƒ์ƒ‰์„ ์œ„ํ•ด ์ธ๋ฑ์Šค ๊ฐ’์€ ์ž ์‹œ graph_copy์— ๋„ฃ์–ด๋‘๊ณ  0(๋ง‰ํžŒ๊ตฌ๊ฐ„)์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค๋Š” ์ฃ„๋‹ค 1๋กœ ๋ฐ”๊พธ๊ฒ ์Šต๋‹ˆ๋‹ค.

graph_copy_distance = copy.deepcopy(graph)
max_distance = 0
for i in range(N):
    for j in range(M):
        if graph[i][j] != 0:
            bfs(i,j)
            graph[i][j] = 1
            if max_distance <= max(map(max,graph)):
                max_distance = max(map(max,graph))
            graph = copy.deepcopy(graph_copy_distance)

ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
graph_copy_distance์— ์•„๊นŒ ์ฃ„๋‹ค 1๋กœ ๋งŒ๋“  ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณต์‚ฌํ•ด์ฃผ๊ณ 
๊ฐ ์ธ๋ฑ์Šค๋งˆ๋‹ค bfs ํƒ์ƒ‰์„ ๋Œ๋ ค์„œ ์‹œ์ž‘์ ๋ถ€ํ„ฐ ์ข…์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฟ๊ฐ’์„ ์ธก์ •ํ•ด์ค๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  max_distance์— ์ตœ๋Œ€๋กœ ๋‚˜์˜จ ๊ฑฐ๋ฆฟ๊ฐ’์„ ๋Œ€์ž…ํ•ฉ์‹œ๋‹ค.

ans = []
ans_tmp = 0
for p in range(N):
    for k in range(M):
        if graph[p][k] != 0:
            [ans_x,ans_y] = bfs(p,k)
            graph[p][k] = 1
            ans_tmp = 0
            if max(map(max,graph)) == max_distance:
                ans_tmp += graph_copy[p][k]
                ans_tmp += graph_copy[ans_x][ans_y]
            graph = copy.deepcopy(graph_copy_distance)
        ans.append(ans_tmp)

์ด์ œ ๋‹ค์‹œ ํƒ์ƒ‰์„ ๋Œ์•„ ์ข…์ ์„ bfs return ๊ฐ’์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ  ๋งŒ์•ฝ ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ๊ทธ๋ž˜ํ”„๊ฐ€ ๊ฑฐ๋ฆฟ๊ฐ’์˜ ์ตœ๋Œ€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค๋ฉด
์‹œ์ž‘์ ๊ณผ ์ข…์ ์„ ans_tmp์— ๋‹ด์•„ ans๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ด์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.

try:
    print(max(ans))
except:
    print(0)

์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ans_tmp์˜ ์ง‘ํ•ฉ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ต์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.




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

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