Now, to solve this problem, you should run through boards, and maintain two sums:
/* Solution to Topcoder SRM 729, D2, 250-pointer.
BrokenChessboard
James S. Plank
Fri Sep 7 14:00:20 EDT 2018
*/
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class BrokenChessboard {
public:
int minimumFixes(vector <string> board);
};
int BrokenChessboard::minimumFixes(vector <string> board)
{
int r, c;
int SW, SB;
SW = 0;
SB = 0;
/* Loop through every square of the checkerboard. */
for (r = 0; r < board.size(); r++) {
for (c = 0; c < board[r].size(); c++) {
/* These squares will match the (0,0) square.
So, if they are 'B', you need to add one to SW.
Otherwise, you add one to SB. */
if ((r + c) %2 == 0) {
if (board[r][c] == 'B') SW++; else SB++;
/* These squares will match the (0,1) square.
So, if they are 'B', you need to add one to SB.
Otherwise, you add one to SW. */
} else {
if (board[r][c] == 'W') SW++; else SB++;
}
}
}
/* Return the smaller of the two sums. */
return (SW < SB) ? SW : SB;
}
|