ooo. oooo .... oooo ooo. |
0001 0000 2222 3333 3334 |
There are two white components and three empty components. Let's number them, as on the right above. We know a few things:
ooo.. oooo. ....o oooo. ooo.. |
00011 00001 22225 33334 33344 |
Now, components 1 and 4 are both safe with respect to the other components. Component 2 is interesting -- it is unsafe with respect to components 0 and 3; however it is safe with respect to component 5.
What does safety have to do with this problem? Well, this:
Let's think about that with respect to component 0 above. It is adjacent to components 1 (safe) and 2 (unsafe). You surround it with two tokens from component 0, which is fine because it doesn't "fill up" the component. Then you surround it with the four tokens from component 2. Although it fills up component two, it kills component 0 first, which renders component 2 safe.
.ooo. oooo. ....o oooo. |
01112 11112 33334 55556 |
Now, all empty components are unsafe with respect to white components 1 and 5, so you can't kill them. Component 6 is unsafe with respect to component 4. However, since component 4 is only adjacent to one unsafe component, you can kill it by placing three black tokens on the board, putting the token into component 6 last. Doing this removes the white token from the board, and now those three components (2, 3 and 6) are safe with respect to all of the other tokens. That's nice because we can now kill components 5 and 1.
I hope that gives you a clue about how to solve this problem:
class Cell { public: vector <Cell *> adj; // Adjacency list (easier than using row/column) class Component *comp; // My component int row; int col; int state; // '.', 'o' int number; // Each cell has a unique number - useful for computing component adjacency }; class Component { public: int type; // '.' or 'o' int number; // Unique across components vector <Cell *> cells; // All of the cells in the component map <int, Component *> adj; // Components to which I'm adjacenct. Keyed by component number. map <int, int> adjcount; // Keyed by component number - # of cells that we have adjacent. set <int> unsafe_neighbors; // Only for white components. Again, keyed on component number. }; class FoxAndGo2 { public: int maxKill(vector <string> board); void DFS(Cell *c, Cell *start); // For determining components vector <string> Board; // The board vector < vector <Cell *> > Cells; // The cells in a grid vector <Cell *> AllCells; // All of the cells in a vector vector < Component * > White_Components; // Just the white components. vector < Component * > Empty_Components; // Just the empty components. vector < Component * > Components; // All components deque < Component * > Killable; // The killable components list. int Rows; int Cols; }; |
Plus, I have annotated the output of my program; you can find outputs for each of the examples in the following links (abbreviations: WC = "White Component", UN = "Unsafe neighbor").