🗒 3085번 문제

📌 2차원 배열을 사용해서 문제를 풀어가자 ❗️
1️⃣ 사탕 색이 다른 인접한 두 칸을 골라서 색을 바꿔줘야 한다
2️⃣ 가로로 두 색을 고르는 경우, 세로로 두 색을 고르는 경우로 나눠서 찾아본다
3️⃣ 인접한 두 색상을 바꾸고 나서 모두 같은 색으로 이루어져 있는 연속 부분을 찾는다
4️⃣ 연속 부분도 가로로 연속, 세로로 연속 두 가지로 나눠서 찾아본다
5️⃣ 인접한 색상을 바꾸는 건 algorithm에 포함된 swap함수를 사용하자
6️⃣ 세로로 연속 찾기 countColumnCandy, 가로로 연속 찾기 countRowCandy
➰ 코드로 나타낸 3085번 ➰
#include <iostream>
#include <algorithm>
using namespace std;
int maxCount = 0;
void countColumnCandy(char arr[51][51], int size) {
for (int i = 0; i < size; i++) {
int count = 1;
for (int j = 0; j < size; j++) {
if(arr[i][j] == arr[i][j+1])
count++;
else {
if (count > maxCount)
maxCount = count;
count = 1;
}
}
}
}
void countRowCandy(char arr[51][51], int size) {
for (int i = 0; i < size; i++) {
int count = 1;
for (int j = 0; j < size; j++) {
if(arr[j][i] == arr[j+1][i])
count++;
else {
if (count > maxCount)
maxCount = count;
count = 1;
}
}
}
}
int main() {
int boardSize = 0;
char board[51][51];
cin >> boardSize;
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
cin >> board[i][j];
}
}
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize - 1; j++) {
swap(board[i][j], board[i][j+1]);
countRowCandy(board, boardSize);
countColumnCandy(board, boardSize);
swap(board[i][j+1], board[i][j]);
swap(board[j][i], board[j+1][i]);
countRowCandy(board, boardSize);
countColumnCandy(board, boardSize);
swap(board[j][i], board[j+1][i]);
}
}
cout << maxCount << endl;
}