ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 13459 ๊ตฌ์Šฌ ํƒˆ์ถœ with Python
    PS 2022. 6. 3. 02:29
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 13459 ๊ตฌ์Šฌ ํƒˆ์ถœ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ๊ตฌ์Šฌ ํƒˆ์ถœ์€ ์ง์‚ฌ๊ฐํ˜• ๋ณด๋“œ์— ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์„ ํ•˜๋‚˜์”ฉ ๋„ฃ์€ ๋‹ค์Œ, ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์„ ํ†ตํ•ด ๋นผ๋‚ด๋Š” ๊ฒŒ์ž„์ด๋‹ค.
    1. ๋ณด๋“œ์˜ ์„ธ๋กœ ํฌ๊ธฐ๋Š” N, ๊ฐ€๋กœ ํฌ๊ธฐ๋Š” M์ด๊ณ , ํŽธ์˜์ƒ 1ร—1ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค.
      N, M (3 โ‰ค N, M โ‰ค 10)
    1. ๊ฐ€์žฅ ๋ฐ”๊นฅ ํ–‰๊ณผ ์—ด์€ ๋ชจ๋‘ ๋ง‰ํ˜€์ ธ ์žˆ๊ณ , ๋ณด๋“œ์—๋Š” ๊ตฌ๋ฉ์ด ํ•˜๋‚˜ ์žˆ๋‹ค.
      ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ํฌ๊ธฐ๋Š” ๋ณด๋“œ์—์„œ 1ร—1ํฌ๊ธฐ์˜ ์นธ์„ ๊ฐ€๋“ ์ฑ„์šฐ๋Š” ์‚ฌ์ด์ฆˆ์ด๊ณ , ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค.
      ๊ฒŒ์ž„์˜ ๋ชฉํ‘œ๋Š” ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์„ ํ†ตํ•ด์„œ ๋นผ๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ, ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ ๋œ๋‹ค.
    1. ์™ผ์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ, ์œ„์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ, ์•„๋ž˜์ชฝ์œผ๋กœ ๊ธฐ์šธ์ด๊ธฐ์™€ ๊ฐ™์€ ๋„ค ๊ฐ€์ง€ ๋™์ž‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    1. ๊ฐ๊ฐ์˜ ๋™์ž‘์—์„œ ๊ณต์€ ๋™์‹œ์— ์›€์ง์ธ๋‹ค. ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋น ์ง€๋ฉด ์„ฑ๊ณต์ด์ง€๋งŒ, ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๊ตฌ๋ฉ์— ๋น ์ง€๋ฉด ์‹คํŒจ์ด๋‹ค.
      ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์ด ๋™์‹œ์— ๊ตฌ๋ฉ์— ๋น ์ ธ๋„ ์‹คํŒจ์ด๋‹ค. ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์€ ๋™์‹œ์— ๊ฐ™์€ ์นธ์— ์žˆ์„ ์ˆ˜ ์—†๋‹ค.
      ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ํฌ๊ธฐ๋Š” ํ•œ ์นธ์„ ๋ชจ๋‘ ์ฐจ์ง€ํ•œ๋‹ค. ๊ธฐ์šธ์ด๋Š” ๋™์ž‘์„ ๊ทธ๋งŒํ•˜๋Š” ๊ฒƒ์€ ๋” ์ด์ƒ ๊ตฌ์Šฌ์ด ์›€์ง์ด์ง€ ์•Š์„ ๋•Œ ๊นŒ์ง€์ด๋‹ค.
    1. ๋ณด๋“œ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, 10๋ฒˆ ์ดํ•˜๋กœ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์„ ํ†ตํ•ด ๋นผ๋‚ผ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
      ํŒŒ๋ž€ ๊ตฌ์Šฌ์„ ๊ตฌ๋ฉ์— ๋„ฃ์ง€ ์•Š์œผ๋ฉด์„œ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์„ 10๋ฒˆ ์ดํ•˜๋กœ ์›€์ง์—ฌ์„œ ๋นผ๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉด 1์„ ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
    1. ๋ฌธ์ž์—ด์€ '.', '#', 'O', 'R', 'B' ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
      '.'์€ ๋นˆ ์นธ์„ ์˜๋ฏธํ•˜๊ณ , '#'์€ ๊ณต์ด ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์žฅ์• ๋ฌผ ๋˜๋Š” ๋ฒฝ์„ ์˜๋ฏธํ•˜๋ฉฐ, 'O'๋Š” ๊ตฌ๋ฉ์˜ ์œ„์น˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
      'R'์€ ๋นจ๊ฐ„ ๊ตฌ์Šฌ์˜ ์œ„์น˜, 'B'๋Š” ํŒŒ๋ž€ ๊ตฌ์Šฌ์˜ ์œ„์น˜์ด๋‹ค.
      ์ž…๋ ฅ๋˜๋Š” ๋ชจ๋“  ๋ณด๋“œ์˜ ๊ฐ€์žฅ์ž๋ฆฌ์—๋Š” ๋ชจ๋‘ '#'์ด ์žˆ๋‹ค. ๊ตฌ๋ฉ์˜ ๊ฐœ์ˆ˜๋Š” ํ•œ ๊ฐœ ์ด๋ฉฐ, ๋นจ๊ฐ„ ๊ตฌ์Šฌ๊ณผ ํŒŒ๋ž€ ๊ตฌ์Šฌ์€ ํ•ญ์ƒ 1๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
    1. BFS, ๊ทธ๋ž˜ํ”„ํƒ์ƒ‰ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from collections import deque
    
    n, m = map(int, input().split())
    dx = [1, -1, 0, 0]
    dy = [0, 0, -1, 1]
    visit = [[[[False] * m for i in range(n)] for i in range(m)] for i in range(n)]
    s = []
    
    
    def move(i, j, dx, dy):
        c = 0
        while s[i + dx][j + dy] != "#" and s[i][j] != "O":
            i += dx
            j += dy
            c += 1
        return i, j, c
    
    
    def bfs():
        while q:
            ri, rj, bi, bj, d = q.popleft()
            if d > 10:
                break
            for i in range(4):
                nri, nrj, rc = move(ri, rj, dx[i], dy[i])
                nbi, nbj, bc = move(bi, bj, dx[i], dy[i])
                if s[nbi][nbj] != "O":
                    if s[nri][nrj] == "O":
                        print(1)
                        return
                    if nri == nbi and nrj == nbj:
                        if rc > bc:
                            nri -= dx[i]
                            nrj -= dy[i]
                        else:
                            nbi -= dx[i]
                            nbj -= dy[i]
                    if not visit[nri][nrj][nbi][nbj]:
                        visit[nri][nrj][nbi][nbj] = True
                        q.append([nri, nrj, nbi, nbj, d + 1])
        print(0)
    
    
    for i in range(n):
        a = list(input())
        s.append(a)
        for j in range(m):
            if a[j] == "R":
                ri, rj = i, j
            if a[j] == "B":
                bi, bj = i, j
    
    q = deque()
    q.append([ri, rj, bi, bj, 1])
    visit[ri][rj][bi][bj] = True
    
    bfs()

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    10 10
    ##########
    #R#...##B#
    #...#.##.#
    #####.##.#
    #......#.#
    #.######.#
    #.#....#.#
    #.#.##...#
    #O..#....#
    ##########

    ์‹คํ–‰๊ฒฐ๊ณผ

    1

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๊ตฌ์Šฌ์„ ๋„ค ๊ฐœ์˜ ๋ฐฉํ–ฅ์œผ๋กœ ๊ธฐ์šธ์˜€์„ ๋–„์˜ ์ƒํƒœ๋ฅผ ํ์— ๋„ฃ๊ณ  ๊ด€๋ฆฌํ•œ๋‹ค.
    1. ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์—๋Š” ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋„๋ก visit ๋ฆฌ์ŠคํŠธ์— ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    1. ํ์—์„œ ๊บผ๋‚ด๊ณ , ์ด๋™์„ ์‹œํ‚ฌ ์ขŒํ‘œ๋ฅผ move ํ•จ์ˆ˜๋กœ ๊ณ„์‚ฐํ•ด ํŒŒ๋ž€๊ตฌ์Šฌ์ด '0'์ด ์•„๋‹ˆ๊ณ  ๋นจ๊ฐ„ ๊ตฌ์Šฌ์ด '0'์œผ๋กœ ๊ฐˆ ๊ฒฝ์šฐ 1์„ ์ถœ๋ ฅํ•œ๋‹ค.
    1. ๋งŒ์•ฝ ์›€์ง์˜€์„ ๋•Œ ๋‘ ๊ตฌ์Šฌ์ด ์ด๋™ํ•  ์ขŒํ‘œ๊ฐ€ '0' ์ด๊ฑฐ๋‚˜ 10ํšŒ๋ฅผ ์ดˆ๊ณผํ•˜์—ฌ ๊ธฐ์šธ์˜€๋‹ค๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ๐Ÿ’พ ๋ฐฐ์šด์ 

    1. ๊ตฌ์Šฌ์„ ์›€์ง์—ฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„์ด๋””์–ด๋Š” ์‰ฌ์› ๋‹ค.
    1. ๊ตฌํ˜„์ด ์–ด๋ ค์› ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.