Editorial for Feed-A-Mole


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: kahootist

This problem wasn't particularly hard to implement but was harder to get within the time limits. This required an efficient approach which didn't simulate all possible hand-paths.

Let's draw, at each timestamp, the number of moles that could have been fed from this timestamp onwards from any particular position, using the same states as the sample input:

X X X t = 1
X X X
-----
O X X t = 2
X X X
-----
X X X t = 3
O X X
-----
X X O t = 4
X X O
-----
X X O t = 5
X X X

For t = 5, this is rather simple - just 1 if an O is present, and 0 if an X is present:

0 0 1 t = 5
0 0 0

For t = 4, this gets more complicated. Both of the right lanes can get moles at both t=4 and t=5, while the top-middle position is still close enough to get a single mole at the last second:

0 1 2 t = 4
0 0 2

For t = 3, this is getting harder to compute. But we can be smart in computing the values. Rather than trying all of the hand-paths from t=3 to t=5, we can instead use our result for t=4 as a sort of memory of the best we can do from this timestamp onwards. For example, let's look at the top-left corner for t=3. This isn't sitting on an O, so the only moles can be collected from t=4 onwards. We have the decision to move to the top-left, top-middle, or bottom-left positions, which from our previous workings out, net the player 0, 1 and 0 moles respectively, assuming they make the best choices. So the value for t=3 at the top-left corner is 1 (And this only required us to read 3 values from the previous solution!)

Let's have another look, say for t=3 at the bottom-middle position. From this position, we can move to bottom-middle, bottom-left, top-middle and bottom-right for t=4. This nets the player 0, 0, 1 and 2 moles at t=4 respectively. So the best option for the player at t=3 in the bottom-middle spot is to move to the bottom-right at t=4.

The only extra thing we need to do is that if the position we start at has a O, then we can do 1 mole better than what looking at the next timestamp would give us.

We can use this to compute all of the values for t = 3:

1 2 2 t = 3
1 2 2

And again for t = 2:

3 2 2
2 2 2

And finally for t = 1:

3 3 2
3 2 2

So the maximum that we can get starting at t=1 is 3, which we can achieve by starting at any of the 3 positions near the top-left.

Here's some pseudo-code to get you started:

# Calculating the moles you can get at time t at position x, y
for newx, newy in possible displacements:
    option = moles we can get at time t+1 at position newx, newy
set score = find max(options)
if moles at time t in position x, y is O:
    score += 1
return score

Comments

There are no comments at the moment.