156 lines
3.5 KiB
C++
156 lines
3.5 KiB
C++
#include <string>
|
|
#include <sys/types.h>
|
|
#include "api.h"
|
|
#include <queue>
|
|
|
|
enum turn{left = 0, right =1};
|
|
struct fill {
|
|
int x, y, z;
|
|
};
|
|
|
|
struct dirnode {
|
|
char value;
|
|
dirnode* next;
|
|
dirnode* prev;
|
|
dirnode(char val): value(val){};
|
|
};
|
|
|
|
struct maze { /* Distancias de Manhattan */
|
|
int vwalls[15][15];
|
|
int hwalls[15][15];
|
|
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;
|
|
|
|
class mmstats {
|
|
private:
|
|
dirnode* tail;
|
|
dirnode* head;
|
|
void addnode(char val) {
|
|
dirnode* temp = new dirnode(val);
|
|
if (head != nullptr) {
|
|
head->next = temp;
|
|
temp->prev = head;
|
|
temp->next = tail;
|
|
head = temp;
|
|
tail->prev = head;
|
|
}
|
|
|
|
if (head == nullptr) {
|
|
head = temp;
|
|
tail = temp;
|
|
}
|
|
}
|
|
public:
|
|
int y;
|
|
int x;
|
|
char dir;
|
|
mmstats(): tail(nullptr), head(nullptr) {
|
|
addnode('e');
|
|
addnode('s');
|
|
addnode('w');
|
|
addnode('n');
|
|
dir = 'n';
|
|
}
|
|
void turn(int opt) {
|
|
if (opt)
|
|
head = head->next;
|
|
else head = head->prev;
|
|
dir = head->value;
|
|
switch (dir) {
|
|
case 'n': log("Norte");
|
|
break;
|
|
case 'e': log("Este");
|
|
break;
|
|
case 's': log("Sur");
|
|
break;
|
|
case 'w': log("Oeste");
|
|
break;
|
|
}
|
|
}
|
|
|
|
}stats;
|
|
|
|
void flood(struct maze*, struct mmstats*);
|
|
|
|
void flood(struct maze* maze, struct mmstats* mmstats) {
|
|
const int w = 15; /* X lenght*/
|
|
int i, j;
|
|
int prevNum;
|
|
int marked[16][16];
|
|
std::queue<fill> myqueue;
|
|
|
|
myqueue.push({6,7,0});
|
|
myqueue.push({6,8,0});
|
|
myqueue.push({7,6,0});
|
|
myqueue.push({8,6,0});
|
|
|
|
myqueue.push({9,7,0});
|
|
myqueue.push({9,8,0});
|
|
myqueue.push({7,9,0});
|
|
myqueue.push({8,9,0});
|
|
|
|
marked[7][7] = 1; /* las celdas del centro tienen */
|
|
marked[7][8] = 1; /* un valor de cero porque son */
|
|
marked[8][7] = 1; /* la meta. */
|
|
marked[8][8] = 1;
|
|
|
|
API::setText(7, 7, 0);
|
|
API::setText(7, 8, 0);
|
|
API::setText(8, 7, 0);
|
|
API::setText(8, 8, 0);
|
|
|
|
while (myqueue.size()) {
|
|
i = myqueue.front().x;
|
|
j = myqueue.front().y;
|
|
prevNum = myqueue.front().z;
|
|
myqueue.pop();
|
|
|
|
if (mmstats->x == i && mmstats->y == j) {
|
|
log("flood end");
|
|
break;
|
|
}
|
|
|
|
if (i < 0 || i > w || j < 0 || j > w
|
|
|| marked[i][j]==1) {
|
|
continue;
|
|
}
|
|
else {
|
|
API::setText(i, j, prevNum+1);
|
|
maze->mhtn[i][j] = prevNum+1;
|
|
marked[i][j] = 1;
|
|
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});
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
log("Running...");
|
|
API::setColor(0, 0, 'G');
|
|
// API::setText(0, 0, "abc");
|
|
API::setWall(1, 0, 'e');
|
|
stats.x = 0;
|
|
stats.y = 0;
|
|
flood(&maze, &stats);
|
|
|
|
while (true) {
|
|
if (!API::wallLeft()) {
|
|
API::turnLeft();
|
|
stats.turn(left);
|
|
}
|
|
while (API::wallFront()) {
|
|
API::turnRight();
|
|
stats.turn(right);
|
|
}
|
|
API::moveForward();
|
|
}
|
|
}
|