Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*** Pathfindig ***/
- int8_t Demo::PathFinding(int8_t sx, int8_t sy, int8_t ex, int8_t ey) {
- // Proteccion de coordenadas
- if ((sx < 0) || (ex < 0) || (sx >= maze_w) || (ex >= maze_w)) return -1;
- if ((sy < 0) || (ey < 0) || (sy >= maze_h) || (ey >= maze_h)) return -1;
- // Si la entrada o salida es invalida
- if ((maze_shape[sy][sx] != maze_floor) || (maze_shape[ey][ex] != maze_floor)) return -1;
- // Marca la salida del laberinto
- maze_dumbbells[ey][ex] = maze_target;
- // Genera la red de pesos
- MazeDumbbells(sy, sx, 1);
- // Marca la salida del laberinto
- maze_dumbbells[ey][ex] = maze_target;
- // Segun la red de pesos creada, busca la ruta mas corta
- MazePath(sy, sx, 1);
- // Marca las entradas y salidas
- maze_path[sy][sx] = maze_start;
- maze_path[ey][ex] = maze_end;
- return 0;
- }
- /*** Busca la ruta a la salida - Colocacion de pesos ***/
- void Demo::MazeDumbbells(int8_t py, int8_t px, int32_t cnt) {
- // Si estas fuera de rango, devuelve -1
- if ((py < 0) || (px < 0) || (py >= maze_h) || (px >= maze_w)) return;
- // Exploracion previa
- if (maze_dumbbells[py][px] == maze_target) { // Si es la salida, sal
- if (path_max_deep == maze_invalid) {
- path_max_deep = cnt;
- } else {
- if (cnt < path_max_deep) path_max_deep = cnt;
- }
- return;
- } else if (maze_dumbbells[py][px] == maze_invalid) { // Si esta marcada como invalida, sal
- return;
- } else if ((maze_dumbbells[py][px] != 0) && (maze_dumbbells[py][px] < cnt)) { // Si ya esta visitada y pesa menos, sal
- return;
- }
- // Coloca el peso en la casilla si es inferior
- maze_dumbbells[py][px] = cnt;
- // Incrementa en 1 el peso
- cnt ++;
- // Recursividad en 4 direcciones
- MazeDumbbells((py - 1), px, cnt);
- MazeDumbbells((py + 1), px, cnt);
- MazeDumbbells(py, (px - 1), cnt);
- MazeDumbbells(py, (px + 1), cnt);
- }
- /*** Busca la ruta a la salida - Ruta mas corta ***/
- int8_t Demo::MazePath(int8_t py, int8_t px, int32_t cnt) {
- // Si estas fuera de rango, devuelve -1
- if ((py < 0) || (px < 0) || (py >= maze_h) || (px >= maze_w)) return maze_invalid;
- // Exploracion previa
- if (maze_dumbbells[py][px] == maze_target) { // Si es la salida, sal
- return maze_target;
- } else if (maze_dumbbells[py][px] == maze_invalid) { // Si esta marcada como invalida, sal
- return maze_invalid;
- } else if (maze_dumbbells[py][px] != cnt) { // Si el peso no coincide, sal
- return maze_invalid;
- } else if (cnt > path_max_deep) {
- return maze_invalid;
- }
- // Incrementa en 1 el peso
- cnt ++;
- // Recursividad en 4 direcciones
- if (
- (MazePath((py - 1), px, cnt) == maze_target)
- ||
- (MazePath((py + 1), px, cnt) == maze_target)
- ||
- (MazePath(py, (px - 1), cnt) == maze_target)
- ||
- (MazePath(py, (px + 1), cnt) == maze_target)
- ) {
- maze_path[py][px] = maze_target;
- return maze_target;
- }
- return maze_invalid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement