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

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

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

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

13565๋ฒˆ - ์นจํˆฌ

์œ„์—์„œ๋ถ€ํ„ฐ ์ž‰ํฌ๋ฅผ ์นจํˆฌ์‹œํ‚ค๋Š”๋ฐ ์•„๋ž˜์ชฝ๊นŒ์ง€ ๋‹ฟ์„์ง€ ์•ˆ๋‹ฟ์„์ง€ ๋งž์ถ”๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

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

๊ฒฉ์ž์˜ ๊ฐ€๋กœ์™€ ์„ธ๋กœ ๊ธธ์ด, ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฐ›์•„์ฃผ๊ณ  que๋ฅผ ํ•˜๋‚˜ ์„ ์–ธํ•ด์ค์‹œ๋‹ค.

from collections import deque
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>=M or ny>=N:
                continue

BFS ํ•ญ์ƒ ์“ฐ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ que์„ ์–ธํ•˜๊ณ  4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ์‹ค์‹œํ•ด์„œ

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

๋งŒ์•ฝ์— ์ž‰ํฌ๋ฅผ ๋–จ์–ดํŠธ๋ฆด์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์ธ 0์ด ๋‚˜์˜จ๋‹ค๋ฉด 9๋กœ ์ˆซ์ž๋ฅผ ๋ฐ”๊ฟ” ์ž‰ํฌ๋ฅผ ๋–จ์–ดํŠธ๋ ธ์Œ์„ ํ‘œ๊ธฐํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

for i in range(len(graph[0])):
    if graph[0][i] == 0:
        bfs(0,i)

๋ฌธ์ œ์—์„œ ์ž‰ํฌ๋Š” ์œ„์—์„œ๋ถ€ํ„ฐ ๋–จ์–ดํŠธ๋ฆฐ๋‹ค ํ–ˆ์œผ๋ฏ€๋กœ ๊ทธ๋ž˜ํ”„ ๋งจ ์œ„๋งŒ ์‹น๋‹ค ์กฐ์‚ฌํ•ด์„œ BFS๋ฅผ ๋Œ๋ ค์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

boolean = False
for j in range(len(graph[-1])):
    if graph[-1][j] == 9:
        boolean = True
        break
if boolean == True:
    print('YES')
else:
    print('NO')

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ๊ทธ๋ž˜ํ”„์˜ ํ•˜๋‹จ์— 9๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์ž‰ํฌ๊ฐ€ ๋๊นŒ์ง€ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ YES๋ฅผ, ์•„๋‹ˆ๋ผ๋ฉด NO๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


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

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