Last active
February 15, 2024 11:48
-
-
Save Anower77/f6867dded46d4ea8da023a66922ab0e2 to your computer and use it in GitHub Desktop.
DFS on 3D Grid Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int MAX_N = 20; // Maximum dimensions for the 3D grid | |
char a[MAX_N][MAX_N][MAX_N]; // 3D grid matrix | |
bool vis[MAX_N][MAX_N][MAX_N]; // 3D visited matrix | |
int n, m, p; // Dimensions of the 3D grid | |
vector<tuple<int, int, int>> d = {{0,1,0},{0,-1,0},{-1,0,0},{1,0,0},{0,0,1},{0,0,-1}}; // 3D directions | |
bool valid(int ci, int cj, int ck) { | |
return ci >= 0 && ci < n && cj >= 0 && cj < m && ck >= 0 && ck < p && !vis[ci][cj][ck]; | |
} | |
void dfs(int si, int sj, int sk) { | |
cout << si << " " << sj << " " << sk << endl; | |
vis[si][sj][sk] = true; | |
for (const auto& [di, dj, dk] : d) { | |
int ci = si + di; | |
int cj = sj + dj; | |
int ck = sk + dk; | |
if (valid(ci, cj, ck)) { | |
dfs(ci, cj, ck); | |
} | |
} | |
} | |
int main() { | |
cin >> n >> m >> p; | |
for (int i = 0; i < n; ++i) { | |
for (int j = 0; j < m; ++j) { | |
for (int k = 0; k < p; ++k) { | |
cin >> a[i][j][k]; | |
} | |
} | |
} | |
int si, sj, sk; | |
cin >> si >> sj >> sk; // Starting position in the 3D grid | |
dfs(si, sj, sk); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input | |
======== | |
3 3 3 | |
0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 | |
0 0 0 | |
output | |
========== | |
0 0 0 | |
0 0 1 | |
0 1 0 | |
1 0 0 | |
0 0 2 | |
0 1 2 | |
0 2 2 | |
1 0 2 | |
1 1 2 | |
2 0 2 | |
1 1 1 | |
1 0 1 | |
0 0 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment