8x8칸에서 하얀칸으로 시작하는경우/ 검정칸으로 시작하는 경우
두개로 나누어 완전탐색 한다.
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 <algorithm> #include <string> #define SZ 8 using namespace std; int n, m; int map[50][50]; int solve(int x,int y, int flag) { int ans = 0; if (flag == 0) { for (int i = x; i < x + SZ; i++) { for (int j = y; j < y + SZ; j++) { if (i % 2 == 0) { if (j % 2 == 0 && map[i][j] == 'B') ans++; if (j % 2 == 1 && map[i][j] == 'W') ans++; } else { if (j % 2 == 1 && map[i][j] == 'B') ans++; if (j % 2 == 0 && map[i][j] == 'W') ans++; } } } } else { for (int i = x; i < x + SZ; i++) { for (int j = y; j < y + SZ; j++) { if (i % 2 == 0) { if (j % 2 == 0 && map[i][j] == 'W') ans++; if (j % 2 == 1 && map[i][j] == 'B') ans++; } else { if (j % 2 == 1 && map[i][j] == 'W') ans++; if (j % 2 == 0 && map[i][j] == 'B') ans++; } } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; string buf; for (int i = 0; i < n; i++) { cin >> buf; for (int j = 0; j < m; j++) { map[i][j] = buf[j]; } } int res = 987654321; for (int i = 0; i < n - SZ + 1; i++) { for (int j = 0; j < m - SZ + 1; j++) { int ans1 = solve(i, j, 0); int ans2 = solve(i, j, 1); res = min(res, min(ans1, ans2)); } } cout << res << endl; return 0; } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
1182 부분집합의 합 (4) | 2018.04.13 |
---|---|
9019 DSLR (0) | 2018.04.13 |
1520 내리막 길 (0) | 2018.04.08 |
1937 욕심쟁이 판다 (0) | 2018.04.08 |
14620 꽃길 (0) | 2018.04.05 |