|
| 1 | +public class Assignment_Backtracking { |
| 2 | + public static void ratInMaze(int maze[][], int row, int col, int newMaze[][]) { |
| 3 | + // base case |
| 4 | + if (row == maze.length - 1 && col == maze.length - 1) { |
| 5 | + newMaze[row][col] = 1; |
| 6 | + printMaze(newMaze); |
| 7 | + return; |
| 8 | + } |
| 9 | + // recursion |
| 10 | + if (col < maze.length - 1 && maze[row][col + 1] == 1) { // right |
| 11 | + newMaze[row][col] = 1; |
| 12 | + ratInMaze(maze, row, col + 1, newMaze); |
| 13 | + newMaze[row][col] = 0; |
| 14 | + } |
| 15 | + if (row < maze.length - 1 && maze[row + 1][col] == 1) { // down |
| 16 | + newMaze[row][col] = 1; |
| 17 | + ratInMaze(maze, row + 1, col, newMaze); |
| 18 | + newMaze[row][col] = 0; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public static void printMaze(int maze[][]) { |
| 23 | + for (int i = 0; i < maze.length; i++) { |
| 24 | + for (int j = 0; j < maze.length; j++) { |
| 25 | + System.out.print(maze[i][j] + " "); |
| 26 | + } |
| 27 | + System.out.println(); |
| 28 | + } |
| 29 | + System.out.println(); |
| 30 | + } |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + // Question 1 : Rat in a Maze -------------------------------- |
| 34 | + // You are given a starting position for a rat which is stuck in a maze at an initial point (0, 0) (the |
| 35 | + // maze can be thought of as a 2-dimensional plane). The maze would be given in the form of a |
| 36 | + // square matrix of order N * N where the cells with value 0 represent the maze’s blocked |
| 37 | + // locations while value 1 is the open/available path that the rat can take to reach its destination. |
| 38 | + // The rat's destination is at (N - 1, N - 1). |
| 39 | + // Your task is to find all the possible paths that the rat can take to reach from source to |
| 40 | + // destination in the maze. |
| 41 | + // The possible directions that it can take to move in the maze are 'U'(up) i.e. (x, y - 1) , 'D'(down) |
| 42 | + // i.e. (x, y + 1) , 'L' (left) i.e. (x - 1, y), 'R' (right) i.e. (x + 1, y) |
| 43 | + // (This problem is similar to Grid ways.) |
| 44 | + int maze[][] = { |
| 45 | + { 1, 0, 0, 0 }, |
| 46 | + { 1, 1, 0, 1 }, |
| 47 | + { 0, 1, 0, 0 }, |
| 48 | + { 1, 1, 1, 1 } |
| 49 | + }; |
| 50 | + int newMaze[][] = new int[4][4]; |
| 51 | + ratInMaze(maze, 0, 0, newMaze); |
| 52 | + // Output = 1 0 0 0 |
| 53 | + // 1 1 0 0 |
| 54 | + // 0 1 0 0 |
| 55 | + // 0 1 1 1 |
| 56 | + |
| 57 | + // Question 2 : Keypad Combinations -------------------------------- |
| 58 | + // Given a string containing digits from 2-9 inclusive, print all possible letter combinations that |
| 59 | + // the number could represent. You can print the answer in any order. |
| 60 | + // A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 |
| 61 | + // does not map to any letters. |
| 62 | + String digits1 = "23"; // Output : "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" |
| 63 | + String digits2 = "2"; // Output : "a", "b", "c" |
| 64 | + String digits3 = ""; // Output : "" |
| 65 | + |
| 66 | + // Question 3 : Knight’s Tour -------------------------------- |
| 67 | + // Given a N*N board with the Knight placed on the first block of an empty board. Moving |
| 68 | + // according to the rules of chess, knights must visit each square exactly once. Print the order of |
| 69 | + // each cell in which they are visited. (Hint : Similar to N Queens) |
| 70 | + int N = 8; |
| 71 | + // Output = 0 59 38 33 30 17 8 63 |
| 72 | + // 37 34 31 60 9 62 29 16 |
| 73 | + // 58 1 36 39 32 27 18 7 |
| 74 | + // 35 48 41 26 61 10 15 28 |
| 75 | + // 42 57 2 49 40 23 6 19 |
| 76 | + // 47 50 45 54 25 20 11 14 |
| 77 | + // 56 43 52 3 22 13 24 5 |
| 78 | + // 51 46 55 44 53 4 21 12 |
| 79 | + |
| 80 | + } |
| 81 | +} |
0 commit comments