primer intento de resolver el laberinto wip
This commit is contained in:
parent
9201c4e75d
commit
efa8485a4c
107
main.cpp
107
main.cpp
|
|
@ -1,7 +1,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include "api.h"
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include "api.h"
|
||||||
#include "mms.h"
|
#include "mms.h"
|
||||||
|
|
||||||
#define W 15 /* ancho y alto del laberinto -1 */
|
#define W 15 /* ancho y alto del laberinto -1 */
|
||||||
|
|
@ -11,12 +11,6 @@ struct maze { /* Distancias de Manhattan */
|
||||||
int vwalls[15][15];
|
int vwalls[15][15];
|
||||||
int hwalls[15][15];
|
int hwalls[15][15];
|
||||||
int mhtn[16][16];
|
int mhtn[16][16];
|
||||||
maze(){
|
|
||||||
mhtn[7][7] = 0; /* las celdas del centro tienen */
|
|
||||||
mhtn[7][8] = 0; /* un valor de cero porque son */
|
|
||||||
mhtn[8][7] = 0; /* la meta. */
|
|
||||||
mhtn[8][8] = 0;
|
|
||||||
}
|
|
||||||
}maze;
|
}maze;
|
||||||
|
|
||||||
struct fill {
|
struct fill {
|
||||||
|
|
@ -35,6 +29,7 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
|
||||||
int marked[16][16];
|
int marked[16][16];
|
||||||
std::queue<fill> myqueue;
|
std::queue<fill> myqueue;
|
||||||
|
|
||||||
|
/*
|
||||||
myqueue.push({6,7,0});
|
myqueue.push({6,7,0});
|
||||||
myqueue.push({6,8,0});
|
myqueue.push({6,8,0});
|
||||||
myqueue.push({7,6,0});
|
myqueue.push({7,6,0});
|
||||||
|
|
@ -44,11 +39,12 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
|
||||||
myqueue.push({9,8,0});
|
myqueue.push({9,8,0});
|
||||||
myqueue.push({7,9,0});
|
myqueue.push({7,9,0});
|
||||||
myqueue.push({8,9,0});
|
myqueue.push({8,9,0});
|
||||||
|
*/
|
||||||
|
|
||||||
marked[7][7] = 1; /* las celdas del centro tienen */
|
myqueue.push({7,7,-1});
|
||||||
marked[7][8] = 1; /* un valor de cero porque son */
|
myqueue.push({7,8,-1});
|
||||||
marked[8][7] = 1; /* la meta. */
|
myqueue.push({8,7,-1});
|
||||||
marked[8][8] = 1;
|
myqueue.push({8,8,-1});
|
||||||
|
|
||||||
API::setText(7, 7, 0);
|
API::setText(7, 7, 0);
|
||||||
API::setText(7, 8, 0);
|
API::setText(7, 8, 0);
|
||||||
|
|
@ -61,13 +57,6 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
|
||||||
prevNum = myqueue.front().z;
|
prevNum = myqueue.front().z;
|
||||||
myqueue.pop();
|
myqueue.pop();
|
||||||
|
|
||||||
if (mmstats->x == i && mmstats->y == j) {
|
|
||||||
log("flood end");
|
|
||||||
API::setText(i, j, prevNum+1);
|
|
||||||
maze->mhtn[i][j] = prevNum+1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i < 0 || i > W || j < 0 || j > W
|
if (i < 0 || i > W || j < 0 || j > W
|
||||||
|| marked[i][j]==1) {
|
|| marked[i][j]==1) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -76,10 +65,17 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
|
||||||
API::setText(i, j, prevNum+1);
|
API::setText(i, j, prevNum+1);
|
||||||
maze->mhtn[i][j] = prevNum+1;
|
maze->mhtn[i][j] = prevNum+1;
|
||||||
marked[i][j] = 1;
|
marked[i][j] = 1;
|
||||||
myqueue.push({i+1, j, prevNum +1});
|
if (!maze->vwalls[i][j])
|
||||||
myqueue.push({i-1, j, prevNum +1});
|
myqueue.push({i+1, j, prevNum +1});
|
||||||
myqueue.push({i, j+1, prevNum +1});
|
|
||||||
myqueue.push({i, j-1, prevNum +1});
|
if (!maze->vwalls[i-1][j])
|
||||||
|
myqueue.push({i-1, j, prevNum +1});
|
||||||
|
|
||||||
|
if (!maze->hwalls[i][j])
|
||||||
|
myqueue.push({i, j+1, prevNum +1});
|
||||||
|
|
||||||
|
if (!maze->hwalls[i][j-1])
|
||||||
|
myqueue.push({i, j-1, prevNum +1});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -91,30 +87,49 @@ void follow(struct maze maze, class mmstats* mms) {
|
||||||
int mht = maze.mhtn[mms->x][mms->y];
|
int mht = maze.mhtn[mms->x][mms->y];
|
||||||
int nextmht = mht-1;
|
int nextmht = mht-1;
|
||||||
int next[2];
|
int next[2];
|
||||||
|
int next_L[2];
|
||||||
|
int next_R[2];
|
||||||
|
int next_B[2];
|
||||||
nextcell(mms->x, mms->y, mms->dir, next);
|
nextcell(mms->x, mms->y, mms->dir, next);
|
||||||
if(maze.mhtn[next[0]][next[1]] == nextmht) {
|
nextcell(mms->x, mms->y, mms->head->prev->value, next_L);
|
||||||
|
nextcell(mms->x, mms->y, mms->head->next->value, next_R);
|
||||||
|
nextcell(mms->x, mms->y, mms->head->next->next->value, next_B);
|
||||||
|
if(maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() || !API::wallFront() && API::wallLeft() && API::wallRight()) {
|
||||||
log("forward");
|
log("forward");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nextcell(mms->x, mms->y, mms->head->prev->value, next);
|
else if(maze.mhtn[next_L[0]][next_L[1]] <= nextmht && !API::wallLeft()) {
|
||||||
if(maze.mhtn[next[0]][next[1]] == nextmht) {
|
|
||||||
API::turnLeft();
|
API::turnLeft();
|
||||||
mms->turn(left);
|
mms->turn(left);
|
||||||
log("left");
|
log("left");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nextcell(mms->x, mms->y, mms->head->next->value, next);
|
else if(maze.mhtn[next_R[0]][next_R[1]] <= nextmht && !API::wallRight()) {
|
||||||
if(maze.mhtn[next[0]][next[1]] == nextmht) {
|
|
||||||
API::turnRight();
|
API::turnRight();
|
||||||
mms->turn(right);
|
mms->turn(right);
|
||||||
log("right");
|
log("right");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
API::turnRight();
|
else if(maze.mhtn[next_B[0]][next_B[1]] == nextmht
|
||||||
mms->turn(right);
|
|| API::wallFront() && API::wallLeft() && API::wallRight()) {
|
||||||
API::turnRight();
|
API::turnRight();
|
||||||
mms->turn(right);
|
mms->turn(right);
|
||||||
log("180");
|
API::turnRight();
|
||||||
|
mms->turn(right);
|
||||||
|
log("180");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if(maze.mhtn[next_R[0]][next_R[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallRight()){
|
||||||
|
API::turnRight();
|
||||||
|
mms->turn(right);
|
||||||
|
log("bRight");
|
||||||
|
}
|
||||||
|
else if(maze.mhtn[next_L[0]][next_L[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallLeft()){
|
||||||
|
API::turnLeft();
|
||||||
|
mms->turn(left);
|
||||||
|
log("bLeft");
|
||||||
|
}
|
||||||
|
log("error en la matri");
|
||||||
}
|
}
|
||||||
|
|
||||||
int* nextcell(int i, int j, int dir, int ndir[2]) {
|
int* nextcell(int i, int j, int dir, int ndir[2]) {
|
||||||
|
|
@ -181,15 +196,15 @@ void locateWall(struct maze* maze, class mmstats *mms) {
|
||||||
break;
|
break;
|
||||||
case sur:
|
case sur:
|
||||||
if(API::wallFront() && mms->y != 0) {
|
if(API::wallFront() && mms->y != 0) {
|
||||||
maze->vwalls[mms->x][mms->y-1] = 1;
|
maze->hwalls[mms->x][mms->y-1] = 1;
|
||||||
API::setWall(mms->x, mms->y-1, 'n');
|
API::setWall(mms->x, mms->y-1, 'n');
|
||||||
}
|
}
|
||||||
if(API::wallLeft() && mms->x != 15){
|
if(API::wallLeft() && mms->x != 15){
|
||||||
maze->hwalls[mms->x][mms->y] = 1;
|
maze->vwalls[mms->x][mms->y] = 1;
|
||||||
API::setWall(mms->x, mms->y, 'e');
|
API::setWall(mms->x, mms->y, 'e');
|
||||||
}
|
}
|
||||||
if(API::wallRight() && mms->x != 0){
|
if(API::wallRight() && mms->x != 0){
|
||||||
maze->hwalls[mms->x -1][mms->y] = 1;
|
maze->vwalls[mms->x -1][mms->y] = 1;
|
||||||
API::setWall(mms->x -1, mms->y, 'e');
|
API::setWall(mms->x -1, mms->y, 'e');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -202,7 +217,7 @@ void locateWall(struct maze* maze, class mmstats *mms) {
|
||||||
maze->hwalls[mms->x][mms->y -1] = 1;
|
maze->hwalls[mms->x][mms->y -1] = 1;
|
||||||
API::setWall(mms->x, mms->y -1, 'n');
|
API::setWall(mms->x, mms->y -1, 'n');
|
||||||
}
|
}
|
||||||
if(API::wallRight() && mms->y != 0){
|
if(API::wallRight() && mms->y != 15){
|
||||||
maze->hwalls[mms->x][mms->y] = 1;
|
maze->hwalls[mms->x][mms->y] = 1;
|
||||||
API::setWall(mms->x, mms->y, 'n');
|
API::setWall(mms->x, mms->y, 'n');
|
||||||
}
|
}
|
||||||
|
|
@ -219,37 +234,41 @@ int main(int argc, char* argv[]) {
|
||||||
stats.x = 0;
|
stats.x = 0;
|
||||||
stats.y = 0;
|
stats.y = 0;
|
||||||
|
|
||||||
flood(&maze, &stats);
|
|
||||||
/*
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
locateWall(&maze, &stats);
|
||||||
|
flood(&maze, &stats);
|
||||||
follow(maze, &stats);
|
follow(maze, &stats);
|
||||||
API::moveForward();
|
|
||||||
updatepos(&stats);
|
|
||||||
if(maze.mhtn[stats.x][stats.y] == 0){
|
if(maze.mhtn[stats.x][stats.y] == 0){
|
||||||
log("Finish");
|
log("Finish");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
log(stats.x);
|
//log(stats.x);
|
||||||
log(stats.y);
|
//log(stats.y);
|
||||||
log("");
|
API::moveForward();
|
||||||
|
updatepos(&stats);
|
||||||
|
//log(maze.hwalls[stats.x][stats.y]);
|
||||||
}
|
}
|
||||||
*/
|
/*
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!API::wallLeft()) {
|
if (!API::wallLeft()) {
|
||||||
locateWall(&maze, &stats);
|
locateWall(&maze, &stats);
|
||||||
API::turnLeft();
|
API::turnLeft();
|
||||||
stats.turn(left);
|
stats.turn(left);
|
||||||
|
flood(&maze, &stats);
|
||||||
}
|
}
|
||||||
while (API::wallFront()) {
|
while (API::wallFront()) {
|
||||||
locateWall(&maze, &stats);
|
locateWall(&maze, &stats);
|
||||||
API::turnRight();
|
API::turnRight();
|
||||||
stats.turn(right);
|
stats.turn(right);
|
||||||
|
flood(&maze, &stats);
|
||||||
}
|
}
|
||||||
locateWall(&maze, &stats);
|
locateWall(&maze, &stats);
|
||||||
API::moveForward();
|
API::moveForward();
|
||||||
updatepos(&stats);
|
updatepos(&stats);
|
||||||
|
flood(&maze, &stats);
|
||||||
log(stats.x);
|
log(stats.x);
|
||||||
log(stats.y);
|
log(stats.y);
|
||||||
log("");
|
log("");
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user