11public class Assignment_Backtracking {
2+ // Solution 1 . --------------------------------
23 public static void ratInMaze (int maze [][], int row , int col , int newMaze [][]) {
34 // base case
45 if (row == maze .length - 1 && col == maze .length - 1 ) {
@@ -29,6 +30,83 @@ public static void printMaze(int maze[][]) {
2930 System .out .println ();
3031 }
3132
33+ // Solution 2 . --------------------------------
34+ final static char [][] L = { {}, {}, { 'a' , 'b' , 'c' }, { 'd' , 'e' , 'f' }, { 'g' , 'h' , 'i' },
35+ { 'j' , 'k' , 'l' }, { 'm' , 'n' , 'o' }, { 'p' , 'q' , 'r' , 's' },
36+ { 't' , 'u' , 'v' }, { 'w' , 'x' , 'y' , 'z' } };
37+
38+ public static void letterCombinations (String D ) {
39+ int len = D .length ();
40+ if (len == 0 ) {
41+ System .out .print ("null" );
42+ return ;
43+ }
44+ bfs (0 , len , new StringBuilder (), D );
45+ }
46+
47+ public static void bfs (int pos , int len , StringBuilder sb , String D ) {
48+ if (pos == len ) {
49+ System .out .print (sb .toString ());
50+ } else {
51+ char [] letters = L [Character .getNumericValue (D .charAt (pos ))];
52+ for (int i = 0 ; i < letters .length ; i ++)
53+ bfs (pos + 1 , len , new StringBuilder (sb ).append (letters [i ]), D );
54+ }
55+ }
56+
57+ // Solution 3 . --------------------------------
58+ static int N = 8 ;
59+
60+ public static boolean isSafe (int x , int y , int sol [][]) {
61+ return (x >= 0 && x < N && y >= 0 && y < N
62+ && sol [x ][y ] == -1 );
63+ }
64+
65+ public static void printSolution (int sol [][]) {
66+ for (int x = 0 ; x < N ; x ++) {
67+ for (int y = 0 ; y < N ; y ++)
68+ System .out .print (sol [x ][y ] + " " );
69+ System .out .println ();
70+ }
71+ }
72+
73+ public static boolean solveKT (int n ) {
74+ int sol [][] = new int [n ][n ];
75+ for (int x = 0 ; x < N ; x ++)
76+ for (int y = 0 ; y < N ; y ++)
77+ sol [x ][y ] = -1 ;
78+ int xMove [] = { 2 , 1 , -1 , -2 , -2 , -1 , 1 , 2 };
79+ int yMove [] = { 1 , 2 , 2 , 1 , -1 , -2 , -2 , -1 };
80+ //As the Knight starts from cell(0,0)
81+ sol [0 ][0 ] = 0 ;
82+ if (!solveKTUtil (0 , 0 , 1 , sol , xMove , yMove )) {
83+ System .out .println ("Solution does not exist" );
84+ return false ;
85+ } else
86+ printSolution (sol );
87+ return true ;
88+ }
89+
90+ public static boolean solveKTUtil (int x , int y , int movei , int sol [][],
91+ int xMove [], int yMove []) {
92+ int k , next_x , next_y ;
93+ if (movei == N * N )
94+ return true ;
95+ for (k = 0 ; k < 8 ; k ++) {
96+ next_x = x + xMove [k ];
97+ next_y = y + yMove [k ];
98+ if (isSafe (next_x , next_y , sol )) {
99+ sol [next_x ][next_y ] = movei ;
100+ if (solveKTUtil (next_x , next_y , movei + 1 ,
101+ sol , xMove , yMove ))
102+ return true ;
103+ else
104+ sol [next_x ][next_y ] = -1 ; // backtracking
105+ }
106+ }
107+ return false ;
108+ }
109+
32110 public static void main (String [] args ) {
33111 // Question 1 : Rat in a Maze --------------------------------
34112 // You are given a starting position for a rat which is stuck in a maze at an initial point (0, 0) (the
@@ -60,8 +138,14 @@ public static void main(String[] args) {
60138 // A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1
61139 // does not map to any letters.
62140 String digits1 = "23" ; // Output : "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
141+ letterCombinations (digits1 );
142+ System .out .println ();
63143 String digits2 = "2" ; // Output : "a", "b", "c"
144+ letterCombinations (digits2 );
145+ System .out .println ();
64146 String digits3 = "" ; // Output : ""
147+ letterCombinations (digits3 );
148+ System .out .println ("\n " );
65149
66150 // Question 3 : Knight’s Tour --------------------------------
67151 // Given a N*N board with the Knight placed on the first block of an empty board. Moving
@@ -76,6 +160,7 @@ public static void main(String[] args) {
76160 // 47 50 45 54 25 20 11 14
77161 // 56 43 52 3 22 13 24 5
78162 // 51 46 55 44 53 4 21 12
163+ solveKT (N );
79164
80165 }
81166}
0 commit comments