[BOJ] 3085 사탕 게임

Time Lapse :NONE

3085.cpp

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

char arr[51][51];
int A_size;
int answer = 0;

void swap(char *px, char *py)
{
int temp = *px;
*px = *py;
*py = temp;
}

void calc(int x, int y)
{
int cnt = 0;
int x1, y1;
x1 = x + 1; y1 = y;
while (1)
{
if (x1 < A_size && arr[x][y] == arr[x1][y])
{
x1++;
cnt++;
}
else
break;
}
x1 = x - 1; y1 = y;
while (1)
{
if (x1 >= 0 && arr[x][y] == arr[x1][y])
{
x1--;
cnt++;
}
else
break;
}
answer = max(answer, cnt + 1);

x1 = x; y1 = y + 1; cnt = 0;
while (1)
{
if (y1 < A_size && arr[x][y] == arr[x][y1])
{
y1++;
cnt++;
}
else
break;
}
x1 = x; y1 = y - 1;
while (1)
{
if (y1 >= 0 && arr[x][y] == arr[x][y1])
{
y1--;
cnt++;
}
else
break;
}
answer = max(answer, cnt + 1);
}

void answer_func()
{
for (int i = 0; i < A_size; i++)
{
for (int j = 0; j < A_size; j++)
{
if (i < A_size - 1 && (arr[i][j] != arr[i + 1][j]))
{
swap(&arr[i][j], &arr[i + 1][j]);
for (int x = 0; x < A_size; x++)
{
for (int y = 0; y < A_size; y++)
{
calc(x, y);
}
}
swap(&arr[i][j], &arr[i + 1][j]);
}
if (j < A_size - 1 && (arr[i][j] != arr[i][j + 1]))
{
swap(&arr[i][j], &arr[i][j + 1]);
for (int x = 0; x < A_size; x++)
{
for (int y = 0; y < A_size; y++)
{
calc(x, y);
}
}
swap(&arr[i][j], &arr[i][j + 1]);
}
}
}
}
int main(void)
{
cin >> A_size;

for (int i = 0; i < A_size; i++)
{
for (int j = 0; j < A_size; j++)
{
cin >> arr[i][j];
}
}
answer_func();
cout << answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/3085/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.