-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabirinto.c
More file actions
199 lines (171 loc) · 4.49 KB
/
labirinto.c
File metadata and controls
199 lines (171 loc) · 4.49 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define TAM 10
void push(int stack[], int *counter, int x)
{
stack[*counter] = x;
(*counter)++;
}
int pop(int stack[], int *counter)
{
int result = stack[*counter - 1];
(*counter)--;
return result;
}
int pathfinder_Iterative(char maze[10][10], int start_col, int start_row)
{
int stack[256];
bool marked[256];
int parent[256];
int counter = 0;
int start = start_row * 10 + start_col;
int final_pos = -1;
for (int i = 0; i < 256; i++)
{
marked[i] = false;
parent[i] = -1;
}
marked[start] = true;
push(stack, &counter, start);
int dRow[4] = {-1, 1, 0, 0};
int dCol[4] = {0, 0, -1, 1};
while (counter > 0)
{
int pos = pop(stack, &counter);
int row = pos / 10;
int col = pos % 10;
if (maze[row][col] == 'S')
{
final_pos = pos;
break;
}
for (int i = 0; i < 4; i++)
{
int nc = col + dCol[i];
int nr = row + dRow[i];
if (nr < 0 || nr >= 10 || nc < 0 || nc >= 10)
{
continue;
}
if (maze[nr][nc] == '#' || maze[nr][nc] == 'E')
{
continue;
}
int new_pos = nr * 10 + nc;
if (marked[new_pos])
{
continue;
}
marked[new_pos] = true;
parent[new_pos] = pos;
push(stack, &counter, new_pos);
}
}
if (final_pos == -1)
{
return -1;
}
int curr = final_pos;
while (parent[curr] != -1)
{
int r = curr / 10;
int c = curr % 10;
if (maze[r][c] != 'S' && maze[r][c] != 'E')
maze[r][c] = 'x';
curr = parent[curr];
}
return 0;
}
int encontrar_E(char lab[TAM][TAM], int *linha_E, int *coluna_E)
{
int i, j;
for (i = 0; i < TAM; i++)
{
for (j = 0; j < TAM; j++)
{
if (lab[i][j] == 'E')
{
*linha_E = i;
*coluna_E = j;
return 1;
}
}
}
return 0; // sem entrada
}
bool caminho(char lab[TAM][TAM], int linha, int coluna)
{
if (linha < 0 || linha >= TAM || coluna < 0 || coluna >= TAM)
{ // limites
return false;
}
else
{
if (lab[linha][coluna] == '#' || lab[linha][coluna] == 'x')
{ // parede ou já visitado
return false;
}
else if (lab[linha][coluna] == 'S')
{
return true;
}
}
if (lab[linha][coluna] == '.' || lab[linha][coluna] == 'E')
{
char aux = lab[linha][coluna]; //guarda valor original
if (lab[linha][coluna] != 'E')
lab[linha][coluna] = 'x';
// cima, baixo, esquerda, direita
if (caminho(lab, linha - 1, coluna))
return true;
if (caminho(lab, linha + 1, coluna))
return true;
if (caminho(lab, linha, coluna - 1))
return true;
if (caminho(lab, linha, coluna + 1))
return true;
lab[linha][coluna] = aux;
}
return false;
}
void imprimirLabirinto(char lab[TAM][TAM])
{
int i, j;
for (i = 0; i < TAM; i++)
{
for (j = 0; j < TAM; j++)
{
printf("%c ", lab[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
char labirinto[TAM][TAM] = {
{'#', '#', '#', '.', '#', '.', '#', '#', '#', '#'},
{'E', '.', '.', '#', '.', '.', '.', '#', '.', '#'},
{'#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
{'#', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
{'#', '.', '#', '#', '#', '#', '#', '#', '.', '#'},
{'.', '.', '#', '.', '.', '.', '#', '.', '.', '#'},
{'#', '.', '#', '#', '#', '.', '#', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.', '.', '#', '.', '.'},
{'#', '#', '#', '#', '#', '#', '.', '.', '.', 'S'},
{'#', '#', '#', '#', '#', '#', '.', '#', '#', '#'}};
int linha_E, coluna_E;
encontrar_E(labirinto, &linha_E, &coluna_E);
printf("___ Original ___\n");
imprimirLabirinto(labirinto);
if (caminho(labirinto, linha_E, coluna_E))
{
printf("___ Solucao Recursiva ___\n");
imprimirLabirinto(labirinto);
}
// printf("___ Solucao Interativa ___\n");
// pathfinder_Iterative(labirinto, linha_E, coluna_E);
// imprimirLabirinto(labirinto);
return 0;
}