Unguarded Cells: grid, hash and a linear approach
The problem this weekend asks for the number of cells that are not guarded. Since the limits of the grid are in the 10^5 range, we're then looking for a O(N) solution. First, it would be wise to add the positions of the guards and the walls in handy hash-tables. Relatively simple. After that, think this way: if you scan the grid in a left-to-right analyzing each row, you can increment the cells that are not reached by a guard in that row from left to right. Any cell that has a 1 at the end of this operation is free from guards in that row from left to right . Now apply the same approach in the other 3 directions: right to left, top down and bottoms-up. By doing so, at the very end, any cell that reaches the number 4 is free from any guard. And that's your answer. Thus, this would be a 4*m*n, and since m*n<=10^5 (as per the constraint), then the solution becomes O(4*10^5), which is very tractable and considered linear in this case. Code is down below, cheers, ACC. Count Ungua...