C. Chessboard( Educational Codeforces Round 41 (Rated for Div. 2))
1 //暴力 2 #include <iostream> 3 #include <algorithm> 4 #include <string> 5 6 using namespace std; 7 const int N = 105; 8 string s1[N], s2[N], s3[N], s4[N]; 9 int a[N][N], b[N][N]; 10 11 int main() 12 { 13 int n; 14 cin >> n; 15 for (int i = 0; i<n; i++) 16 cin >> s1[i]; 17 cin.get(); 18 for (int i = 0; i<n; i++) 19 cin >> s2[i]; 20 cin.get(); 21 for (int i = 0; i<n; i++) 22 cin >> s3[i]; 23 cin.get(); 24 for (int i = 0; i<n; i++) 25 cin >> s4[i]; 26 27 for (int i = 0; i<n; i++) 28 for (int j = 0; j < n; j++){ 29 //只有两种方式 30 a[i][j] = (i + j) % 2 == 0 ? 1 : 0; 31 b[i][j] = (i + j) % 2 ? 1 : 0; 32 } 33 for (int i = 0; i<n; i++) 34 for (int j = 0; j<n; j++) 35 a[i][j] += 48, b[i][j] += 48; 36 37 //比较 38 int ans = 1e9; 39 int f = 0; 40 for (int i = 0; i<n; i++) 41 for (int j = 0; j<n; j++) 42 f += (s1[i][j] != a[i][j]) + (s2[i][j] != a[i][j]) + (s3[i][j] != b[i][j]) + (s4[i][j] != b[i][j]); 43 ans = min(ans, f); 44 f = 0; 45 for (int i = 0; i<n; i++) 46 for (int j = 0; j<n; j++) 47 f += (s1[i][j] != a[i][j]) + (s2[i][j] != b[i][j]) + (s3[i][j] != a[i][j]) + (s4[i][j] != b[i][j]); 48 ans = min(ans, f); 49 f = 0; 50 for (int i = 0; i<n; i++) 51 for (int j = 0; j<n; j++) 52 f += (s1[i][j] != a[i][j]) + (s2[i][j] != b[i][j]) + (s3[i][j] != b[i][j]) + (s4[i][j] != a[i][j]); 53 ans = min(ans, f); 54 f = 0; 55 for (int i = 0; i<n; i++) 56 for (int j = 0; j<n; j++) 57 f += (s1[i][j] != b[i][j]) + (s2[i][j] != a[i][j]) + (s3[i][j] != a[i][j]) + (s4[i][j] != b[i][j]); 58 ans = min(ans, f); 59 f = 0; 60 for (int i = 0; i<n; i++) 61 for (int j = 0; j<n; j++) 62 f += (s1[i][j] != b[i][j]) + (s2[i][j] != a[i][j]) + (s3[i][j] != b[i][j]) + (s4[i][j] != a[i][j]); 63 ans = min(ans, f); 64 f = 0; 65 for (int i = 0; i<n; i++) 66 for (int j = 0; j<n; j++) 67 f += (s1[i][j] != b[i][j]) + (s2[i][j] != b[i][j]) + (s3[i][j] != a[i][j]) + (s4[i][j] != a[i][j]); 68 ans = min(ans, f); 69 cout<< ans <<endl; 70 system("pause");
71 return 0; 72 }