fork download
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. const char b[8][9] = {
  6. {"BWBWBWBW"},
  7. {"WBWBWBWB"},
  8. {"BWBWBWBW"},
  9. {"WBWBWBWB"},
  10. {"BWBWBWBW"},
  11. {"WBWBWBWB"},
  12. {"BWBWBWBW"},
  13. {"WBWBWBWB"}
  14. };
  15. const char w[8][9] = {
  16. {"WBWBWBWB"},
  17. {"BWBWBWBW"},
  18. {"WBWBWBWB"},
  19. {"BWBWBWBW"},
  20. {"WBWBWBWB"},
  21. {"BWBWBWBW"},
  22. {"WBWBWBWB"},
  23. {"BWBWBWBW"}
  24. };
  25.  
  26. int black(int x, int y);
  27. int white(int x, int y);
  28. char board[50][50];
  29.  
  30. int main()
  31. {
  32. int row, columns, result = 123456;
  33. cin >> row >> columns;
  34.  
  35. for (int i = 0; i < row; i++)
  36. cin >> board[i];
  37.  
  38. for (int i = 0; i < row - 7; i++)
  39. {
  40. for (int j = 0; j < columns - 7; j++)
  41. {
  42. result = min(result, min(white(i, j), black(i, j)));
  43. }
  44. }
  45. cout << result << endl;
  46. return 0;
  47. }
  48.  
  49. int black(int x, int y)
  50. {
  51. int cnt = 0;
  52. for (int i = 0; i < 8; i++)
  53. {
  54. for (int j = 0; j < 8; j++)
  55. {
  56. if (board[x + i][y + j] != b[i][j])
  57. cnt++;
  58. }
  59. }
  60. return cnt;
  61. }
  62.  
  63. int white(int x, int y)
  64. {
  65. int cnt = 0;
  66. for (int i = 0; i < 8; i++)
  67. {
  68. for (int j = 0; j < 8; j++)
  69. {
  70. if (board[x + i][y + j] != w[i][j])
  71. cnt++;
  72. }
  73. }
  74. return cnt;
  75. }
Success #stdin #stdout 0s 4964KB
stdin
10 13
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
WWWWWWWWWWBWB
WWWWWWWWWWBWB
stdout
12