leetCode130, like go game.
how to design the solution, at first it is naive to traverse with two for loops, then there will be too many if statement to specify, and not clear to seperate these if statements.
three ideas behind a sample solution.
using addtional status variable
in lower space the problem is difficult to analysis, then from a higher space, the problem is clear and easy. e.g. trakcing the O
in the board, how to mark them ? keep O
will not tell the marked ones and the original ones. so using M
to mark these already traversed, it make the situation much more clear
starting from the critial domain
at first, no idea where is a good start point, but the trick point is at the boundary, only the cells at boundary with O
will keep O
; all the O
not on the boundary either connect to the boundary O
s or they will be X
s.
traversing with queue
if not queue, to detect these O
cells need two for loops, then deal with these O
cells again need another two for loops.
for loop
is a global handling, while queue list
is a local way, no need to take all cells in consideration, but only the cells in queue.
queue should be a good way to traverse whenever the operation is not deal with all the elements.
|
|