MAZE USING BACKTRACKING DESIGN AND ANALYSIS OF ALGORITHMS MINI-PROJECT REPORT For the partial fulfillment of completion of degree of B.Tech.(CSE) Submitted by M.HEMANTH(RA2011003011124) UDAY KUMAR(RA2011003011117) Under the Guidance of M.REVANTHI AcademicYear:2021-2022 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 1 1 Declaration I, hereby declare that the work presented in this dissertation entitled "MAZE USING BACKTRACKING" has been done by me and my team, and this dissertation embodies my own work CONTENT Page Numbers S.No Title of Content 1. Contribution Table 1 2. Problem Definition 2 3. Problem Explanation with Diagram 2 4. Design Techniques used 3 5. Algorithm of the problem 4 6. Algorithm Explanation with example 4 7. Problem Code 6 8. Complexity Analysis 9 9. Conclusion 9 References 9 10. 2 CONTRIBUTION TABLE SLNO Name REGNO Contribution 1 RA201103011117 Code, algorithm 2 UDAY KUMAR M.HEMANTH RA2011003011124 Preparation of report Test and analysis of code Problem Definition Given a maze in the form of a matrix of size n * n. Each cell is either clear or blocked denoted by 1 and 0 respectively. A person sits at the top-left cell [0][0] and there exists a destination at the bottom-right cell [N-1][N-1]. Both these cells are guaranteed to be clear. You need to find if a person can get the destination he can move only in one of the two direct ions-down and right. He can’t move to blocked cells. Problem Explanation with Diagram Following is a binary matrix representation of the above maze. {1,0,0,0} {1,1,0,1} {0,1,0,0} {1,1,1,1} NoteGrey or zero represents blocks in maze. White or one’s represent path available. Green is the solution path found. 3 Problem: Start Destination Design Techniques used : Backtracking Backtracking is an algorithmic technique used for recursively addressing issues by attempting to construct a solution incrementally. Backtracking is the process of solving one component at I mean discarding those solutions that fail to satisfy the problem's requirements at any point in time (here, time refers to the time elapsed till reaching any level of the search tree Create a recursive function that will follow a path and check if it leads to the desired destination. Backtrack and try alternate paths if the path does not lead to the destination Using this technique, we can keep bounding the incorrect paths. We can achieve feasible paths form a pool of solution paths. 4 Algorithm of the problemStart: //Create a recursive function,which takes an initial matrix(Maze), output matrix(Solution) and position(i,j) Print Solution(int sol[N][N]) {for i := 0 toN{ For j := 0 to N print(sol[i][j]) printf("\n"); }} Solve Maze Util(maze[N][N],x, y,sol[N][N]) {if(x==N-1&&y==N-1) then(x,y):=1 //if(x,y is goal)return true return true; if(is Safe(maze,x,y)==true)then // Check if maze[x][y] is validsol[x][y]:=1 // mark x,y as part of solution path if(solve Maze Util(maze,x+1,y,sol)==true)then //Move forward if(solve Maze Util(maze,x,y+1,sol)==true)then //Move down return true; Else sol[x][y]:=0; Return false //Backtrack if no path is found }return false} End 5 Algorithm Explanation with example This algorithm will take the maze as a matrix. In the matrix, the value1indicates the free space and 0 indicates the wall of blocked areaIf the position is out of the matrix or the position is not valid then “bound “the path Mark the position output[i][j] as1and check if the current position is the destination or not. Recursively call for position(i+1, j)and(i,j+1).Unmark position(i,j), i.e output[i][j]= 0. SolutionStart Destination 6 Problem Code/*program to solve Rat in a Maze problem using backtracking*/ #include<stdio.h> // Maze size#define N4 bool solve Maze Util(int maze[N][N], int x,int y,int sol[N][N]); /*A function to print solution matrix sol[N][N]*/ void print Solution(int sol[N][N]) { for (int i=0;i<N;i++) { for (int j=0;j<N;j++) {printf("%d",sol[i][j]); } printf("\n"); } } /*A function to check if x,y is valid index for N*Nmaze* / bool is Safe(int maze[N][N],int x,int y) { //if(x,y outside maze)return false if(x>= 0&&x< N&&y>= 0&&y<N&&maze[x][y] == 1)return true; return false; } Bool solve Maze(int maze[N][N]) { /defining a null matrix int sol[N][N] ={ {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} 7 }; if(solve Maze Util(maze,0,0,sol)==false) { printf("Solution doesn't exist"); return false; } PrintSolution(sol); return true; } Bool solve Maze Util(int maze[N][N], int x,int y,int sol[N][N]) { // if(x,y is goal) return true if(x==N-1&&y==N-1) { sol[x][y]=1; return true; } //Check if maze[x][y] is valid if(is Safe(maze,x,y)==true) { //mark x,y as part of solution pathsol[x][y] =1; /*Move forward in x direction */ if(solve Maze Util (maze,x+1,y,sol)==true) return true; /*If moving in x direction doesn't give solution then Move down in y direction*/ if(solve Maze Util(maze,x,y+1,sol)==true) return true; unmark x,y as part of solution path */sol[x][y] =0; return false; } 8 Return false; } //program to test above function int main() { int maze[N][N]={ {1,0,0,0}, {1,1,0,1}, {0,1,0,0}, {1,1,1,1} }; SolveMaze(maze) return 0; } Output {1,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,1,1,1} Complexity Analysis SLNO Complexity 1 Time-Complexity: O(2*(n^2)) 2 Auxiliary Space O(n^2) Conclusion Backtracking is a general algorithm for finding solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate as soon as it determines that the candidate cannot possibly be completed to a valid solution. Backtracking solves each instance of a problem in an acceptable amount of time. It generates all elements of the problem state. Thus, a feasible path is found for a person from source to destination successfully. REFERENCES Clear programmer,Educative 9