787C: Berzerk
The kernel of this problem is to handle the unvisited state
Why the DFS approach does not work?!!!
We may have a case where we visit an ancestor when we have no conclusion on it yet, and thus we will conclude the state as LOOP
prematurely
In other words, a DFS suggests that when we visit that ancestor again, there is no conclusion on that ancestor yet, and therefore, we can
not form any conclusion on the child node we are on yet --> A deadlock here because by DFS, the ancestor can do nothing either!
Concretely, in our DFS we have an implicit ancetor -> child order in the final steps already, but we can not rule out child -> ancetor
possiblity in a winning play. That is why DFS will terminate prematurely!
Then how do we break the premature loop? => Don’t form it in the first place! We start from the blackhole, which has edges from only 1 side
Insight
1. We can use an event-driven approach, similar to 781B
2. If the current state is losing, then mark all neighbors as winning, and expand on those neighbor
s. Compared with DFS which builds on unknowns, we expand on known states,
3. If the current state is winning, then all 1 bad count to all neighbors, if any neighbor is full of bad notes, we conclude it is in a bad
state, and enter that node
4. Use arrays instead of separate variables to make coding easier
5. When calculating the previous pos we from from, need to exclude 1, which is invalid case