반응형
https://www.acmicpc.net/problem/3085
3085번: 사탕 게임
예제 3의 경우 4번 행의 Y와 C를 바꾸면 사탕 네 개를 먹을 수 있다.
www.acmicpc.net
한 번 바꾼 후 전체를 탐색해서 색이 같으면 count + 1 색이 다르면 초기화를 진행한다. 위에서 아래로 하는 탐색과 아래에서 위로 하는 탐색이 같고 왼쪽에서 오른쪽으로 하는 탐색과 오른쪽에서 왼쪽으로 하는 탐색이 같으므로 이 부분을 배제한다.
n = int(input())
graph = [list(input()) for _ in range(n)]
dx = [1,0]
dy = [0,1]
length = 0
def change(x, y, color):
for i in range(2):
nx = x + dx[i]
ny = y + dy[i]
if nx < n and ny < n:
if color[x][y] != color[nx][ny]:
color[x][y], color[nx][ny] = color[nx][ny], color[x][y]
check(color)
color[x][y], color[nx][ny] = color[nx][ny], color[x][y]
if length == n:
return
def check(graph):
global length
result = 1
for i in range(n):
row = 1
col = 1
for j in range(1, n):
if graph[i][j] == graph[i][j-1]:
row += 1
else: row = 1
if graph[j][i] == graph[j-1][i]:
col += 1
else: col = 1
result = max(row, col, result)
length = max(result, length)
for i in range(n):
for j in range(n):
change(i, j, graph)
if length == n:
break
print(length)
length에 값을 넣는 과정에서 들여쓰기와 max에 length를 따로 넣지 않은 실수를 해서 시간이 많이 걸렸다.
반응형
'알고리즘' 카테고리의 다른 글
백준 2231 자바 (0) | 2022.03.05 |
---|---|
백준 2636 치즈 자바 (0) | 2022.03.05 |
백준 1541 파이썬 (0) | 2021.11.18 |
백준 1931 파이썬 (0) | 2021.11.17 |
백준 13305 주유소 파이썬 (0) | 2021.11.17 |