Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5701 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 938 |

@bt8816103042 | 739 |

@SHOAIBVIJAPURE | 428 |

@codeantik | 412 |

@SherlockHolmes3 | 407 |

@neverevergiveup | 348 |

@mahlawatep | 347 |

@shalinibhataniya1097 | 343 |

@murarry3625 | 333 |

@saiujwal13083 | 326 |

@rohitanand | 314 |

Complete Leaderboard |

A Maze is given as **N*N** binary matrix of blocks where source block is the upper left most block i.e., **maze[0][0]** and destination block is lower rightmost block i.e., **maze[N-1][N-1]**. A rat starts from source and has to reach the destination. The rat can move in only two directions: first forward if possible or down. If multiple solutions exist, the shortest earliest hop will be accepted. For the same hop distance at any point, forward will be preferred over downward.

In the maze matrix, 0 means the block is the dead end and 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 number of maximum jumps rat can make from cell mat[i][j].

In this variation, Rat is allowed to jump multiple steps at a time instead of 1.

**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 the square matrix followed by N*N space-separated values of the matrix m where 0's represents blocked paths and any number represents valid paths.

**Output:**

For each test case, the output is a matrix containing 1 for the path taken and 0 for not chosen path. If no path exists print **-1**.

**Constraints:**

1 <= T <= 50

2 <= n <= 10

0 <= m[i][j] <= 5

**Example:
Input**

2

4

2 1 0 0

3 0 0 1

0 1 0 1

0 0 0 1

4

2 1 0 0

2 0 0 1

0 1 0 1

0 0 0 1

**Output**:

1 0 0 0

1 0 0 1

0 0 0 1

0 0 0 1

-1

**Explanation:
Testcase 1: **Rat started 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 taking 1 jump each time to reach destination at m[3][3].

**Testcase 2: **As no path exists, so -1.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Rat Maze With Multiple Jumps

...