출처 : 백준, https://www.acmicpc.net/problem/4396
4396번: 지뢰 찾기
지뢰찾기는 n × n 격자 위에서 이루어진다. m개의 지뢰가 각각 서로 다른 격자 위에 숨겨져 있다. 플레이어는 격자판의 어느 지점을 건드리기를 계속한다. 지뢰가 있는 지점을 건드리면 플레이어
www.acmicpc.net
더보기
풀이
def makeNumber(current, map, output):
i, j = current
di = [0, 1, -1, 0, -1, -1, 1, 1]
dj = [1, 0, 0, -1, -1, 1, -1, 1]
mine = 0
if map[i][j] == '*':
return True
for index in range(8):
ni = i + di[index]
nj = j + dj[index]
if 0 <= ni < len(map) and 0 <= nj < len(map) and map[ni][nj] == '*':
mine += 1
output[i][j] = mine
return False
n = int(input())
map = [list(input()) for _ in range(n)]
open = [list(input()) for _ in range(n)]
output = [list('.' for _ in range(n)) for _ in range(n)]
mine = False
for i in range(n):
for j in range(n):
if open[i][j] == 'x':
mine |= makeNumber((i, j), map, output)
if mine:
for i in range(n):
for j in range(n):
if map[i][j] == '*':
output[i][j] = '*'
for line in output:
for char in line:
print(char, end='')
print()
시간 복잡도
O(N^2)
알게 된 점
-
고찰
-
'알고리즘 > Python' 카테고리의 다른 글
[python] 프로그래머스 - 여행 경로 (0) | 2022.06.29 |
---|---|
[python] 백준 1913 - 달팽이 (0) | 2022.06.29 |
[python] 백준 21608 - 상어 초등학교 (0) | 2022.06.29 |
[python] 프로그래머스 - 단어 변환 (0) | 2022.06.29 |
[python] 프로그래머스 - 네트워크 (0) | 2022.06.28 |