[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ / ํŒŒ์ด์ฌ / Python / 2021์นด์นด์˜ค ์ธํ„ด์‹ญ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ / ํŒŒ์ด์ฌ / Python / 2021์นด์นด์˜ค ์ธํ„ด์‹ญ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿ’กsolutions )

๐Ÿ’ฌ ๋Œ€๊ธฐ์‹ค์˜ ๊ฐ ์นธ์„ ํ•˜๋‚˜์˜ ์ •์ ์œผ๋กœ ๋ณด๊ณ , ์‘์‹œ์ž๋“ค์ด ์žˆ๋Š” ์ •์ ์„ ๊ธฐ์ค€์œผ๋กœ bfs ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•จ.

๐Ÿ’ฌ ๊ฐ ์ •์ ์—์„œ ์ธ์ ‘ํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ๋“ค์„ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ ํŒŒํ‹ฐ์…˜์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์ง€๋‚˜๊ฐ€์ง€ ๋ชปํ•˜๊ณ , ๊ฑฐ๋ฆฌ 2์ด๋‚ด์˜ ์ •์ ๋“ค๋งŒ ์‚ดํŽด๋ณด๋ฉฐ ๋‹ค๋ฅธ ์‘์‹œ์ž์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•จ.

๐Ÿ’ฌ ์ด๋•Œ ์ง€๋‚˜๊ฐ„ ์ •์ ์€ ๋‹ค์‹œ ์ง€๋‚˜๊ฐ€์ง€ ์•Š๊ธฐ ์œ„ํ•ด visit ๋ฐฐ์—ด์„ ํ†ตํ•ด ์ค‘๋ณต์„ ์ฒดํฌํ•จ.

 

๐Ÿ‘จ‍๐Ÿ’ปcode )

from collections import deque

dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]  # ์ƒํ•˜์ขŒ์šฐ

def bfs(place, i, j):
    visit = [[0]*5 for _ in range(5)]
    q = deque()
    q.append((i, j, 0))
    visit[i][j] = 1

    while q:
        x, y, dist = q.popleft()
        if 0 < dist < 3 and place[x][y] == 'P':
            return False
        if dist > 2:
            break  # ๊ฑฐ๋ฆฌ๊ฐ€ 3๋ถ€ํ„ฐ๋Š” ํƒ์ƒ‰ ์ค‘๋‹จ(๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ž˜ ์ง€ํ‚จ ๊ฒฝ์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์—)
        for k in range(4):
            nx, ny, nd = x + dx[k], y + dy[k], dist + 1
            if 0 <= nx < 5 and 0 <= ny <5:
                if place[nx][ny] != 'X' and not visit[nx][ny]:  # ํŒŒํ‹ฐ์…˜์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์•„๋‹ˆ๋ฉด ์ด๋™๊ฐ€๋Šฅ
                    q.append((nx, ny, nd))
                    visit[nx][ny] = 1
    return True

def solution(places):
    answer = []

    for place in places:
        chk = 0
        for i in range(len(place)):
            for j in range(len(place[0])):
                if place[i][j] == "P":
                    if not bfs(place, i, j):
                        answer.append(0)
                        chk = 1
                        break
            if chk:
                break
        else:
            answer.append(1)
    return answer

 

๐Ÿ“Œdescription )

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ

[["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]] [1, 0, 1, 1, 1]

programmers.co.kr


๋ฌธ์ œ ์„ค๋ช…

๊ฐœ๋ฐœ์ž๋ฅผ ํฌ๋งํ•˜๋Š” ์ฃ ๋ฅด๋””๊ฐ€ ์นด์นด์˜ค์— ๋ฉด์ ‘์„ ๋ณด๋Ÿฌ ์™”์Šต๋‹ˆ๋‹ค.

์ฝ”๋กœ๋‚˜ ๋ฐ”์ด๋Ÿฌ์Šค ๊ฐ์—ผ ์˜ˆ๋ฐฉ์„ ์œ„ํ•ด ์‘์‹œ์ž๋“ค์€ ๊ฑฐ๋ฆฌ๋ฅผ ๋‘ฌ์„œ ๋Œ€๊ธฐ๋ฅผ ํ•ด์•ผํ•˜๋Š”๋ฐ ๊ฐœ๋ฐœ ์ง๊ตฐ ๋ฉด์ ‘์ธ ๋งŒํผ
์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ๋Œ€๊ธฐ์‹ค์— ๊ฑฐ๋ฆฌ๋ฅผ ๋‘๊ณ  ์•‰๋„๋ก ์•ˆ๋‚ดํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๋Œ€๊ธฐ์‹ค์€ 5๊ฐœ์ด๋ฉฐ, ๊ฐ ๋Œ€๊ธฐ์‹ค์€ 5x5 ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค.
  2. ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์œ„ํ•˜์—ฌ ์‘์‹œ์ž๋“ค ๋ผ๋ฆฌ๋Š” ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ1๊ฐ€ 2 ์ดํ•˜๋กœ ์•‰์ง€ ๋ง์•„ ์ฃผ์„ธ์š”.
  3. ๋‹จ ์‘์‹œ์ž๊ฐ€ ์•‰์•„์žˆ๋Š” ์ž๋ฆฌ ์‚ฌ์ด๊ฐ€ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๋ง‰ํ˜€ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด,

์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ž๋ฆฌ ์‚ฌ์ด์— ํŒŒํ‹ฐ์…˜์ด ์กด์žฌํ•œ๋‹ค๋ฉด ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 2์—ฌ๋„ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํŒŒํ‹ฐ์…˜์„ ์‚ฌ์ด์— ๋‘๊ณ  ์•‰์€ ๊ฒฝ์šฐ๋„ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ž๋ฆฌ ์‚ฌ์ด๊ฐ€ ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ 2์ด๊ณ  ์‚ฌ์ด์— ๋นˆ ํ…Œ์ด๋ธ”์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
     
์‘์‹œ์ž๊ฐ€ ์•‰์•„์žˆ๋Š” ์ž๋ฆฌ(P)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋นˆ ํ…Œ์ด๋ธ”(O)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜(X)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

5๊ฐœ์˜ ๋Œ€๊ธฐ์‹ค์„ ๋ณธ ์ฃ ๋ฅด๋””๋Š” ๊ฐ ๋Œ€๊ธฐ์‹ค์—์„œ ์‘์‹œ์ž๋“ค์ด ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ž˜ ๊ธฐํ‚ค๊ณ  ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ž๋ฆฌ์— ์•‰์•„์žˆ๋Š” ์‘์‹œ์ž๋“ค์˜ ์ •๋ณด์™€ ๋Œ€๊ธฐ์‹ค ๊ตฌ์กฐ๋ฅผ ๋Œ€๊ธฐ์‹ค๋ณ„๋กœ ๋‹ด์€ 2์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด places๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ๋Œ€๊ธฐ์‹ค๋ณ„๋กœ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์œผ๋ฉด 1์„, ํ•œ ๋ช…์ด๋ผ๋„ ์ง€ํ‚ค์ง€ ์•Š๊ณ  ์žˆ์œผ๋ฉด 0์„ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.





๋ฐ˜์‘ํ˜•