Professional Documents
Culture Documents
A Maze is given as an N*N binary matrix of blocks. Where source block is the upper leftmost
block. Which is a maze[0][0], and the destination block is a lower rightmost block. That is
a maze[N-1][N-1]. A rat starts from the source and has to reach the destination. The rat can
move only in two directions: if possible, the first is forward or else down. If multiple
solutions exist, the shortest and earliest hop will be accepted. For the same hopping
distance at any point, forward will be preferred over downward.
In the maze matrix, 0 means the block is dead-end. And a non-zero number means the
block can be used in the path from source to destination. The non-zero value
of mat[i][j] indicates, the number of maximum jumps rat can make from cell mat[i][j].
Format:
Input:
The first line of input contains an integer T, denoting the number of test cases. For each test
case, the first line contains an integer N, denoting the size of a square matrix N*N. The next
line is the space-separated values of the matrix M, where 0's represent blocked paths and
any other number represents valid paths.
Output:
For each test case, the output is a matrix that contains 1 for the path and 0 for no path
taken. If no path exists in a matrix, print -1.
Constraints:
1 <= T <= 50
2 <= n <= 10
Example:
Input:
2
4
2100
3001
0101
0001
2100
2001
0101
0001
Output:
1000
1001
0001
0001
-1
Explanation:
Testcase 1: Rat will start with m[0][0] and can jump up to 2 steps right/down. First check
m[0][1] as it is 1, next check m[0][2], this won't lead to the solution. Then check m[1][0], as
this is 3(non-zero). So we can make 3 jumps to reach m[1][3]. From m[1][3] we can move
downwards, by taking 1 jump each at a time to reach the destination at m[3][3].
2. Policemen - Thieves
Given an array of size N. Which has the following specifications: Each element in the array
contains either a policeman or a thief. Each policeman can catch only one thief. A policeman
cannot catch a thief who is more than K units away from him. We need to find the
maximum number of thieves that can be caught.
Example:
Input:
Output:
Input :
6
T
T
P
P
T
P
2
Output: