백준 #3085 사탕 게임
서로 다른 인접한 사탕의 두 위치를 바꿔 가장 긴 행 또는 열의 길이를 구하는 구현 문제.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int N;
char board[50][50];
int dx[2] = {0, 1};
int dy[2] = {1, 0};
bool isInBoard(int x, int y) {
return x >= 0 && x < N && y >= 0 && y < N;
}
int countLong() {
int ans = 0;
int rowCount = 0;
int colCount = 0;
for(int i = 0; i < N; i++) {
rowCount = 1;
colCount = 1;
for(int j = 1; j < N; j++) {
if(board[i][j] == board[i][j - 1]) {
rowCount++;
}
else {
rowCount = 1;
}
if(board[j][i] == board[j - 1][i]) {
colCount++;
}
else {
colCount = 1;
}
ans = max({ans, rowCount, colCount});
}
}
return ans;
}
int main() {
scanf("%d", &N);
getchar();
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
board[i][j] = getchar();
}
getchar();
}
int maxLen = 0;
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
for(int k = 0; k < 2; k++) {
int nX = i + dx[k];
int nY = j + dy[k];
if(isInBoard(nX, nY) && board[i][j] != board[nX][nY]) {
swap(board[i][j], board[nX][nY]);
maxLen = max(maxLen, countLong());
swap(board[i][j], board[nX][nY]);
}
}
}
}
printf("%d", maxLen);
return 0;
}
This post is licensed under CC BY 4.0 by the author.