파비의 매일매일 공부기록

[BOJ] 1996 본문

Problem Solving/BOJ

[BOJ] 1996

fabichoi 2022. 4. 8. 23:45

https://www.acmicpc.net/problem/1996 

 

1996번: 지뢰 찾기

첫째 줄에 N(1 ≤ N ≤ 1,000)이 주어진다. 다음 N개의 줄에는 지뢰 찾기 map에 대한 정보가 주어지는데 '.' 또는 숫자로 이루어진 문자열이 들어온다. '.'는 지뢰가 없는 것이고 숫자는 지뢰가 있는 경

www.acmicpc.net

주어진 조건대로 구현하면 됨.
딱히 어려운 건 없었으나, 문자(*, M) 출력이 조금 번거로웠음.

import sys

input = sys.stdin.readline
pos_x = [0, 1, 1, 1, 0, -1, -1, -1]
pos_y = [-1, -1, 0, 1, 1, 1, 0, -1]

n = int(input())
board = []
for _ in range(n):
    board.append(list(input()))
res = [[0 for _ in range(n)] for __ in range(n)]


def fill_mines(board, x, y):
    if board[y][x] == '.':
        return
    v = int(board[y][x])

    for i in range(8):
        if x + pos_x[i] < 0 or y + pos_y[i] < 0 or x + pos_x[i] >= n or y + pos_y[i] >= n:
            continue
        res[y + pos_y[i]][x + pos_x[i]] += v


for y in range(n):
    for x in range(n):
        fill_mines(board, x, y)

for y in range(n):
    for x in range(n):
        if board[y][x] != '.':
            res[y][x] = '*'
        else:
            if int(res[y][x]) > 9:
                res[y][x] = 'M'

        print(res[y][x], end='')
    print('')
반응형

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 2028  (0) 2022.04.10
[BOJ] 2010  (0) 2022.04.09
[BOJ] 1977  (0) 2022.04.07
[BOJ] 1975  (0) 2022.04.06
[BOJ] 1964 [재시도 필요]  (0) 2022.04.05
Comments