/* Topcoder SRM 700, D2, 500-Pointer (XMarksTheSpot) * James S. Plank * Tue Oct 25 15:18:18 EDT 2016 */ #include #include #include #include #include #include #include #include #include #include using namespace std; class XMarksTheSpot { public: int countArea(vector board); }; int XMarksTheSpot::countArea(vector B) { int i, j; vector QR, QC; // Row/columns of the question marks. int minrow; // Minimum row for the monuments ('O') int maxrow; // Maximum row for the monuments ('O') int mincol; // Minimum col for the monuments ('O') int maxcol; // Maximum col for the monuments ('O') int rv; // Return value. /* During the enumeration, you need to calculate the min/max row/column when you consider each subset. I use r, R, c and C to do that calculation. */ int r, R, c, C; /* Sentinelize the min/max row/col. */ minrow = B.size(); maxrow = -1; mincol = B[0].size(); maxcol = -1; /* Here's where we create QR and QC, and also calculate the min/max row/col */ for (i = 0; i < B.size(); i++) { for (j = 0; j < B[i].size(); j++) { if (B[i][j] == '?') { QR.push_back(i); QC.push_back(j); } else if (B[i][j] == 'O') { if (i < minrow) minrow = i; if (i > maxrow) maxrow = i; if (j < mincol) mincol = j; if (j > maxcol) maxcol = j; } } } /* The constraints tell you that there is always at least one 'O', so you don't have to check whether minrow still equals B.size() or maxrow still equals -1. */ rv = 0; printf("\n"); /* Do the power set enumeration of all subsets of question marks */ for (i = 0; i < (1 << QR.size()); i++) { /* You need to calculate the min/max row/column which includes the subset of question marks. Start with the min/max row/column of the known monuments. */ r = minrow; R = maxrow; c = mincol; C = maxcol; /* For each subset, run through and modify r/R/c/C if the question mark changes it. */ for (j = 0; j < QR.size(); j++) { if (i & (1 << j)) { if (QR[j] < r) r = QR[j]; if (QR[j] > R) R = QR[j]; if (QC[j] < c) c = QC[j]; if (QC[j] > C) C = QC[j]; } } /* Add the rectangle size to the return value. */ rv += ( (R-r+1)*(C-c+1) ); /* I'm printing the HTML of that table. */ printf("\n"); printf("\n"); printf("\n", i); printf("\n", r); printf("\n", R); printf("\n", c); printf("\n", C); printf("\n", ( (R-r+1)*(C-c+1) )); printf("\n", rv); printf("\n"); } printf("
{"); for (j = 0; j < QR.size(); j++) { if (i & (1 << j)) printf(" %d", j); } printf(" }0x%x%d%d%d%d%d%d
\n"); /* Return the final value. */ return rv; }