-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRat In A Maze Problem
More file actions
46 lines (44 loc) · 955 Bytes
/
Rat In A Maze Problem
File metadata and controls
46 lines (44 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int sol[20][20];
void travel(int maze[][20],int sol[20][20],int n,int row,int col){
if(row==n-1 && col==n-1){
sol[row][col]=1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<sol[i][j]<<" ";
}
}
cout<<endl;
return;
}
if(row<0 || row>=n || col<0 || col>=n || maze[row][col]==0 || sol[row][col]==1){
return;
}
sol[row][col]=1;
travel(maze,sol,n,row-1,col);
travel(maze,sol,n,row+1,col);
travel(maze,sol,n,row,col-1);
travel(maze,sol,n,row,col+1);
sol[row][col]=0;
//return;
}
void ratInAMaze(int maze[][20], int n){
memset(sol,0,20*20*sizeof(int));
travel(maze,sol,n,0,0);
}
#include<iostream>
using namespace std;
#include "Solution.h"
int main(){
int n;
cin >> n ;
int maze[20][20];
for(int i = 0; i < n ;i++){
for(int j = 0; j < n; j++){
cin >> maze[i][j];
}
}
ratInAMaze(maze, n);
}