-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.cpp
64 lines (55 loc) · 1.52 KB
/
maze.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
#include <cstdio>
#include <cstring>
using namespace std;
#define size 5
char maze[size][size] = {{'X','_','_','_','_'},
{'_','#','#','#','#'},
{'_','_','_','_','_'},
{'#','#','#','#','_'},
{'#','#','#','#','X'}};
bool vis[size][size];
int pos[4][2] = {{0, -1},
{0, 1},
{-1, 0},
{ 1, 0}};
int step = -1;
void printmaze() {
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
printf("%c",maze[i][j]);
}
printf("\n");
}
printf("\n");
}
bool dfs(int x, int y) {
vis[x][y] = true;
for (int i = 0; i < 4; i++) {
int novox = x+pos[i][0];
int novoy = y+pos[i][1];
if (novox >= 0 && novox < size && novoy >= 0 && novoy < size && !vis[novox][novoy]) {
if (maze[novox][novoy] == 'X') return true;
if (maze[novox][novoy] == '_') {
maze[novox][novoy] = 'o';
if (step < 0) printmaze();
if (dfs(novox, novoy)) return true;
maze[novox][novoy] = '_';
if (step < 0) printmaze();
}
}
}
return false;
}
int main() {
memset(vis, 0, sizeof(vis));
printf("Labirinto Original:\n\n");
printmaze();
if (dfs(0, 0)) {
printf("Solução do Labirinto:\n\n");
printmaze();
}
else {
printf("O Labirinto não possui solução.\n");
}
return 0;
}