-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaekjoon_1285.cpp
54 lines (43 loc) · 942 Bytes
/
baekjoon_1285.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
#include <iostream>
#include <climits>
using namespace std;
int N;
int _map[20];
char input;
int ans = INT_MAX;
void reverseRow(int* arr, int row) {
arr[row] = (~arr[row] & ((1 << N) - 1));
}
int getTCount(int* arr) {
int sum = 0;
for(int x = 0; x < N; x++) {
int cnt = 0;
for(int y = 0; y < N; y++) {
if(arr[y] & (1 << x)) cnt ++;
}
sum += min(cnt, N - cnt);
}
return sum;
}
void dfs(int row) {
if (row >= N) return;
int tCnt = getTCount(_map);
ans = min(ans, tCnt);
for(int r = row; r < N; r++) {
reverseRow(_map, r);
dfs(r + 1);
reverseRow(_map, r);
}
}
int main() {
scanf("%d ", &N);
for(int y = 0; y < N; y++) {
for(int x = 0; x < N; x++) {
scanf("%1c ", &input);
if(input == 'T') _map[y] = _map[y] | (1 << x);
}
}
dfs(0);
printf("%d\n", ans);
return 0;
}