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

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

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

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

18405๋ฒˆ - ๊ฒฝ์Ÿ์  ์ „์—ผ

ํ–‰๋ ฌ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ 1์ดˆ๋’ค 1~3๊นŒ์ง€์˜ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ƒํ•˜์ขŒ์šฐ ํ•œ ์นธ์”ฉ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ฐ์—ผ์„ ์‹œํ‚ต๋‹ˆ๋‹ค.
N์ดˆ๊ฐ€ ์ง€๋‚ฌ์„ ๋•Œ, ํ–‰๋ ฌ์˜ X,Y์ขŒํ‘œ์— ์–ด๋–ค ๋ฐ”์ด๋ผ์Šค๊ฐ€ ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

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

ํ–‰๋ ฌํฌ๊ธฐ์™€ ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜, ๊ทธ๋ž˜ํ”„์™€ ์‹œ๊ฐ„ ๋ฐ ์ตœ์ข…์ขŒํ‘œ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ฒ ์Šต๋‹ˆ๋‹ค.

for k in range(1,K+1):
    for i in range(N):
        for j in range(N):
            if graph[i][j] == k:
                que.append([i,j])

1~K๊นŒ์ง€์˜ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ BFS์•ˆ์— ๋„ฃ์–ด์„œ ์ „์—ผ์„ ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

dx = [-1,1,0,0]
dy = [0,0,1,-1]
def bfs():
    for _ in range(S):
        for _ in range(len(que)):

์ด ๋ถ€๋ถ„์ด ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. BFS์˜ ํ‰์†Œ while que์™€๋Š” ๋‹ค๋ฅด๊ฒŒ 1์ดˆ,2์ดˆ,3์ดˆ... ์ผ๋•Œ 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>=N:
                    continue 

4๋ฐฉํ–ฅ ๊ฒ€์‚ฌ๋ฅผ ์‹ค์‹œํ•ด์ฃผ๊ณ 

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

๋งŒ์•ฝ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๊ฐ์—ผ ์•ˆ๋œ ๊ตฌ๊ฐ„์ด ์žˆ๋‹ค๋ฉด ์ „ ์‹œํ—˜๊ด€์— ์ ํžŒ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ๊ฐ์—ผ์„ ์‹œ์ผœ์ฃผ๊ณ  ํ•ด๋‹น ๊ตฌ๊ฐ„๋„ ๊ฒ€์‚ฌ ์˜ˆ์ •์— ํฌํ•จ์‹œํ‚ค๋„๋ก ํ•ฉ์‹œ๋‹ค.

์ˆœ์„œ๋ฅผ ๋ณด๋ฉด
1์ดˆ-> 1,2,3....๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ ํžŒ ์ƒํ•˜์ขŒ์šฐ ํ•œ์นธ๋งŒ ๊ฐ์—ผ ์‹คํ–‰ (๋‹ค์Œ ํ›„๋ณด que์— append)
2์ดˆ-> 1,2,3....๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ ํžŒ ๋‹ค์Œ ํ›„๋ณด๋“ค ์ƒํ•˜์ขŒ์šฐ ํ•œ์นธ๋งŒ ๊ฐ์—ผ ์‹คํ–‰ (๊ทธ๋‹ค์Œ ํ›„๋ณด que์— append)
2์ดˆ-> 1,2,3....๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ ํžŒ ๊ทธ๋‹ค์Œ ํ›„๋ณด๋“ค ์ƒํ•˜์ขŒ์šฐ ํ•œ์นธ๋งŒ ๊ฐ์—ผ ์‹คํ–‰ (๊ทธ๋‹ค๋‹ค์Œ ํ›„๋ณด que์— append)
,
.
.
์ด๋Ÿฐ์”ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

bfs()
print(graph[X-1][Y-1])

๋งˆ์ง€๋ง‰์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋ฉด ๋‹ต์ด ์ •์ƒ์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


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

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