-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolve.c
247 lines (191 loc) · 4.87 KB
/
solve.c
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_ROW 150
#define MAX_COL 150
typedef struct registro node;
struct registro {
char info;
node *prox;
};
typedef struct cabeca head;
struct cabeca {
int num_itens;
node *prox;
node *ultimo;
};
typedef struct {
int x;
int y;
} pair;
typedef struct {
bool paredes[4]; // paredes[0] - cima | paredes[1] - esquerda | paredes[2] -
// baixo | parede[3] - direita
int dir;
bool visitado;
pair pai;
} mapa;
// Declaração de variáveis globais
int mouseLookAt = 0;
mapa grid[MAX_ROW][MAX_COL];
pair directions[] = {
(pair){-1, 0}, // pra cima
(pair){0, -1}, // pra esquerda
(pair){1, 0}, // pra baixo
(pair){0, 1}, // pra direita
};
pair goal;
head *stack;
node *criar_no(char x) {
node *no = malloc(sizeof(node));
no->prox = NULL;
no->info = x;
return no;
}
head *criar_stack() {
head *le = malloc(sizeof(head));
le->num_itens = 0;
le->prox = NULL;
le->ultimo = NULL;
return le;
}
bool stackVazia(head *p) { return (p->prox == NULL); }
void empilha(head *lista, char x) {
node *novo = criar_no(x);
if (novo) {
if (stackVazia(lista)) lista->ultimo = novo;
novo->prox = lista->prox;
lista->prox = novo;
lista->num_itens++;
}
}
char desempilha(head *lista) {
node *topo = lista->prox;
lista->prox = topo->prox;
if (topo == lista->ultimo) lista->ultimo = NULL;
lista->num_itens--;
char x = topo->info;
free(topo);
return x;
}
int doAction(char c) {
printf("%c\n", c);
fflush(stdout);
int ans;
scanf("%d", &ans);
return ans;
}
pair move(pair originCell, int direcaoRato) {
int x = originCell.x, y = originCell.y;
pair destinyCell;
x += directions[direcaoRato].x;
y += directions[direcaoRato].y;
grid[x][y].pai = originCell;
grid[x][y].visitado = true;
grid[x][y].dir = direcaoRato;
destinyCell.x = x;
destinyCell.y = y;
return destinyCell;
}
void setWall(pair coord, int direction) {
int x = coord.x, y = coord.y;
grid[x][y].paredes[direction] = true;
}
bool isVisited(pair currentCell, int ratoDir) {
int x = currentCell.x, y = currentCell.y;
x += directions[ratoDir].x;
y += directions[ratoDir].y;
return grid[x][y].visitado;
}
void rotate(int direction, int flag) {
if (mouseLookAt == direction) return;
int cont = 0;
for (int i = 0; i < 4; i++) {
cont++;
mouseLookAt = (mouseLookAt + 1) % 4;
if (mouseLookAt == direction) break;
}
if (cont == 3) {
doAction('r');
if (flag) empilha(stack, 'r');
} else {
for (int i = 0; i < cont; i++) {
doAction('l');
if (flag) empilha(stack, 'l');
}
}
}
void goBack(pair currentCell, bool flag) {
// printf("Entrou no Goback!!!\n");
int x = currentCell.x, y = currentCell.y;
int dirCameFrom = grid[x][y].dir;
dirCameFrom = (dirCameFrom + 2) % 4;
// printf("o tal do dirCameFron %d\n",dirCameFrom );
rotate(dirCameFrom, flag);
// printf("o tal do dirCameFron depois do rotacoes %d\n",dirCameFrom );
doAction('w');
if (flag) empilha(stack, 'w');
}
bool dfs(pair coord) {
// printf("Coordenada que estou: {%d, %d}\n", coord.x, coord.y);
for (int i = 0; i < 4; i++) {
if (mouseLookAt != i) rotate(i, false);
// printf("mouseLookAt: %d\n", mouseLookAt);
if (isVisited(coord, i) || grid[coord.x][coord.y].paredes[i]) {
// fprintf(stderr, "o no que eu estou olhando foi visitado\n");
// printf(
// "-----------------------------------------------------------------"
// "\n");
continue;
}
int judgeAns = doAction('w');
if (judgeAns == 1) {
if (dfs(move(coord, mouseLookAt))) {
if (coord.x != MAX_ROW / 2 || coord.y != MAX_COL / 2)
goBack(coord, true);
return true;
}
// printf("SAIMOS DA RECURSÃO!\n");
// printf("O rato tá olhando pra la: %d", mouseLookAt);
} else if (judgeAns == 0) {
setWall(coord, mouseLookAt);
} else if (judgeAns == 2) {
goBack(move(coord, mouseLookAt), true);
goBack(coord, true);
goal = coord;
return true;
}
// printf(
// "-----------------------------------------------------------------\n");
}
goBack(coord, false);
return false;
}
void finishMaze() {
int judgeAns;
while (!stackVazia(stack)) {
char action = desempilha(stack);
if (action == 'l')
judgeAns = doAction('r');
else if (action == 'r')
judgeAns = doAction('l');
else
judgeAns = doAction('w');
}
}
int main() {
stack = criar_stack();
pair inital_coord = {MAX_ROW / 2, MAX_COL / 2};
grid[MAX_ROW / 2][MAX_COL / 2].visitado = true;
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
for (int k = 0; k < 4; k++) grid[i][j].paredes[k] = false;
grid[i][j].visitado = false;
}
}
dfs(inital_coord);
doAction('l');
doAction('l');
finishMaze();
return 0;
}