λ¬Έμ
https://www.acmicpc.net/problem/3085
λ¬Έμ μ΄ν΄
- N×N ν¬κΈ°μ 보λμ μ¬νμ 4κ°μ§ μμΌλ‘ μ±μμ Έμλ€.
- μΈμ ν λ μΉΈμ κ³¨λΌ μ¬νμ μλ‘ κ΅ννλ€.
- ν λλ μ΄μμ κ°μ μμΌλ‘ μ΄λ£¨μ΄μ Έ μλ κ°μ₯ κΈ΄ μ°μ λΆλΆμ ꡬνλ€.
λ¬Έμ νμ΄
π μ κ·Ό
λͺ¨λ κ²½μ°μ μλ₯Ό νμνλ μκ°λ³΅μ‘λλ₯Ό κ³μ°ν΄λ³΄λ©΄,
μ¬νμ κ΅ννλ λ° N * N, μ°μνλ μ΅λ κ°μλ₯Ό ꡬνλ λ° N * N μ΄λ―λ‘ μ΄ O(N^4)μ΄λ€.
N ≤ 50μ΄λ―λ‘ μ νμκ° λ΄μ κ³μ°ν μ μλ€.
π‘ λΈλ£¨νΈν¬μ€
- ν/μ΄μμ μΈμ ν λ μΉΈμ κ³¨λΌ μλ‘ μ¬νμ κ΅ννλ€.
- μ°μνλ ν/μ΄μ μ΅λ κ°μλ₯Ό μ°Ύλλ€.
1ν κ΅νν λλ§λ€ μ΅λ κ°μλ₯Ό μ°Ύλλ€.
μ½λ
#include <iostream>
#include <vector>
using namespace std;
/*
1. ν/μ΄μμ μΈμ ν λ μΉΈμ κ³¨λΌ μλ‘ μ¬νμ κ΅ννλ€.
2. μ°μνλ ν/μ΄μ μ΅λ κ°μλ₯Ό μ°Ύλλ€.
*/
int checkMax(int n, vector<vector<char>> &board) {
int cnt, ret = 1;
// νμμ μ°μ κ°μ
for (int i = 0; i < n; i++) {
cnt = 1;
for (int j = 1; j < n; j++) {
if (board[i][j] == board[i][j - 1]) cnt++;
else cnt = 1;
ret = max(cnt, ret);
}
}
// μ΄μμ μ°μ κ°μ
for (int j = 0; j < n; j++) {
cnt = 1;
for (int i = 1; i < n; i++) {
if (board[i][j] == board[i - 1][j]) cnt++;
else cnt = 1;
ret = max(cnt, ret);
}
}
return ret;
}
int swapCandy(int n, vector<vector<char>> &board) {
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// ν νμ
if (j < n - 1) {
swap(board[i][j], board[i][j + 1]);
ans = max(ans, checkMax(n, board));
swap(board[i][j], board[i][j + 1]);
}
// μ΄ νμ
if (i < n - 1) {
swap(board[i][j], board[i + 1][j]);
ans = max(ans, checkMax(n, board));
swap(board[i][j], board[i + 1][j]);
}
}
}
return ans;
}
int main() {
int n;
cin >> n;
vector<vector<char>> board(n, vector<char>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
}
}
cout << swapCandy(n, board);
return 0;
}
'π Problem Solving' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ][C++] λ°±μ€ 1789λ²: μλ€μ ν© (0) | 2024.10.29 |
---|---|
[BOJ][C++] λ°±μ€ 15650λ²: Nκ³Ό M (2) (0) | 2024.10.28 |
[BOJ][C++] λ°±μ€ 15649λ²: Nκ³Ό M (1) (0) | 2024.10.28 |
[SWEA][C++] 1289. μμ¬μ λ©λͺ¨λ¦¬ 볡ꡬνκΈ° (0) | 2024.10.16 |
[BOJ][C++] λ°±μ€ 2002λ²: μΆμ (0) | 2024.06.14 |