๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 18242 - ๋„ค๋ชจ๋„ค๋ชจ ์‹œ๋ ฅ๊ฒ€์‚ฌ

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 18242๋ฒˆ

ํ’€์ด ๊ณผ์ •

๋„ค๋ชจ๋„ค๋ชจ ์•ˆ๊ณผ์˜ ์‹œ๋ ฅ ๊ฒ€์‚ฌ๋ฅผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๊ฒฉ์ž์—๋Š” ๋‹จ ํ•˜๋‚˜์˜ ์ง์‚ฌ๊ฐํ˜•์ด ์กด์žฌํ•œ๋‹ค๋Š” ๋ณด์žฅ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
์šฐ๋ฆฌ๊ฐ€ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฒซ๋ฒˆ์งธ ์ •๋ณด๋Š” ์™ผ์ชฝ ์ƒ๋‹จ ๋ชจ์„œ๋ฆฌ์˜ ์œ„์น˜์ž…๋‹ˆ๋‹ค.

์ดํ›„ ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ ๋ชจ์„œ๋ฆฌ์™€ ์™ผ์ชฝ ํ•˜๋‹จ ๋ชจ์„œ๋ฆฌ์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•ด์„œ ์ง์‚ฌ๊ฐํ˜•์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ๊ตฌํ•˜๊ณ 
์ด๋ฅผ ํ™œ์šฉํ•ด ์ง์‚ฌ๊ฐํ˜•์˜ ๊ฐ ๋ณ€์„ ๊ฒ€์‚ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys

N, M = list(map(int, sys.stdin.readline().split()))
board = [list(sys.stdin.readline().strip()) for _ in range(N)]


def get_pivot(board):
    pivot = None

    for x in range(N):
        for y in range(M):
            if board[x][y] == '#':
                pivot = [x, y]
                break
        if pivot is not None:
            break

    return pivot


def get_width_height(board, sx, sy):
    right_top, bottom_left = [], []

    # ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ ๋ชจ์„œ๋ฆฌ ์ฐพ๊ธฐ
    for y in range(M - 1, sy, -1):
        if board[sx][y] == '#':
            right_top = [sx, y]
            break

    # ์™ผ์ชฝ ํ•˜๋‹จ ๋ชจ์„œ๋ฆฌ ์ฐพ๊ธฐ
    for x in range(N - 1, sx, -1):
        if board[x][sy] == '#':
            bottom_left = [x, sy]
            break

    width = right_top[1] - sy + 1
    height = bottom_left[0] - sx + 1

    return width, height


def inspect_board(board, width, height, sx, sy):
    # UP, DOWN
    for y in range(sy, sy + width):
        if board[sx][y] == '.':
            return 'UP'
        if board[sx + height - 1][y] == '.':
            return 'DOWN'

    # LEFT, RIGHT
    for x in range(sx, sx + height):
        if board[x][sy] == '.':
            return 'LEFT'
        if board[x][sy + width - 1] == '.':
            return 'RIGHT'


def solution():
    pivot = get_pivot(board)
    width, height = get_width_height(board, *pivot)
    answer = inspect_board(board, width, height, *pivot)

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 20044 - Project Teams  (0) 2021.03.08
BOJ 1644 - ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ  (0) 2021.03.08
BOJ 9184 - ์‹ ๋‚˜๋Š” ํ•จ์ˆ˜ ์‹คํ–‰  (0) 2021.03.08
BOJ 14226 - ์ด๋ชจํ‹ฐ์ฝ˜  (0) 2021.03.08
BOJ 2529 - ๋ถ€๋“ฑํ˜ธ  (0) 2021.03.08

๐Ÿ’ฌ ๋Œ“๊ธ€