본문 바로가기

백준문제풀이

[백준 문제풀이] 3085번 : 사탕게임

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

 

3085번: 사탕 게임

예제 3의 경우 4번 행의 Y와 C를 바꾸면 사탕 네 개를 먹을 수 있다.

www.acmicpc.net

💡 상하좌우를 확인해야 하는데, 위에서부터 순차적으로 확인한다면 위쪽과 왼쪽은 이미 이전에 확인되는 부분이기 때문에 오른쪽과 아래쪽만 확인하면 된다. (check 함수)

 

N = int(input())

table = [list(map(str, input())) for i in range(N)]

result = 0

#문자를 바꿨을 때 가장 긴 연속한 부분 찾기
def check(table):
    count = 0
    for i in range(N):
        row = 1
        col = 1
        for j in range(N-1):
            #열순회
            if table[i][j] == table[i][j+1]:
                row += 1
            else:
                count = max(count, row)
                row = 1
            #행순회
            if table[j][i] == table[j+1][i]:
                col += 1
            else:
                count = max(count, col)
                col = 1
        count = max(count, row, col)
    return count

for i in range(N):
    for j in range(N-1):
        #열확인 -> 오른쪽에 다른문자가 있을때 바꾸고 최대길이인지 확인
        if table[i][j] != table[i][j+1]:
            temp = table[i][j]
            table[i][j] = table[i][j+1]
            table[i][j+1] = temp

            result = max(result, check(table))

            #다시 원래대로 되돌리기
            temp = table[i][j]
            table[i][j] = table[i][j+1]
            table[i][j+1] = temp
        #행확인
        if table[j][i] != table[j+1][i]:
            temp = table[j][i]
            table[j][i] = table[j+1][i]
            table[j+1][i] = temp

            result = max(result, check(table))
            #다시 원래대로 되돌리기
            temp = table[j][i]
            table[j][i] = table[j+1][i]
            table[j+1][i] = temp

print(result)