프로그래밍 공부/백준 (C++)
[C++ / 백준 3085번] 사탕 게임
Rocketbabydolls
2024. 12. 5. 00:52
좀 멍청하게 풀었나 해서 다른 사람들의 풀이를 확인 해 보았었는데, 메서드 화 하지 않고 푼 것 말고는 딱히 다른 효율적인 방법이 없었다.
어짜피 브루트 포스로 풀이하는 것이기도 했고, 다음 문제로 넘어가기로 했다.
#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;
}