forked from mrigaankzoro/Hacktoberfest24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RatInAMaze.cpp
91 lines (76 loc) · 2.04 KB
/
RatInAMaze.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// https://www.naukri.com/code360/problems/rat-in-a-maze_1215030
#include <bits/stdc++.h>
using namespace std;
void recurseCall(int n, int row, int col, string str, vector<vector<int>> &mat, vector<string> &a)
{
if (row == n - 1 && col == n - 1)
{
a.push_back(str);
return;
}
mat[row][col] = 9;
// LOOK SOUTH
if (row + 1 <= n - 1 && mat[row + 1][col] == 1)
{
mat[row + 1][col] = 9;
recurseCall(n, row + 1, col, str + 'D', mat, a);
mat[row + 1][col] = 1;
}
// LOOK LEFT
if (col - 1 >= 0 && mat[row][col - 1] == 1)
{
mat[row][col - 1] = 9;
recurseCall(n, row, col - 1, str + 'L', mat, a);
mat[row][col - 1] = 1;
}
// LOOK RIGHT
if (col + 1 <= n - 1 && mat[row][col + 1] == 1)
{
mat[row][col + 1] = 9;
recurseCall(n, row, col + 1, str + 'R', mat, a);
mat[row][col + 1] = 1;
}
// LOOK NORTH
if (row - 1 >= 0 && mat[row - 1][col] == 1)
{
mat[row - 1][col] = 9;
recurseCall(n, row - 1, col, str + 'U', mat, a);
mat[row - 1][col] = 1;
}
mat[row][col] = 1;
}
vector<string> searchMaze(vector<vector<int>> &arr, int n)
{
// Write your code here.
vector<string> a;
string s;
if (arr[0][0] == 1)
recurseCall(n, 0, 0, s, arr, a);
return a;
}
int main()
{
// vector<vector<int>> a = {
// {1, 0, 0, 0},
// {1, 1, 0, 1},
// {1, 1, 0, 0},
// {0, 1, 1, 1},
// };
// vector<vector<int>> a = {{1, 0}, {1, 1}};
// vector<vector<int>> a = {{1, 1}, {0, 1}};
// vector<vector<int>> a = {{1, 0}, {1, 0}};
// vector<vector<int>> a = {{1, 1, 1},
// {1, 0, 1},
// {1, 1, 1}};
vector<vector<int>> a = {{1, 1, 1, 0}, {1, 1, 1, 0}, {1, 0, 1, 1}, {0, 0, 0, 1}};
vector<string> ans;
ans = searchMaze(a, a.size());
// cout << endl
// << endl
// << endl
// << endl;
for (auto i : ans)
{
cout << i << endl;
}
}