Skip to content

Commit 3fb3f8f

Browse files
authored
Merge pull request #101 from Shoyeb45/feature/labyrinth
Added Solution to the Graph Algorithm -Labyrinth
2 parents 3905d4d + 9a9138a commit 3fb3f8f

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

4_graph_algorithms/Labyrinth.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
6+
Labyrinth(https://cses.fi/problemset/task/1193)
7+
8+
# Prerequisite:
9+
-> Graph theory, Breadth First Search
10+
11+
# Main Idea:
12+
-> We can start from the starting position and visit all the 4 direction one by one and when we reach 'B', we'll stop. But the main task is to find the
13+
actual path. For tracking the path, we can backtrack the from the last node('B') to first node('A').
14+
15+
Solution:
16+
17+
1. Find the position of the 'A' and store that in queue.
18+
2. Start BFS from starting position and visit all the cell.
19+
3. While processing if we reach our destination then backtrack and construct the path else we can't reach till the end.
20+
21+
Complexties:
22+
1. Time Complexity: O(n * m)
23+
2. Space Commplexity: O(n * m)
24+
*/
25+
26+
27+
int main() {
28+
ios::sync_with_stdio(false);
29+
cin.tie(nullptr);
30+
31+
int n, m;
32+
cin >> n >> m;
33+
34+
/// Helper function to verify indices
35+
std::function<bool(int, int)> is_valid_index = [&](int x, int y) -> bool {
36+
return x >= 0 && y >= 0 && x < n && y < m;
37+
};
38+
39+
/// Given labyrinth map
40+
vector<string> labyrinth(n);
41+
/// Stores if the cell is visited or not, so that we don't visit the same cell twice.
42+
vector<vector<bool>> vis(n, vector<bool> (m, false));
43+
/// Queue to perform DFS
44+
queue<pair<int, int>> q;
45+
/// Matrix to store the information that from which side we came. path[2][3] = 'L', means we came from (2, 4)
46+
vector<vector<char>> path(n, vector<char> (m));
47+
48+
// These array for traversion to the L, R, D and U
49+
int dx[] = {1, 0, -1, 0};
50+
int dy[] = {0, 1, 0, -1};
51+
char direction[] = {'D', 'R', 'U', 'L'};
52+
53+
for (int i = 0; i < n; i++) {
54+
cin >> labyrinth[i];
55+
for (int j = 0; j < m; j++) {
56+
if (labyrinth[i][j] == 'A') {
57+
q.push({i, j});
58+
vis[i][j] = true;
59+
break;
60+
}
61+
}
62+
}
63+
bool is_path_exists = false;
64+
/// Index of destination
65+
int final_x, final_y;
66+
67+
// BFS Algorithm
68+
while (!q.empty()) {
69+
int x = q.front().first, y = q.front().second;
70+
q.pop();
71+
72+
for (int k = 0; k < 4; k++) {
73+
int nx = x + dx[k], ny = y + dy[k];
74+
if (!is_valid_index(nx, ny)) {
75+
continue;
76+
}
77+
78+
// We didn't go to this cell. Note that we can't visit the cell which has '#'
79+
if (labyrinth[nx][ny] == '.' && !vis[nx][ny]) {
80+
vis[nx][ny] = true;
81+
q.push({nx, ny});
82+
path[nx][ny] = direction[k]; // Store the information from where we came
83+
} else if (labyrinth[nx][ny] == 'B') {
84+
path[nx][ny] = direction[k];
85+
final_x = nx, final_y = ny;
86+
is_path_exists = true;
87+
break;
88+
}
89+
}
90+
if (is_path_exists) {
91+
break;
92+
}
93+
}
94+
95+
if (!is_path_exists) {
96+
cout << "NO\n";
97+
return 0;
98+
}
99+
100+
// We need to construct the path
101+
string full_path = "";
102+
103+
while (is_valid_index(final_x, final_y) && labyrinth[final_x][final_y] != 'A') {
104+
full_path += path[final_x][final_y];
105+
// Dependeing upon the direction, adjust the index.
106+
// 'U' means we came from the down, so go down
107+
if (path[final_x][final_y] == 'U') {
108+
final_x += 1;
109+
} else if (path[final_x][final_y] == 'D') {
110+
final_x -= 1;
111+
} else if (path[final_x][final_y] == 'L') {
112+
final_y += 1;
113+
} else {
114+
final_y -= 1;
115+
}
116+
}
117+
// reverse the string as we came from the last
118+
reverse(full_path.begin(), full_path.end());
119+
cout << "YES\n" << full_path;
120+
return 0;
121+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ The solutions are organized by problem categories:
6363
### 3. Dynamic Programming
6464
- **Minimizing Coins** - Classic coin change problem
6565

66+
### 4. [Graph Algorithms](./4_graph_algorithms/)
67+
- [**Counting Rooms**](./4_graph_algorithms/Counting_Rooms.cpp)
68+
- [**Labyrinth** ](./4_graph_algorithms/Labyrinth.cpp) - Finding path from A to B
69+
6670
### 7. Mathematics
6771
- **Exponentiation 2** - Fast exponentiation with modular arithmetic
6872
- **Sum of Divisors** - Mathematical series calculation

0 commit comments

Comments
 (0)