Fortify
You’re in an arena consisting of N ×M cells shaped in the form of a N ×M grid. The cell located on the i
th row
from the top and the j
th column from the left is represented by the pair (i, j). You’re currently standing in the
cell (N, M) and spot a monster currently on cell (1, 1). Thankfully, you’ve got fortification spells to save yourself.
The monster can move into any of the cells adjacent to the one it’s currently on, any number of times. To stop
it, you may select some cells to fortify them. Note that the selected cells can be any valid subset of the grid,
and so, need not be adjacent to one another. The monster cannot enter a fortified cell, nor can it leave one if
it happens to be already on a fortified cell.
However, some cells are harder to fortify than others. Formally, it costs Ai,j energy to fortify the cell (i, j). So,
the total energy you’d need to spend is a sum over the costs for all the cells you pick. Note that once a cell is
fortified, it stays fortified forever.
Find the minimum total energy you’d have to spend in order to ensure that the monster does not get to
you.
Note that two cells (a, b) and (x, y) are adjacent iff |a − x| + |b − y| = 1. Note that fortifying the cell (N, M)
is a valid solution since it stops the monster from entering the cell. Similarly, fortifying the cell (1, 1) is also
always a valid solution since the monster can now never leave its starting cell. But do note that either of these
may not be the optimal approach to stopping the monster with respect to the cost incurred.
4.1 Constraints
• All input values are integers
• 2 ≤ N × M ≤ 2 × 105
• 1 ≤ N, M ≤ 2 × 105
• 1 ≤ Ai,j ≤ 103
for all 1 ≤ i ≤ N, 1 ≤ j ≤ M
• 1 ≤ T ≤ 100
• Sum over N × M across all test-cases does not exceed 106
4.2 Input Format
The first line consists of a single integer T that denotes the number of test-cases. Then, the description of each
test-case follows.
The first line of each test-case consists of two space-separated integers, N and M, denoting the size of the arena.
Then, N lines follow describing the costs for each row.
The i
th line (1 ≤ i ≤ N) consists of M space-separated integers Ai,1, Ai,2, . . . , Ai,M denoting the costs of cells
on the i
th row of the arena.
4.3 Output Format
For each test-case, output on a single line, the minimum total energy you’d have to spend in order to ensure
that the monster does not get to you. It can be shown that it is always possible to do so.
4.4 Sample Test Cases
Input Output
1
2 2
7 4
4 7
7
8
Input Output
1
3 3
10 1 5
11 3 1
2 7 6
6
4.5 Explanation of samples test-cases
Test-case 1: In the first case, it is optimal to fortify the cell (1, 1) incurring a cost of 7. It can be shown that
any other approach of fortifying incurs a larger cost.
Note that fortifying the cell (1, 1) which is the starting cell is sufficient to stop the monster since the monster
cannot enter a fortified cell, nor can it leave one if it happens to be already one a fortified cell.
Test-case 2: It can be shown that the optimal plan of fortification is to fortify the cells (2, 2),(2, 3),(3, 1)
incurring a total cost of 6. Alternatively, one could also incur the same cost by fortifying the cell (3, 3). Either
of these solutions are optimal.
c program provide