find the first '?' in A
set it to '0' if we can, otherwise set it to '1'
repeat until no '?''s are left in A
The tricky part is how do we check whether we can set a ? to 0. Suppose you have set some ? to 0 in A. How do you check that A is still compatible with B? Think about the columns in A and in B. They should match. This is a matching problem! We are matching columns of A to columns of B. And if we have a matching subroutine pre-coded, we can get a lot of points for this 450-point problem.
Code follows. LIB_getMatchingSize is my library function that returns the maximum matching size.
vector<string> rows, cols;
int r,c;
bool works() {
vector<vector< int> > adj(c, vector<int>(c,0));
for (int i = 0; i < c; i++) {
for (int j = 0; j < c; j++) {
int good = 1;
for (int k = 0; k < r; k++) {
if (rows[k][i] != '?' && cols[j][k] != '?' && rows[k][i] != cols[j][k])
good = 0;
}
adj[i][j] = good;
}
}
return LIB_getMatchingSize(adj)==c;
}
struct P8XMatrixRecovery {
vector<string> solve(vector<string> _rows, vector<string> _cols) {
rows = _rows;
cols = _cols;
r = rows.size();
c = cols.size();
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (rows[i][j] == '?') {
rows[i][j] = '0';
if (!works())
rows[i][j] = '1';
}
}
}
return rows;
}
};