ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [λ°±μ€€] 3187 μ–‘μΉ˜κΈ° 꿍 with Python
    PS 2022. 3. 14. 23:23
    728x90
    λ°˜μ‘ν˜•

    πŸ“Œ BOJ 3187 μ–‘μΉ˜κΈ° 꿍

    πŸ’‘ 쑰건

    1. 같은 μšΈνƒ€λ¦¬ μ˜μ—­ μ•ˆμ˜ μ–‘λ“€μ˜ μˆ«μžκ°€ λŠ‘λŒ€μ˜ μˆ«μžλ³΄λ‹€ 더 λ§Žμ„ 경우 λŠ‘λŒ€κ°€ μ „λΆ€ μž‘μ•„λ¨ΉνžŒλ‹€. λ¬Όλ‘  κ·Έ μ™Έμ˜ κ²½μš°λŠ” 양이 μ „λΆ€ μž‘μ•„λ¨ΉνžŒλ‹€.

    2. λ§Œμ•½ 빈 곡간을 '.'(점)으둜 λ‚˜νƒ€λ‚΄κ³  μšΈνƒ€λ¦¬λ₯Ό '#', λŠ‘λŒ€λ₯Ό 'v', 양을 'k'라고 λ‚˜νƒ€λ‚Έλ‹€λ©΄ λͺ‡λ§ˆλ¦¬μ˜ μ–‘κ³Ό λŠ‘λŒ€κ°€ λ‚¨μ•„μžˆλŠ”κ°€?

    3. μšΈνƒ€λ¦¬λ‘œ λ§‰νžˆμ§€ μ•Šμ€ μ˜μ—­μ—λŠ” μ–‘κ³Ό λŠ‘λŒ€κ°€ μ—†μœΌλ©° μ–‘κ³Ό λŠ‘λŒ€λŠ” λŒ€κ°μ„ μœΌλ‘œ 이동할 수 μ—†λ‹€.

    4. μ˜μ—­μ˜ μ„Έλ‘œμ™€ κ°€λ‘œμ˜ 길이λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 두 개의 μ •μˆ˜ R, C (3 ≀ R, C ≀ 250)

    5. BFS μœ ν˜•μ˜ 문제

    πŸ–₯ μ†ŒμŠ€ μ½”λ“œ

    from collections import deque
    from sys import stdin
    
    board = []
    n, m = map(int, stdin.readline().split())
    for i in range(n):
        board.append(list(stdin.readline().rstrip()))
    
    dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]
    visited = set()
    
    
    def bfs(x, y):
        wolf, sheep = 0, 0
    
        if board[x][y] == 'k':
            sheep += 1
        elif board[x][y] == 'v':
            wolf += 1
    
        q = deque()
        q.append((x, y))
        visited.add((x, y))
    
        while q:
            x, y = q.popleft()
            for i in range(4):
                nx, ny = dx[i] + x, dy[i] + y
                if -1 < nx < n and -1 < ny < m:
                    if (nx, ny) not in visited:
                        if board[nx][ny] == 'k':
                            sheep += 1
                            q.append((nx, ny))
                            visited.add((nx, ny))
    
                        elif board[nx][ny] == 'v':
                            wolf += 1
                            q.append((nx, ny))
                            visited.add((nx, ny))
    
                        elif board[nx][ny] == '.':
                            q.append((nx, ny))
                            visited.add((nx, ny))
    
        if wolf >= sheep:
            return wolf, 'w'
        else:
            return sheep, 's'
    
    
    ans = [0, 0]
    for i in range(n):
        for j in range(m):
            if board[i][j] != '#' and (i, j) not in visited:
                res, win = bfs(i, j)
                if win == 's':
                    ans[0] += res
                else:
                    ans[1] += res
    
    print(*ans)

    πŸ”– 예제 및 μ‹€ν–‰κ²°κ³Ό

    예제

    6 6
    ...#..
    .##v#.
    #v.#.#
    #.k#.#
    .###.#
    ...###

    μ‹€ν–‰κ²°κ³Ό

    0 2

    ⌨️ 문제 풀이

    1. μ–‘κ³Ό λŠ‘λŒ€μ™€ μšΈνƒ€λ¦¬ 정보λ₯Ό μž…λ ₯λ°›μ•„ board에 μ €μž₯ν•œλ‹€.

    2. 맡을 μˆœνšŒν•˜λ©΄μ„œ μšΈνƒ€λ¦¬κ°€ μ•„λ‹ˆλ©°, (i, j) μ’Œν‘œκ°€ λ°©λ¬Έν•˜μ§€ μ•Šμ€ μ’Œν‘œλΌλ©΄ bfs ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€.

    3. k일 λ•Œμ™€ v일 λ•Œ, .일 λ•Œ 큐에 집어넣고, 방문처리λ₯Ό ν•΄μ€€λ‹€.

    4. k일 λ•ŒλŠ” sheep λ³€μˆ˜μ— 1을 λ”ν•˜κ³ , v일 λ•ŒλŠ” wolf λ³€μˆ˜μ— 1을 더해쀀닀.

    5. λ§Œμ•½ λŠ‘λŒ€μ˜ μˆ˜κ°€ λ§Žλ‹€λ©΄ wolf, 'w' λ₯Ό λ°˜ν™˜ν•˜κ³ 
      λ§Œμ•½ μ–‘μ˜ μˆ˜κ°€ λ§Œγ„·λ‚˜λ©΄ sheep, 's' 을 λ°˜ν™˜ν•œλ‹€.

    6. λ°˜ν™˜λ°›μ€ 결과에 λ”°λΌμ„œ μ–‘κ³Ό λŠ‘λŒ€μ˜ 수λ₯Ό ans에 각각 μ €μž₯ν•œλ‹€.
      ansλŠ” μ•žλΆ€ν„° μ–‘κ³Ό λŠ‘λŒ€μ˜ μˆ˜λ‹€.

    πŸ’Ύ λŠλ‚€μ 

    1. BSFλ₯Ό μ‚¬μš©ν•˜λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€. 이와 같은 μœ ν˜•μ΄ μ€κ·Όνžˆ λ§Žμ•„ μ—°μŠ΅ν•˜κΈ° 쒋은 문제라고 μƒκ°ν•©λ‹ˆλ‹€.
    λ°˜μ‘ν˜•

    λŒ“κΈ€

Designed by Tistory.