From 442c65f9b47de28f3f8813160e8c275cd7517a5f Mon Sep 17 00:00:00 2001 From: Akshat Goel <98082704+DecryptAG@users.noreply.github.com> Date: Mon, 30 May 2022 23:51:18 +0530 Subject: [PATCH] Create Rat in a maze.cpp --- c++/Rat in a maze.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 c++/Rat in a maze.cpp 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