백준 3085 사탕 게임 Python

Derhon·2023년 12월 11일
0
post-custom-banner

백준 3085 사탕 게임

failed

확실히 알았다 .. 나 완탐 약하구나..

틀린 답

import sys
input = sys.stdin.readline

n = int(input().rstrip())
candies = [list(input().rstrip()) for _ in range(n)]
dx = [0, 1]
dy = [1, 0]
res = 0

def check():
    max_cnt = 1
    for i in range(n):
        cnt = 1
        for j in range(1, n):
            if candies[i][j] == candies[i][j-1]:
                cnt += 1
            else:
                cnt = 1
            max_cnt = max(max_cnt, cnt)
        cnt = 1
        for j in range(1, n):
            if candies[j][i] == candies[j-1][i]:
                cnt += 1
            else:
                cnt = 1
            max_cnt = max(max_cnt, cnt)
    return max_cnt

for i in range(n):
    for j in range(n):
        for d in range(2):
            nx = i + dx[d]
            ny = j + dy[d]
            if (0 <= nx < n) and (0 <= ny < n):
                candies[i][j] = candies[nx][ny]
                res = max(res, check())
                candies[nx][ny] = candies[i][j]

print(res)

맞은 답

import sys
input = sys.stdin.readline

n = int(input().rstrip())
candies = [list(input().rstrip()) for _ in range(n)]
dx = [0, 1]
dy = [1, 0]
res = 0

def check():
    max_cnt = 1
    for i in range(n):
        cnt = 1
        for j in range(1, n):
            if candies[i][j] == candies[i][j-1]:
                cnt += 1
            else:
                cnt = 1
            max_cnt = max(max_cnt, cnt)
        cnt = 1
        for j in range(1, n):
            if candies[j][i] == candies[j-1][i]:
                cnt += 1
            else:
                cnt = 1
            max_cnt = max(max_cnt, cnt)
    return max_cnt

for i in range(n):
    for j in range(n):
        for d in range(2):
            nx = i + dx[d]
            ny = j + dy[d]
            if (0 <= nx < n) and (0 <= ny < n):
                candies[i][j], candies[nx][ny] = candies[nx][ny], candies[i][j]
                res = max(res, check())
                candies[i][j], candies[nx][ny] = candies[nx][ny], candies[i][j]

print(res)

바보같이 바꿀 때 제대로 안바꿔줬다...하아

profile
🧑‍🚀 이사했어요 ⮕ https://99uulog.tistory.com/
post-custom-banner

0개의 댓글