A simple simulation problem, but be careful that one could solve 0, 1, or 2 problems.
Once a square becomes black, we check all the 3 * 3 areas (in fact there 9 such areas) which contain it, to see whether they are all black. The complexity is O(M).
A straightforward greedy algorithm solves this problem.
We can consider each of the three dimensions independently. At first, we can calculate when it hits the door according to vy. Then, we calculate the total distance that it goes along the x and y dimension, respectively, assuming that no reflection occurs. It can be observed that for x dimension, the distance has a cycle of length 2a while for y dimension, the cycle has length 2b. Therefore, we “eliminate” a multiple of cycles from the total distance and it is then simple to determine the final position, according to the “residual” distance.