Editorial for SAC '22 Code Challenge 2 P4 - Cookie Galore


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: maxcruickshanks

First, realize that this problem asks for the shortest path from (1, 1) to (N, M).

Next, decompose the grid of characters into a graph:

  • If the next position you are travelling to is a . cell, the weight is 0.

  • If the next position you are travelling to is a C cell, the weight is 1.

Now, run a shortest path algorithm (e.g., Dijkstra's algorithm) to find the shortest path.

Time Complexity: \mathcal{O}(NM\log(NM))

Note that a faster time complexity can be achieved by further realizing that the weights are either 0 or 1, allowing you to use 0-1 BFS.

Also, the bounds for N and M are relatively low because the author believes that distinguishing between a log factor (e.g., Dijkstra's algorithm vs. 0-1 BFS) is unfair to competitors.


Comments

There are no comments at the moment.