Note that in order for Mario to escape the top left 2x2 grid, the minimum amount of damage he will have to take is 5, by taking the top wall, also we by maximizing the number of turns he is on the right or bottom wall we can increase marios health, which would produce a lower required initial heatlh, since both the right and bottom wall can increase marios health by 4, then we simply choose the initial exit path from the top left 2x2 square which minimizes the damage, therefore the path RRDD is the best possible path.
2
We want to figure out the minimal amount of helath mario needs to get to bowser in general, one way of thinking about this is that as mario traverses the grid, his health will change based on the path he's taken so far. For example in the example grid , then mario intially takes damage, by moving to the right he's taken total damage, then by moving right again, he gains health so that the total health change is , by moving down once the health change becomes finally landing on the the final square yields a health change of .
Looking at the above sequence , we can see that as he moves through this path he's going to need to have at least health to survive this journey. Note that it's not enough to simply look at the numbers on the grid, as then we might come to believe we would only need health to survive the last block, but we need to keep track of how much health we've gained and lost as we move.
At the same time, mario could have taken a different path to get to the goal state, for example if he went down twice and then right twice we would have a health change sequence as follows which would say that mario needs at least health to get to the end state. There would be other paths to get to the goal state, but in general we want to know the minimum health mario would need to get to the goal state, so out of all the paths that could get us there, we only care about the one that requires mario having minimal health.
Before continuing we'll make some definitions to make speaking about this problem easier. If mario takes some path on the map, it generates a "health change sequence", as discussed in the previous paragraph, note that the last term in the sequence is the total health change obtained by getting to the current square, let's denote this as the health change amount (HCA) of a given square. We'll denote the original grid taken as input as the (AHC) absolute health change grid
If we have some health change amount (HCA) , then the larger it is, the better it is for mario because it would mean that you lose less health. Also at any given position in the map, there may be multiple ways to get there, each sequence yielding their own HCA, by taking the maximum of these paths, we can determine the path which gives mario the highest HCA at this square.
Let's try constructing a maximal HCA grid, given any position on the AHC grid, suppose we know the max HCA value for and , denoted by respectively then the max HCA value for would be given by
Where is the value at that particular cell, note that it must be included because you took a path and ended up at this square, meaning you have to change your health by the definition of the game.
Since we subtracted one from the or component of without considering if the resulting position is within the grid, we can correct for this by noting that if we're on say the left wall, we'd only have to look up, to compute our MHCA value, that is:
MHCA[i][j] =
- if i == j == 0
- elif i == 0 && j > 0
- MHCA[i][j - 1] + AHC[i][j] (on left wall, look up)
- elif j == 0 && i > 0
- MHCA[i - 1][j] + AHC[i][j] (on top wall, look left)
- else
- max(MHCA[i - 1][j], MHCA[i][j - 1]) + AHC[i][j]
Now that we have this new MHCA grid constructed, it doesn't straight away give us the minimum amount of health mario will need to survive the journey, any given square simply tells us the maximum change in health mario could have at this position. For example suppose we have the following grid.
The top right element of the MHCA grid says that the maximum health change mario could get at this grid position is which means that any path that takes mario to this position, will give a net result of mario losing at least health. Therefore we can deduce that the path would require mario to have at least health, whereas would only require mario to have health.
Let's use the MHCA grid to compute marios minimum required health. We'll do this by constructing one more grid such that a position on the grid is the minimum amount of health required for mario to make it to this grid square. We'll denote this grid by the "MHR grid" (min health required). Given the MHCA grid that we most recently discussed, we'll note that it's MHR grid is as follows:
Which was obtained by taking any position in the MCHA grid and taking the max of left and up denoting that value by and then taking the min of and the MCHA value at that square. Specifically each square is computed as follows
MHR[i][j] =
- if i == j == 0
- elif i == 0 && j > 0
- min(MHCA[i][j - 1], MHCA[i][j]) (on left wall, look up)
- elif j == 0 && i > 0
- min(MHCA[i - 1][j] + MHCA[i][j]) (on top wall, look left)
- else
- min(MHCA[i - 1][j], MHCA[i][j - 1], MHCA[i][j])
Now once that entire grid has been computed to find the minimum health required by mario we simply just look at MHR[N][M], which is our solution.
3
In order to compute the MHCA grid, we take a bottom up approach only computing each element of the grid once which will take time, we only need a single row or column to produce the next, and therefore we can reduce our space complexity to by selecting the smaller dimension, and removing previous rows from memory.
The same analysis holds true for the MHR grid, thus, the runtime and space complexity does not change as this would only add a constant to the front of each.