좀 멍청하게 풀었나 해서 다른 사람들의 풀이를 확인 해 보았었는데, 메서드 화 하지 않고 푼 것 말고는 딱히 다른 효율적인 방법이 없었다.
어짜피 브루트 포스로 풀이하는 것이기도 했고, 다음 문제로 넘어가기로 했다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N;
char a[51][51];
int main() {
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> a[i][j];
}
}
int max = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
char tmp;
if (i + 1 < N && a[i][j] != a[i + 1][j])
{
tmp = a[i][j];
a[i][j] = a[i + 1][j];
a[i + 1][j] = tmp;
// 행 검사
for (int x = 0; x < N; x++)
{
int cnt = 1;
for (int y = 1; y < N; y++)
{
if (a[x][y] == a[x][y - 1]) cnt++;
else
{
if (max < cnt) max = cnt;
cnt = 1;
}
}
if (max < cnt) max = cnt;
}
// 열 검사
for (int x = 0; x < N; x++)
{
int cnt = 1;
for (int y = 1; y < N; y++)
{
if (a[y][x] == a[y - 1][x]) cnt++;
else
{
if (max < cnt) max = cnt;
cnt = 1;
}
}
if (max < cnt) max = cnt;
}
tmp = a[i][j];
a[i][j] = a[i + 1][j];
a[i + 1][j] = tmp;
}
if (j + 1 < N && a[i][j] != a[i][j + 1])
{
tmp = a[i][j];
a[i][j] = a[i][j + 1];
a[i][j + 1] = tmp;
// 행 검사
for (int x = 0; x < N; x++)
{
int cnt = 1;
for (int y = 1; y < N; y++)
{
if (a[x][y] == a[x][y - 1]) cnt++;
else
{
if (max < cnt) max = cnt;
cnt = 1;
}
}
if (max < cnt) max = cnt;
}
// 열 검사
for (int x = 0; x < N; x++)
{
int cnt = 1;
for (int y = 1; y < N; y++)
{
if (a[y][x] == a[y - 1][x]) cnt++;
else
{
if (max < cnt) max = cnt;
cnt = 1;
}
}
if (max < cnt) max = cnt;
}
tmp = a[i][j];
a[i][j] = a[i][j + 1];
a[i][j + 1] = tmp;
}
}
}
cout << max;
return 0;
}
'프로그래밍 공부 > 백준 (C++)' 카테고리의 다른 글
[백준 15649번, 15650번 / C++] N과 M (0) | 2024.12.19 |
---|---|
[C++ / 백준 14500번] 테트로미노 (0) | 2024.12.07 |
[C++ / 백준 11054번] 가장 긴 바이토닉 부분 수열 (0) | 2024.11.30 |
[C++ / 백준 1392번] 정수 삼각형 (0) | 2024.11.29 |
[C++ / 백준 9465번] 스티커 (0) | 2024.11.28 |