๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ: 5๊ฐœ์˜ ๋Œ€๊ธฐ์‹ค ์ •๋ณด
  • ์ถœ๋ ฅ: ๊ฐ ๋Œ€๊ธฐ์‹ค์—์„œ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ž˜ ํ–ˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅ

2. ์ฝ”๋“œ

import queue

D = ((-1,0),(1,0),(0,-1),(0,1))#์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•œ delta array

def bfs(place, row, col): #P์˜ ์œ„์น˜์—์„œ bfs์‹คํ–‰
    visited = [[False for _ in range(5)] for _ in range(5)] #5x5
    q = queue.Queue()
    visited[row][col] = True #ํ ๊บผ๋‚ด๊ธฐ์ „์— ๋งˆํ‚น
    q.put((row,col,0)) #ํ–‰, ์—ด, ๊ฑฐ๋ฆฌ

    while not q.empty():
        curr = q.get()

        if curr[2] > 2: #๊ฑฐ๋ฆฌ๊ฐ€ 2๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด ์Šคํ‚ต
            continue
        if curr[2] !=0 and place[curr[0]][curr[1]] == 'P': #์‹œ์ž‘์œ„์น˜๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ P๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ
            return False

        #๊ธธ์ด 2์ดํ•˜์ผ ๋•Œ
        for i in range(4):
            nr = curr[0] +D[i][0]
            nc = curr[1] +D[i][1]
            if nr < 0 or nr >4 or nc <0 or nc >4:#๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค๋ฉด
                continue
            if visited[nr][nc]: #์ด๋ฏธ ํ์— ๋„ฃ์€ ์ ์ด ์žˆ๋‹ค๋ฉด ๋‹ค์‹œ ๋„ฃ์„ ํ•„์š”๊ฐ€์—†์Œ
                continue
            if place[nr][nc] == 'X':#ํŒŒํ‹ฐ์…˜์ด ์žˆ์œผ๋ฉด ์ด๋™ํ•  ์ˆ˜ ์—†์Œ
                continue
            visited[nr][nc] = True
            q.put((nr,nc, curr[2]+1))
    return True


def check(place):
    for r in range(5):
        for c in range(5):
            if place[r][c] == 'P':
                if bfs(place,r,c) == False: #๊ฑฐ๋ฆฌ๋‘๊ธฐ ์•ˆ์ง€์ผœ์ง
                    return False
    return True #์กฐ๊ฑด๋ฌธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š์•„์„œ ๋ชจ๋‘ ํƒ์ƒ‰์„ ์™„๋ฃŒํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด True ๋ฆฌํ„ด


def solution(places):
    answer = []

    for place in places:
        if check(place): #๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ๋Š”์ง€
            answer.append(1)
        else:
            answer.append(0)

    return answer

3. ํšŒ๊ณ 

  • P๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ๊ฑฐ๋ฆฌ๊ฐ€ 2์ดํ•˜์ธ ๋ถ€๋ถ„๋งŒ bfs๋กœ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๊ฐ€ ์ž˜ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š”์ง€ ์ฝ”๋“œ๋ฅผ ์งœ์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐ
  • bfs ๊ตฌํ˜„ ๋ถ€๋ถ„์—์„œ ๋ง‰ํ˜”๋‹ค.
  • ๊ธธ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ์ถ”์ ํ• ์ง€? (2์ดํ•˜๋งŒ ํƒ์ƒ‰ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด)
    • ํ์— ๊ฑฐ๋ฆฌ์ •๋ณด๋ฅผ ์ถ”๊ฐ€
    • ํ์—๋Š” ์ƒˆ๋กญ๊ฒŒ ํƒ์ƒ‰ํ•  ์ขŒํ‘œ์™€ P๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ์ขŒํ‘œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.
    • ํ์—์„œ ์ •๋ณด๋ฅผ ๊บผ๋ƒˆ์„ ๋•Œ, ๊ฑฐ๋ฆฌ๊ฐ€ 2๋ฅผ ์ดˆ๊ณผํ•˜๋Š”์ง€ ํ™•์ธ ํ›„,
      • ์ดˆ๊ณผํ•œ๋‹ค๋ฉด continue๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•œ๋‹ค.
      • ๊ฑฐ๋ฆฌ๊ฐ€ 2์ด๋‚ด์ธ ์ขŒํ‘œ์˜ ์ƒํ•˜์ขŒ์šฐ๋งŒ์„ ํƒ์ƒ‰ํ•˜๋„๋ก ํ•œ๋‹ค.
  • ์ฃผ๋ณ€์„ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ํƒ์ƒ‰ํ• ์ง€?
    • delta array๋ฅผ ํ™œ์šฉํ•ด์„œ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰
    • ์ƒˆ๋กญ๊ฒŒ ๋ฐœ๊ฒฌํ•œ ํƒ์ƒ‰๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„์˜ ์ขŒํ‘œ(nr,nc),ํ˜„์žฌ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ+1(curr[2]+1)ํ•ด์„œ ํ์— ๋‹ด๊ธฐ
      • nr,nc๊ฐ€ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์„ ๊ฒฝ์šฐ, ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์ขŒํ‘œ์ธ๊ฒฝ์šฐ, ํ•ด๋‹น ์œ„์น˜์— ํŒŒํ‹ฐ์…˜์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” continue๋ฅผ ํ†ตํ•ด ์•„๋ž˜ ๋‚ด์šฉ ๊ฑด๋„ˆ๋›ฐ๊ธฐ(ํ์— ๋„ฃ์ง€ ๋ชปํ•˜๋„๋ก)
  • ํŒŒํ‹ฐ์…˜์ด ์žˆ์„ ๋•Œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ํ• ์ง€?
    • P์™€ ๋˜ ๋‹ค๋ฅธ P๊ฐ€ ๊ฑฐ๋ฆฌ 2 ์ด๋‚ด์— ์žˆ์„ ๋•Œ, X๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ ค๋‹ˆ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ์ƒ๊ฐ์ด ์•ˆ๋‚จ
    • ํ’€์ด๋ฅผ ๋ณด๋‹ˆ, ๋Œ€๊ธฐ์‹ค ๋ณ„๋กœ P๋ฅผ ๋จผ์ € ์ฐพ๊ณ (checkํ•จ์ˆ˜), ๊ทธ ๋•Œ์˜ ์ขŒํ‘œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ , ๊ธธ์ด 2์ด๋‚ด์ผ ๋•Œ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ(bfsํ•จ์ˆ˜ ๋‚ด์˜ for๋ฌธ)์—์„œ ํŒŒํ‹ฐ์…˜์ด ์žˆ์œผ๋ฉด ํ์— ๋„ฃ์ง€ ์•Š๋„๋ก ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์•ผํ•จ
      • if place[nr][nc] == 'X' : continue
      • ํ•ด๋‹น ๋ฐฉํ–ฅ์˜ ํƒ์ƒ‰์„ ์ค‘์ง€์‹œํ‚ด

4. ์ฐธ๊ณ 

https://www.youtube.com/watch?v=hCVgKE6qwFs