diff --git a/c++/Rat in a maze.cpp b/c++/Rat in a maze.cpp new file mode 100644 index 0000000..28e1cff --- /dev/null +++ b/c++/Rat in a maze.cpp @@ -0,0 +1,91 @@ +//Alogrithm for Rat in a Maze +//Akshat Goel +//21065009 +//Civil + +#include +using namespace std; + +bool Safe(int x, int y, int n, vector> visited, vector> &m ) +{ + if((x>0 && x0 && y> &m, int n, vector& ans, int x, int y, vector> visited, string path){ + if(x==n-1 & y==n-1){ + ans.push_back(path); + return; + } + + visited[x][y] =1; + + //down + int newx = x+1; + int newy = y; + if(Safe(newx,newy,n, visited,m)) + { + path.push_back('D'); + solve(m,n,ans,newx,newy,visited,path); + path.pop_back(); + } + //left + int newx = x; + int newy = y-1; + if(Safe(newx,newy,n, visited,m)) + { + path.push_back('L'); + solve(m,n,ans,newx,newy,visited,path); + path.pop_back(); + } + //right + int newx = x; + int newy = y+1; + if(Safe(newx,newy,n, visited,m)) + { + path.push_back('R'); + solve(m,n,ans,newx,newy,visited,path); + path.pop_back(); + } + //up + int newx = x-1; + int newy = y; + if(Safe(newx,newy,n, visited,m)) + { + path.push_back('U'); + solve(m,n,ans,newx,newy,visited,path); + path.pop_back(); + } + + + visited[x][y] =0; +} + +vector Path(vector> &m, int n ){ + vector ans; + + if(m[0][0]==0) + { + return ans; + } + + int srcx = 0; + int srcy = 0; + vector> visited = m; + + for(int i=0;i