# Mouse and Maze (Part 2)

Problem:
Write a program that will input a 2 D array from the user. The array will be the structure of a maze. The 1s in the array will represent the path where the mouse can move and  0s in the array will represent walls. There is a big piece of cheese in the maze represented by a 9 in the array. The mouse will start moving from the corner (0,0) of the array. The program should tell that the mouse can reach the piece of cheese or not. The program should also tell the path using which the mouse can reach the cheese. It is made clear in the example below:

Example 1:
1100
0111
0010
0090
The mouse can reach 9 in the above array.
Path:
Right
Down
Right
Down
Down

Example 2:
1000
1110
0000
9111
The mouse can’t reach 9 in the above array

Code:

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86``` ``` import java.util.Scanner; public class MouseAndMaze { public static String resultant=""; public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter the number of rows of array: "); int n=input.nextInt(); System.out.print("Enter the number of columns of array: "); int m=input.nextInt(); System.out.println("Enter the array:"); int are[][]=new int[n][m]; for (int t=0;t0) { u=maze(arr,x,(y-1)); if (u!=0) { resultant="Left"+"n"+resultant; return 1; } } if (y0) { l=maze(arr,x-1,y); if (l!=0) { resultant="Up"+"n"+resultant; return 1; } } return 0; } } ```

Output:

```Enter the number of rows of array: 20
Enter the number of columns of array: 20
Enter the array:
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0
0 1 1 1 1 1 1 0 1 1 0 0 0 1 1 1 1 1 0 0
0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0 0 1 0 1 1 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 1 0 0
0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0
1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0

The mouse can reach the cheese by following the path below:
Right
Right
Right
Right
Right
Right
Right
Right
Down
Down
Down
Down
Right
Right
Right
Right
Right
Right
Down
Down
Down
Down
Down
Down
Left
Down
Down
Down
Down
Right
Right
Right
Down
Down
Down
Right
Right
Down
Down
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Left
Reached the cheese
```

```Enter the number of rows of array: 20
Enter the number of columns of array: 20
Enter the array:
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0
0 1 1 1 1 1 1 0 1 1 0 0 0 1 1 1 1 1 0 0
0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0 0 1 0 1 1 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 1 0 0
0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0
1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0

The mouse can not reach the cheese.
```

Explanation: