From 97bc9c864ecf4081b50c2f05f79ff17263eb29dd Mon Sep 17 00:00:00 2001 From: harsh_v <96617032+HARSHV10@users.noreply.github.com> Date: Mon, 3 Oct 2022 22:44:06 +0530 Subject: [PATCH] added rat and maze problem added rat and maze problem --- rat_in_a_maze.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 rat_in_a_maze.cpp diff --git a/rat_in_a_maze.cpp b/rat_in_a_maze.cpp new file mode 100644 index 0000000..1604755 --- /dev/null +++ b/rat_in_a_maze.cpp @@ -0,0 +1,51 @@ +#include +using namespace std ; + +bool solve(char arr[10][10],int ans[][10],int n,int i,int j){ + + + // we will first assume ki jaha par hum hai ussse solution aa jaega + //so, + if(i==n&&j==n){ + ans[n][n]=1; + for(int i=0;i<=n;i++){ + for(int j=0;j<=n;j++){ + cout<n||j>n){ + return false ; + } + if(ans[i][j]=='0'){ + return false; + } + ans[i][j]=1; + // now we will check that can we go right or left or not + bool right=solve(arr,ans,n,i,j+1); + bool down = solve(arr,ans,n,i+1,j); + // yaha backtracking wala case hoga jismai baki sare path zero ho jaenge except hte one jisse ja sake + ans[i][j]=0; + if(right || down ){ + return true; + } + else{ + return false; + } + +} + + +int main(){ + char maze[10][10]={"0100","0001","1010","1100"}; + int sol[10][10]={0}; + int n=4; + bool ans=solve(maze,sol,n-1,0,0); + if(ans==false){ + cout<<"no"<