[BOJ] #16724 ํ”ผ๋ฆฌ ๋ถ€๋Š” ์‚ฌ๋‚˜์ด

    ๋ฐ˜์‘ํ˜•

     

     

    ๐ŸŒฑ ๋ฌธ์ œ

     

    16724๋ฒˆ: ํ”ผ๋ฆฌ ๋ถ€๋Š” ์‚ฌ๋‚˜์ด

    ์ฒซ ๋ฒˆ์งธ ์ค„์— ์ง€๋„์˜ ํ–‰์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N(1 ≤ N ≤ 1,000)๊ณผ ์ง€๋„์˜ ์—ด์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” M(1 ≤ M ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ง€๋„์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธธ์ด๊ฐ€ M์ธ ๋ฌธ์ž์—ด์ด ์ฃผ

    www.acmicpc.net

     

    ๐ŸŒฑ ํ’€์ด

    0. ๊ธฐ๋ณธ ๋กœ์ง

    safe_zone = []
    idx = 1
    for i in range(n):
        for j in range(m):
            if visited[i][j] == False:
                dfs(board, i, j, visited)
                idx += 1
    • ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ ๋Œ๋ฉด์„œ dfs ํƒ์ƒ‰์„ ํ•ฉ๋‹ˆ๋‹ค.
    • dfs ํ•จ์ˆ˜์—์„œ ๋ฌด์กฐ๊ฑด ํ•˜๋‚˜์˜ ์‚ฌ์ดํด์ด ์ฐพ์•„์ง€๋ฏ€๋กœ dfsํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜์˜ค๋ฉด idx ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.

     

    1. dfs ํ•จ์ˆ˜

    def dfs(board, x, y, visited):
        global idx
        if x < 0 or y < 0 or x > n - 1 or y > m - 1:
            return 0
    
        if visited[x][y] != False:
            if visited[x][y] == idx:
                safe_zone.append((x, y))
            return 1
    
        visited[x][y] = idx
        dx, dy = movements[board[x][y]]
        nx, ny = x + dx, y + dy
        dfs(board, nx, ny, visited)
    • ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ˜„์žฌ ๋Œ๊ณ  ์žˆ๋Š” ์‚ฌ์ดํด์˜ ๋ฒˆํ˜ธ์ธ idx๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋•Œ ์ž…๋ ฅ๋ฐ›์€ ๋ฐฉํ–ฅ(D, U, L, R)์— ๋”ฐ๋ผ dfs ํƒ์ƒ‰์„ ํ–ˆ์„ ๋•Œ, ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ผ๋ฉด ์‚ฌ์ดํด์ด ์™„์„ฑ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
    • ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋œ ๊ฐ’(idx)์ด ํ˜„์žฌ ๋Œ๊ณ  ์žˆ๋Š” ์‚ฌ์ดํด์˜ ๋ฒˆํ˜ธ์™€ ๊ฐ™๋‹ค๋ฉด ์ƒˆ๋กœ์šด ์‚ฌ์ดํด์ด ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์ด๋ฏ€๋กœ safe_zone์— ํ˜„์žฌ ์œ„์น˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ์‚ฌ์ดํด์— ํ˜„์žฌ ์œ„์น˜๊ฐ€ ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ safe_zone์— ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

     

    ๐ŸŒฑ ์ฝ”๋“œ

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # boj 16724 ํ”ผ๋ฆฌ ๋ถ€๋Š” ์‚ฌ๋‚˜์ด
    
    import sys
    from collections import deque
    
    input = sys.stdin.readline
    n, m = map(int, input().split())
    
    board = []
    for _ in range(n):
        board.append(list(map(str, input().rstrip())))
    
    # ๋ฐฉํ–ฅ ์„ค์ •
    movements = {'U': (-1, 0), 'D': (1, 0), 'L': (0, -1), 'R': (0, 1)}
    visited = [[False] * m for _ in range(n)]
    
    def dfs(board, x, y, visited):
        global idx
        if x < 0 or y < 0 or x > n - 1 or y > m - 1:
            return 0
    
        if visited[x][y] != False:
            if visited[x][y] == idx:
                safe_zone.append((x, y))
            return 1
    
        visited[x][y] = idx
        dx, dy = movements[board[x][y]]
        nx, ny = x + dx, y + dy
        return dfs(board, nx, ny, visited)
    
    safe_zone = []
    idx = 1
    for i in range(n):
        for j in range(m):
            if visited[i][j] == False:
                dfs(board, i, j, visited)
                idx += 1
    
    print(len(safe_zone))
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€