1
+ #include < iostream>
2
+ #include < string>
3
+ using namespace std ;
4
+
5
+ /*
6
+ 상근이는 어렸을 적에 "봄보니 (Bomboni)" 게임을 즐겨했다.
7
+
8
+ 가장 처음에 N×N크기에 사탕을 채워 놓는다. 사탕의 색은 모두 같지 않을 수도 있다. 상근이는 사탕의 색이 다른 인접한 두 칸을 고른다. 그 다음 고른 칸에 들어있는 사탕을 서로 교환한다. 이제, 모두 같은 색으로 이루어져 있는 가장 긴 연속 부분(행 또는 열)을 고른 다음 그 사탕을 모두 먹는다.
9
+
10
+ 사탕이 채워진 상태가 주어졌을 때, 상근이가 먹을 수 있는 사탕의 최대 개수를 구하는 프로그램을 작성하시오.
11
+ */
12
+
13
+ int check_longest (char ** table, const int table_size) {
14
+ int longest_len = 0 ;
15
+ for (int i = 0 ; i < table_size; ++i) {
16
+ int current_len = 1 ;
17
+ char current_color = 0 ;
18
+ for (int j = 0 ; j < table_size; ++j) {
19
+ if (table[i][j] != current_color) {
20
+ longest_len = (longest_len < current_len) ? current_len : longest_len;
21
+ current_len = 1 ;
22
+ continue ;
23
+ }
24
+ ++current_len;
25
+ }
26
+ longest_len = (longest_len < current_len) ? current_len : longest_len;
27
+ }
28
+
29
+ for (int j = 0 ; j < table_size; ++j) {
30
+ int current_len = 1 ;
31
+ char current_color = 0 ;
32
+ for (int i = 0 ; i < table_size; ++i) {
33
+ if (table[i][j] != current_color) {
34
+ longest_len = (longest_len < current_len) ? current_len : longest_len;
35
+ current_len = 1 ;
36
+ continue ;
37
+ }
38
+ ++current_len;
39
+ }
40
+ longest_len = (longest_len < current_len) ? current_len : longest_len;
41
+ }
42
+
43
+ return longest_len;
44
+ }
45
+
46
+ void print_table (char ** table, int table_size) {
47
+ for (int i = 0 ; i < table_size; ++i) {
48
+ for (int j = 0 ; j < table_size; ++j)
49
+ cout << table[i][j] << ' ' ;
50
+ cout << ' \n ' ;
51
+ }
52
+ cout << ' \n ' ;
53
+ }
54
+
55
+ void print_current_loc (char ** table, int table_size, int i_loc, int j_loc) {
56
+ for (int i = 0 ; i < table_size; ++i) {
57
+ for (int j = 0 ; j < table_size; ++j)
58
+ if ( (i == i_loc) && (j == j_loc) )
59
+ cout << " * " ;
60
+ else
61
+ cout << table[i][j] << ' ' ;
62
+
63
+ cout << ' \n ' ;
64
+ }
65
+ cout << ' \n ' ;
66
+ }
67
+
68
+ int main () {
69
+ ios_base::sync_with_stdio (false );
70
+ cin.tie ();
71
+
72
+ int n, longest_len = 1 ;
73
+ cin >> n;
74
+ char ** table = new char *[n];
75
+
76
+ cout << " table created... at least\n " ;
77
+
78
+ for (int i = 0 ; i < n; ++i) {
79
+ table[i] = new char [n];
80
+ string temp_input;
81
+ cin >> temp_input;
82
+ for (int j = 0 ; j < n; ++j)
83
+ table[i][j] = temp_input[j];
84
+ // DEBUG
85
+ cout << " The input string is: " << temp_input << endl;
86
+ for (int j = 0 ; j < n; ++j) {
87
+ cout << table[i][j] << ' ' ;
88
+ }
89
+ cout << endl;
90
+ }
91
+
92
+ cout << " input read\n " ;
93
+
94
+ for (int i = 0 ; i < n; ++i) {
95
+ for (int j = 0 ; j < n; ++j) {
96
+ print_current_loc (table, n, i, j);
97
+ if (j != n-1 )
98
+ if (table[i][j] != table[i][j+1 ]) {
99
+ char temp = table[i][j];
100
+ table[i][j] = table[i][j+1 ];
101
+ table[i][j+1 ] = temp;
102
+ // DEBUG
103
+ print_table (table, n);
104
+ cout << " ------jchange------\n " ;
105
+ int max_len = check_longest (table, n);
106
+ // DEBUG
107
+ cout << max_len << ' \n ' ;
108
+ longest_len = (longest_len < max_len) ? max_len : longest_len;
109
+ temp = table[i][j];
110
+ table[i][j] = table[i][j+1 ];
111
+ table[i][j+1 ] = temp;
112
+
113
+ }
114
+ if (i != n-1 )
115
+ if (table[i][j] != table[i+1 ][j]) {
116
+ char temp = table[i][j];
117
+ table[i][j] = table[i+1 ][j];
118
+ table[i+1 ][j] = temp;
119
+ // DEBUG
120
+ print_table (table, n);
121
+ cout << " ------ichange------\n " ;
122
+ int max_len = check_longest (table, n);
123
+ // DEBUG
124
+ cout << max_len << ' \n ' ;
125
+ longest_len = (longest_len < max_len) ? max_len : longest_len;
126
+ temp = table[i][j];
127
+ table[i][j] = table[i+1 ][j];
128
+ table[i+1 ][j] = temp;
129
+
130
+ }
131
+ cout << " ====== Good ======\n " ;
132
+ }
133
+ }
134
+
135
+ cout << longest_len << ' \n ' ;
136
+
137
+
138
+ // deallocate memory
139
+ for (int i = 0 ; i < n; ++i) {
140
+ delete [] table[i];
141
+ table[i] = nullptr ;
142
+ }
143
+ delete [] table;
144
+ table = nullptr ;
145
+
146
+ return 0 ;
147
+ }
0 commit comments