본문 바로가기

알고리즘 문제풀이

백준 1652. 누울 자리를 찾아라 Python 구현 문자열 실버5

정답 풀이

# 실버5
n = int(input())
condo = [list(input()) for _ in range(n)]
cnt = 0
rows = 0
cols = 0
for i in range(n):
    for j in range(n):
        if condo[i][j] == '.':
            cnt += 1
        else:
            if cnt >= 2:
                rows += 1
                cnt = 0
            else:
                cnt = 0
    if cnt >= 2:
        rows += 1
        cnt = 0
    else:
        cnt = 0
# 세로
cnt = 0
for i in range(n):
    for j in range(n):
        if condo[j][i] == '.':
            cnt += 1
        else:
            if cnt >= 2:
                cols += 1
                cnt = 0
            else:
                cnt = 0
    if cnt >= 2:
        cols += 1
        cnt = 0
    else:
        cnt = 0
print(rows, cols)
rows = 0
cols = 0
for i in range(n):
    for j in range(n-1):
        if condo[i][j] == '.':
            if condo[i][j] == condo[i][j+1] and (j+1 == n-1 or condo[i][j+2] == 'X'):
                rows += 1
        else:
            if condo[i][j+1] == condo[i][j + 2] and (j + 2 == n - 1 or condo[i][j + 3] == 'X'):
                rows += 1


for j in range(n-1):
    for i in range(n):
        if condo[i][j] == '.':
            if condo[j][i] == condo[j+1][i] and (j+1 == n-1 or condo[j+2][i] == 'X'):
                cols += 1
        else:
            if condo[j+1][i] == condo[j+2][i] and (j+2 == n-1 or condo[j+3][i] == 'X'):
                cols += 1
print(rows, cols)