You are on page 1of 1

Prince and Princess

Prince has to save princess. She is in the cell (a,b) of a rectangular grid of size
(m,n). He starts from (1,1) and moves to the princess moving right, left , down or
up. When he enters some cell he has to kill all the guards in the cell before
leaving the cell. This killing takes some amount of time depending on the cell. Let
us say, it takes c[i,j] amount of time to do so. Print minimum time required for
prince to save princess
Constraints:
1 <= m,n <= 200
Input Format:
First line contains an integer t, representing number of test cases. Each test case
contains four integers
M, N, x, y where M and N are dimensions of matrix and x, y is position of princess.
Output Format:
Output Solution for each test case.
Sample Input:

1
6 4
3 2
0 21 57 0
47 71 76 0
18 1 3 0
33 0 59 0
6 97 20 0
8 34 98 0

Sample Output:

66

Explaination:

In this case, For prince to reach (3,2) from (1,1), he will go through cells
(1,1)->(2,1)->(3,1)->(3,2). The time required will be 47+18+1 = 66.

Time Limit(s):
6
Memory Limit(MBs):
1000

You might also like