๐๋ฌธ์ ํ์ํ๊ธฐ
- NxN(3 ≤ N ≤ 50) ํฌ๊ธฐ์ ์ฌํ์ ์ฑ์ ๋์
- ์ฌํ์ ์์ด ๋ค๋ฅธ ์ธ์ ํ ๋ ์นธ์ ๊ณจ๋ผ ๊ณ ๋ฅธ ์นธ์ ์๋ ์ฌํ์ ์๋ก ๊ตํ
- ๋ชจ๋ ๊ฐ์ ์์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๋ ๊ฐ์ฅ ๊ธด ์ฐ์ ๋ถ๋ถ(ํor์ด)์ ๊ณจ๋ผ ๊ทธ ์ฌํ์ ๋ชจ๋ ๋จน์
- ์ฌํ์ด ์ฑ์์ง ์ํ๊ฐ ์ฃผ์ด์ก์ ๋, ๋จน์ ์ ์๋ ์ต๋ ์ฌํ์ ๊ฐ์ ๊ตฌํ๊ธฐ
ํ๋ฐฉํฅ, ์ด๋ฐฉํฅ์ผ๋ก ์ธ์ ํ ์นธ๋ค์ ์๋ก ๊ตํํด์ฃผ๋ฉด์ ๊ฐ ๋ฐฉํฅ์์ ๊ฐ์ ์์ด ์ฐ์์ ์ผ๋ก ๋์ธ ์ต๋ ๊ธธ์ด๋ฅผ ๊ตฌํด์ ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ฉด ๋ชจ๋ ์นธ์ ๋ํ ํ์์ด ๋๋๊ณ ๋จน์ ์ ์๋ ์ต๋ ์ฌํ์ ๊ฐ์๋ฅผ ๊ตฌํ ์ ์๋ค.
๐์ฝ๋ ์ค๊ณํ๊ธฐ
- ๋ณด๋์ ํฌ๊ธฐ N์ ์ ๋ ฅ๋ฐ๋๋ค.
- ๋ฆฌ์คํธ candies์ ๋ณด๋์ ์ฑ์์ ธ ์๋ ์ฌํ์ ์์์ ์ ๋ ฅ๋ฐ๋๋ค.
- ๋จน์ ์ ์๋ ์ต๋ ์ฌํ ๊ฐ์๋ฅผ ์ ์ฅํ ๋ณ์ max_candy_cnt๋ฅผ 0์ผ๋ก ์ด๊ธฐํํ๋ค.
- ๊ฐ ์นธ์ ๋๋ฉด์ ์ค๋ฅธ์ชฝ์นธ๊ณผ ๊ตํ / ์๋์นธ๊ณผ ๊ตํํ๋ค.
- ์ค๋ฅธ์ชฝ์นธ๊ณผ ๊ตํํ๋ค๋ฉด, ํด๋น์นธ์ ์ด๋ฐฉํฅ์ ํ์ํด์ ๊ฐ์ฅ ๊ธด ์ฐ์ ๋ถ๋ถ์ ๊ฐ์๋ฅผ ํ์ธํ๊ณ , ์๋์นธ๊ณผ ๊ตํํ๋ค๋ฉด ํด๋น์นธ์ ํ๋ฐฉํฅ์ ํ์ํด์ ๊ฐ์ฅ ๊ธด ์ฐ์ ๋ถ๋ถ์ ๊ฐ์๋ฅผ ํ์ธํ์ฌ max_candy_cnt๋ฅผ ๊ฐฑ์ ํ๋ค.
- ๋ชจ๋ ์นธ์ ๋ํด ํ์์ด ์ข ๋ฃ๋๋ฉด max_candy_cnt๋ฅผ ์ถ๋ ฅํ๋ค.
๐์ ๋ต ์ฝ๋
N = int(input())
candies = [list(input()) for _ in range(N)]
def get_max_candy_cnt():
max_candy_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_candy_cnt = max(max_candy_cnt, cnt)
cnt = 1
for j in range(1, N):
if candies[j][i] == candies[j-1][i]:
cnt += 1
else:
cnt = 1
max_candy_cnt = max(max_candy_cnt, cnt)
return max_candy_cnt
max_candy_cnt = 0
for i in range(N):
for j in range(N):
if i < N-1 and candies[i][j] != candies[i+1][j]:
candies[i][j], candies[i+1][j] = candies[i+1][j], candies[i][j]
max_candy_cnt = max(max_candy_cnt, get_max_candy_cnt())
candies[i][j], candies[i+1][j] = candies[i+1][j], candies[i][j]
if j < N-1 and candies[i][j] != candies[i][j+1]:
candies[i][j], candies[i][j+1] = candies[i][j+1], candies[i][j]
max_candy_cnt = max(max_candy_cnt, get_max_candy_cnt())
candies[i][j], candies[i][j+1] = candies[i][j+1], candies[i][j]
print(max_candy_cnt)
728x90