initial commit
This commit is contained in:
parent
a0b9f25368
commit
54b28682b1
|
@ -0,0 +1,56 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"iostream": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"dfs.h": "c",
|
||||||
|
"eller.h": "c",
|
||||||
|
"stdio.h": "c",
|
||||||
|
"prim.h": "c",
|
||||||
|
"bfs.h": "c",
|
||||||
|
"water.h": "c",
|
||||||
|
"dijkstra.h": "c",
|
||||||
|
"labyrinth.h": "c"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
CC := gcc
|
||||||
|
SRC := src
|
||||||
|
OBJ := obj
|
||||||
|
|
||||||
|
TARGET := a.out
|
||||||
|
|
||||||
|
SRCS := $(shell find $(SRC) -name *.c)
|
||||||
|
FILES := $(notdir $(basename $(SRCS)))
|
||||||
|
OBJS := $(addprefix $(OBJ)/,$(addsuffix .o,$(FILES)))
|
||||||
|
|
||||||
|
FLAGS := -Werror -Wall
|
||||||
|
|
||||||
|
all: clear build
|
||||||
|
|
||||||
|
build: $(OBJS)
|
||||||
|
$(CC) $(FLAGS) $(OBJS)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
$(OBJ)/%.o: $(SRC)/%.c
|
||||||
|
$(CC) -Werror -Wall -c $< -o $@
|
||||||
|
|
||||||
|
only: build
|
||||||
|
|
||||||
|
clean: clear
|
||||||
|
-rm -f $(OBJS)
|
||||||
|
|
||||||
|
|
||||||
|
clear:
|
||||||
|
clear
|
|
@ -0,0 +1,55 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"iostream": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"dfs.h": "c",
|
||||||
|
"eller.h": "c",
|
||||||
|
"stdio.h": "c",
|
||||||
|
"prim.h": "c",
|
||||||
|
"bfs.h": "c",
|
||||||
|
"water.h": "c",
|
||||||
|
"dijkstra.h": "c"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
CC := gcc
|
||||||
|
SRC := src
|
||||||
|
OBJ := obj
|
||||||
|
|
||||||
|
TARGET := a.out
|
||||||
|
|
||||||
|
SRCS := $(shell find $(SRC) -name *.c)
|
||||||
|
FILES := $(notdir $(basename $(SRCS)))
|
||||||
|
OBJS := $(addprefix $(OBJ)/,$(addsuffix .o,$(FILES)))
|
||||||
|
|
||||||
|
FLAGS := -Werror -Wall
|
||||||
|
|
||||||
|
all: clear build
|
||||||
|
|
||||||
|
build: $(OBJS)
|
||||||
|
$(CC) $(FLAGS) $(OBJS)
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
$(OBJ)/%.o: $(SRC)/%.c
|
||||||
|
$(CC) -Werror -Wall -c $< -o $@
|
||||||
|
|
||||||
|
only: build
|
||||||
|
|
||||||
|
clean: clear
|
||||||
|
-rm -f $(OBJS)
|
||||||
|
|
||||||
|
|
||||||
|
clear:
|
||||||
|
clear
|
|
@ -0,0 +1,2 @@
|
||||||
|
- water
|
||||||
|
- emplace
|
Binary file not shown.
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef BFS_H
|
||||||
|
#define BFS_H
|
||||||
|
|
||||||
|
void bfs(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef BINARY_TREE_H
|
||||||
|
#define BINARY_TREE_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* binaryTree(int n, int m);
|
||||||
|
Labyrinth* binaryTreeShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef COOR_H
|
||||||
|
#define COOR_H
|
||||||
|
|
||||||
|
typedef struct coor{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} coor;
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef DEPTH_FIRST_H
|
||||||
|
#define DEPTH_FIRST_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* depthFirst(int n, int m);
|
||||||
|
Labyrinth* depthFirstShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef DFS_H
|
||||||
|
#define DFS_H
|
||||||
|
|
||||||
|
void dfs(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef DIJKSTRA_H
|
||||||
|
#define DIJKSTRA_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
void dijkstra(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef ELLER_H
|
||||||
|
#define ELLER_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* eller(int n, int m);
|
||||||
|
Labyrinth* ellerShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,49 @@
|
||||||
|
#ifndef LABYRINTH_H
|
||||||
|
#define LABYRINTH_H
|
||||||
|
|
||||||
|
#include "coor.h"
|
||||||
|
|
||||||
|
enum Color{
|
||||||
|
WHITE,
|
||||||
|
GREEN,
|
||||||
|
ORANGE,
|
||||||
|
BLUE
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Direction{
|
||||||
|
UP,
|
||||||
|
RIGHT,
|
||||||
|
DOWN,
|
||||||
|
LEFT
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct Node{
|
||||||
|
short south;
|
||||||
|
short east;
|
||||||
|
|
||||||
|
short visited;
|
||||||
|
int dp;
|
||||||
|
|
||||||
|
enum Color color;
|
||||||
|
enum Direction direction;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
typedef struct Labyrinth {
|
||||||
|
Node** at;
|
||||||
|
int x_size;
|
||||||
|
int y_size;
|
||||||
|
|
||||||
|
coor start;
|
||||||
|
coor end;
|
||||||
|
} Labyrinth;
|
||||||
|
|
||||||
|
Labyrinth* newLabyrinth(int n, int m);
|
||||||
|
void resetLabyrinth(Labyrinth* Labyrinth);
|
||||||
|
void removeLabyrinth(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
void find_path(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
void printLab(Labyrinth* labyrinth);
|
||||||
|
void printLabWith(Labyrinth* labyrinth, char wall, char floor, char corner);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef PRIM_H
|
||||||
|
#define PRIM_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* prim(int n, int m);
|
||||||
|
Labyrinth* primShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,40 @@
|
||||||
|
#ifndef PRIORITY_QUEUE_H
|
||||||
|
#define PRIORITY_QUEUE_H
|
||||||
|
|
||||||
|
#define PRIORITY_QUEUE_INIT_GROWTH_FACT 2
|
||||||
|
|
||||||
|
#define priority_queue(pq, compare) priority_queue* pq = newPriority_queue(compare);
|
||||||
|
|
||||||
|
typedef struct priority_queue{
|
||||||
|
void** at;
|
||||||
|
int capacity;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
int growthFactor;
|
||||||
|
|
||||||
|
void (*toString)();
|
||||||
|
bool (*compare)();
|
||||||
|
} priority_queue;
|
||||||
|
|
||||||
|
|
||||||
|
priority_queue* newPriority_queue(bool (*compare)());
|
||||||
|
|
||||||
|
void priority_queueResize(priority_queue* v, int capacity);
|
||||||
|
|
||||||
|
void priority_queueClear(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queuePush(priority_queue* pq, void* element);
|
||||||
|
|
||||||
|
void priority_queueFree(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queueShrink_to_fit(priority_queue* pq);
|
||||||
|
|
||||||
|
bool priority_queueIsEmpty(priority_queue* pq);
|
||||||
|
|
||||||
|
void* priority_queueTop(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queuePop(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queuePrint(priority_queue* pq);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,50 @@
|
||||||
|
#ifndef QUEUE_H
|
||||||
|
#define QUEUE_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macro to create a new queue.
|
||||||
|
*/
|
||||||
|
#define queue(q) queue* q = newQueue();
|
||||||
|
|
||||||
|
typedef struct queue_element{
|
||||||
|
void* next;
|
||||||
|
void* element;
|
||||||
|
} queue_element;
|
||||||
|
|
||||||
|
typedef struct queue{
|
||||||
|
|
||||||
|
int size; /* The number of at in use */
|
||||||
|
queue_element* front; /* pointer to the first element of the queue */
|
||||||
|
queue_element* back; /* pointer to the back element of the queue */
|
||||||
|
|
||||||
|
void (*toString)(); /* Pointer to toString function */
|
||||||
|
} queue;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Creates new queue.
|
||||||
|
*/
|
||||||
|
queue* newQueue();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Adds a new element to the queue.
|
||||||
|
*/
|
||||||
|
void queuePush(queue* q, void* element);
|
||||||
|
|
||||||
|
void queuePrint(queue* q);
|
||||||
|
|
||||||
|
void queuePop(queue* q);
|
||||||
|
|
||||||
|
bool queueIsEmpty(queue* q);
|
||||||
|
|
||||||
|
void queueMerge(queue* q1, queue* q2);
|
||||||
|
|
||||||
|
void queueClear(queue* q);
|
||||||
|
|
||||||
|
void queueFree(queue* q);
|
||||||
|
|
||||||
|
void queueEmplace(queue* q, void* element, size_t size);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef WATER_H
|
||||||
|
#define WATHER_H
|
||||||
|
|
||||||
|
void water(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,87 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/bfs.h"
|
||||||
|
#include "../include/queue.h"
|
||||||
|
#include "../include/coor.h"
|
||||||
|
|
||||||
|
void bfs(Labyrinth* labyrinth){
|
||||||
|
int i, j;
|
||||||
|
int arr[4] = {0, 1, 2, 3}, tmp, tp;
|
||||||
|
coor* cor = (coor*)malloc(sizeof(coor));
|
||||||
|
coor* test;
|
||||||
|
cor->x = labyrinth->start.x;
|
||||||
|
cor->y = labyrinth->start.y;
|
||||||
|
queue(q);
|
||||||
|
queuePush(q, cor);
|
||||||
|
|
||||||
|
while(!queueIsEmpty(q)){
|
||||||
|
test = (coor*)q->front->element;
|
||||||
|
i = test->x;
|
||||||
|
j = test->y;
|
||||||
|
queuePop(q);
|
||||||
|
|
||||||
|
if(i==labyrinth->end.x && j==labyrinth->end.y){
|
||||||
|
queueFree(q);
|
||||||
|
find_path(labyrinth);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i!=labyrinth->start.x || j!=labyrinth->start.y)
|
||||||
|
labyrinth->at[i][j].color = GREEN;
|
||||||
|
|
||||||
|
printLab(labyrinth);
|
||||||
|
|
||||||
|
labyrinth->at[i][j].visited = true;
|
||||||
|
|
||||||
|
|
||||||
|
for(int h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// goes in the direction if possible
|
||||||
|
for(int h=0; h<4; h++){
|
||||||
|
coor* cor2 = (coor*)malloc(sizeof(coor));
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].south && !labyrinth->at[i-1][j].visited){
|
||||||
|
if(i-1!=labyrinth->end.x && j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i-1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i-1][j].direction = UP;
|
||||||
|
cor2->x = i-1;
|
||||||
|
cor2->y = j;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<labyrinth->y_size-1 && labyrinth->at[i][j].east && !labyrinth->at[i][j+1].visited){
|
||||||
|
if(i!=labyrinth->end.x && j+1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j+1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j+1].direction = RIGHT;
|
||||||
|
cor2->x = i;
|
||||||
|
cor2->y = j+1;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<labyrinth->y_size-1 && labyrinth->at[i][j].south && !labyrinth->at[i+1][j].visited){
|
||||||
|
if(i+1!=labyrinth->end.x && j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i+1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i+1][j].direction = DOWN;
|
||||||
|
cor2->x = i+1;
|
||||||
|
cor2->y = j;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].east && !labyrinth->at[i][j-1].visited){
|
||||||
|
if(i!=labyrinth->end.x && j-1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j-1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j-1].direction = LEFT;
|
||||||
|
cor2->x = i;
|
||||||
|
cor2->y = j-1;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/binaryTree.h"
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* binaryTree_r(int n, int m, bool show);
|
||||||
|
|
||||||
|
Labyrinth* binaryTree(int n, int m){
|
||||||
|
return binaryTree_r(n, m, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* binaryTreeShow(int n, int m){
|
||||||
|
return binaryTree_r(n, m, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// creates a labyrinth using the binaryTree algorithm
|
||||||
|
Labyrinth* binaryTree_r(int n, int m, bool show){
|
||||||
|
int i, j, tmp;
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
labyrinth->at[0][0].east = 1;
|
||||||
|
labyrinth->at[0][0].south = 1;
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[0][0].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[0][0].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=1; i<n; i++){
|
||||||
|
labyrinth->at[i-1][0].south = 1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i-1][0].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i-1][0].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(j=1; j<m; j++){
|
||||||
|
labyrinth->at[0][j-1].east = 1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[0][j-1].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[0][j-1].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=1; i<n; i++){
|
||||||
|
for(j=1; j<m; j++){
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = rand()%2;
|
||||||
|
if(tmp)
|
||||||
|
labyrinth->at[i-1][j].south = 1;
|
||||||
|
else
|
||||||
|
labyrinth->at[i][j-1].east = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/depthFirst.h"
|
||||||
|
|
||||||
|
void depthFirst_r(int i, int j, Labyrinth* labyrinth, bool show);
|
||||||
|
|
||||||
|
// dfs search algorithm
|
||||||
|
Labyrinth* depthFirst(int n, int m){
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
depthFirst_r(rand()%n, rand()%m, labyrinth, false);
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* depthFirstShow(int n, int m){
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
depthFirst_r(rand()%n, rand()%m, labyrinth, true);
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ricursive function used by depthFirst
|
||||||
|
void depthFirst_r(int i, int j, Labyrinth* labyrinth, bool show){
|
||||||
|
labyrinth->at[i][j].visited = 1;
|
||||||
|
labyrinth->at[i][j].dp = -1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int arr[4] = {0, 1, 2, 3}, tmp, tp;
|
||||||
|
for(int h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int h=0; h<4; h++){
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && !labyrinth->at[i-1][j].visited){
|
||||||
|
labyrinth->at[i-1][j].south = 1;
|
||||||
|
depthFirst_r(i-1, j, labyrinth, show);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<labyrinth->y_size-1 && !labyrinth->at[i][j+1].visited){
|
||||||
|
labyrinth->at[i][j].east = 1;
|
||||||
|
depthFirst_r(i, j+1, labyrinth, show);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<labyrinth->x_size-1 && !labyrinth->at[i+1][j].visited){
|
||||||
|
labyrinth->at[i][j].south = 1;
|
||||||
|
depthFirst_r(i+1, j, labyrinth, show);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && !labyrinth->at[i][j-1].visited){
|
||||||
|
labyrinth->at[i][j-1].east = 1;
|
||||||
|
depthFirst_r(i, j-1, labyrinth, show);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/dfs.h"
|
||||||
|
|
||||||
|
short dfs_r(Labyrinth* labyrinth, int i, int j);
|
||||||
|
|
||||||
|
void dfs(Labyrinth* labyrinth){
|
||||||
|
dfs_r(labyrinth, labyrinth->start.x, labyrinth->start.y);
|
||||||
|
find_path(labyrinth);
|
||||||
|
}
|
||||||
|
|
||||||
|
short dfs_r(Labyrinth* labyrinth, int i, int j){
|
||||||
|
if(i==labyrinth->end.x && j==labyrinth->end.y)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if(i!=labyrinth->start.x || j!=labyrinth->start.y)
|
||||||
|
labyrinth->at[i][j].color = GREEN;
|
||||||
|
|
||||||
|
printLab(labyrinth);
|
||||||
|
|
||||||
|
labyrinth->at[i][j].visited = 1;
|
||||||
|
|
||||||
|
// randomly generates order of direction to try
|
||||||
|
int arr[4] = {0, 1, 2, 3}, tmp, tp;
|
||||||
|
for(int h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// goes in the direction if possible
|
||||||
|
for(int h=0; h<4; h++){
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].south && !labyrinth->at[i-1][j].visited){
|
||||||
|
labyrinth->at[i-1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i-1][j].direction = UP;
|
||||||
|
if(dfs_r(labyrinth, i-1, j))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<labyrinth->y_size-1 && labyrinth->at[i][j].east && !labyrinth->at[i][j+1].visited){
|
||||||
|
labyrinth->at[i][j+1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j+1].direction = RIGHT;
|
||||||
|
if(dfs_r(labyrinth, i, j+1))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<labyrinth->y_size-1 && labyrinth->at[i][j].south && !labyrinth->at[i+1][j].visited){
|
||||||
|
labyrinth->at[i+1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i+1][j].direction = DOWN;
|
||||||
|
if(dfs_r(labyrinth, i+1, j))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].east && !labyrinth->at[i][j-1].visited){
|
||||||
|
labyrinth->at[i][j-1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j-1].direction = LEFT;
|
||||||
|
if(dfs_r(labyrinth, i, j-1))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,117 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/dijkstra.h"
|
||||||
|
#include "../include/priority_queue.h"
|
||||||
|
|
||||||
|
typedef struct element{
|
||||||
|
int dist;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} element;
|
||||||
|
|
||||||
|
bool compare(element* e1, element* e2){
|
||||||
|
return e1->dist > e2->dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
void toString(element* e){
|
||||||
|
printf("(%d) %d %d\n", e->dist, e->x, e->y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dijkstra_r(Labyrinth* labyrinth, int i, int j, int dist, priority_queue* queue);
|
||||||
|
|
||||||
|
// dijkstra search algorithm
|
||||||
|
void dijkstra(Labyrinth* labyrinth){
|
||||||
|
priority_queue(queue, compare);
|
||||||
|
queue->toString = toString;
|
||||||
|
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = 0;
|
||||||
|
tmp->x = labyrinth->start.x;
|
||||||
|
tmp->y = labyrinth->start.y;
|
||||||
|
labyrinth->at[tmp->x][tmp->y].dp = 0;
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
int a, b, c;
|
||||||
|
|
||||||
|
while(!priority_queueIsEmpty(queue)){
|
||||||
|
a = ((element*)priority_queueTop(queue))->dist;
|
||||||
|
b = ((element*)priority_queueTop(queue))->x;
|
||||||
|
c = ((element*)priority_queueTop(queue))->y;
|
||||||
|
|
||||||
|
priority_queuePop(queue);
|
||||||
|
|
||||||
|
dijkstra_r(labyrinth, b, c, a, queue);
|
||||||
|
printLab(labyrinth);
|
||||||
|
}
|
||||||
|
priority_queueFree(queue);
|
||||||
|
|
||||||
|
find_path(labyrinth);
|
||||||
|
printf("test\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ricursive function used by dijkstra
|
||||||
|
void dijkstra_r(Labyrinth* labyrinth, int i, int j, int dist, priority_queue* queue){
|
||||||
|
|
||||||
|
if(i==labyrinth->end.x && j==labyrinth->end.y){
|
||||||
|
priority_queueClear(queue);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dist)
|
||||||
|
labyrinth->at[i][j].color = GREEN;
|
||||||
|
|
||||||
|
// adds all four directions to the priority_queue
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].south && labyrinth->at[i-1][j].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i-1;
|
||||||
|
tmp->y = j;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i-1!=labyrinth->end.x || j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i-1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i-1][j].direction = UP;
|
||||||
|
labyrinth->at[i-1][j].dp = dist+1;
|
||||||
|
}
|
||||||
|
if(j<labyrinth->y_size-1 && labyrinth->at[i][j].east && labyrinth->at[i][j+1].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i;
|
||||||
|
tmp->y = j+1;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i!=labyrinth->end.x || j+1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j+1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j+1].direction = RIGHT;
|
||||||
|
labyrinth->at[i][j+1].dp = dist+1;
|
||||||
|
}
|
||||||
|
if(i<labyrinth->x_size-1 && labyrinth->at[i][j].south && labyrinth->at[i+1][j].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i+1;
|
||||||
|
tmp->y = j;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i+1!=labyrinth->end.x || j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i+1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i+1][j].direction = DOWN;
|
||||||
|
labyrinth->at[i+1][j].dp = dist+1;
|
||||||
|
}
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].east && labyrinth->at[i][j-1].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i;
|
||||||
|
tmp->y = j-1;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i!=labyrinth->end.x || j-1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j-1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j-1].direction = LEFT;
|
||||||
|
labyrinth->at[i][j-1].dp = dist+1;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,137 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/eller.h"
|
||||||
|
#include "../include/queue.h"
|
||||||
|
|
||||||
|
#define ELLER_COST 2
|
||||||
|
|
||||||
|
Labyrinth* eller_(int n, int m, bool show);
|
||||||
|
|
||||||
|
Labyrinth* eller(int n, int m){
|
||||||
|
return eller_(n, m, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* ellerShow(int n, int m){
|
||||||
|
return eller_(n, m, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* eller_(int n, int m, bool show){
|
||||||
|
int i, j, h, tmp, tp;
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
int sets[m][m];
|
||||||
|
int setSizes[m];
|
||||||
|
|
||||||
|
queue(freeSets);
|
||||||
|
|
||||||
|
int arr[m];
|
||||||
|
for(i=0; i<m; i++){
|
||||||
|
int* s = (int*)malloc(sizeof(int));
|
||||||
|
*s = i;
|
||||||
|
queuePush(freeSets, s);
|
||||||
|
setSizes[i] = 0;
|
||||||
|
|
||||||
|
arr[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int down[m];
|
||||||
|
|
||||||
|
|
||||||
|
printLab(labyrinth);
|
||||||
|
|
||||||
|
for(i=0; i<n; i++){
|
||||||
|
// reset sets
|
||||||
|
for(j=0; j<m; j++){
|
||||||
|
setSizes[j] = 0;
|
||||||
|
down[j] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// gives cells with no set a set
|
||||||
|
for(j=0; j<m; j++){
|
||||||
|
if(labyrinth->at[i][j].dp < 0){
|
||||||
|
int* tmp = (int*)freeSets->front->element;
|
||||||
|
labyrinth->at[i][j].dp = *tmp;
|
||||||
|
queuePop(freeSets);
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = labyrinth->at[i][j].dp;
|
||||||
|
sets[tmp][setSizes[tmp]] = j;
|
||||||
|
setSizes[tmp]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomizes order
|
||||||
|
for(j=m; j>0; j--){
|
||||||
|
tmp = rand() % j;
|
||||||
|
tp = arr[tmp];
|
||||||
|
arr[tmp] = arr[j-1];
|
||||||
|
arr[j-1] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomly merge cells
|
||||||
|
for(j=0; j<m; j++){
|
||||||
|
tmp = arr[j];
|
||||||
|
if(tmp < m-1 && labyrinth->at[i][tmp].dp != labyrinth->at[i][tmp+1].dp && (rand()%ELLER_COST || i==n-1)){
|
||||||
|
labyrinth->at[i][tmp].east = 1;
|
||||||
|
|
||||||
|
tp = labyrinth->at[i][tmp].dp;
|
||||||
|
tmp = labyrinth->at[i][tmp+1].dp;
|
||||||
|
for(h=0; h<setSizes[tmp]; h++){
|
||||||
|
sets[tp][setSizes[tp]] = sets[tmp][h];
|
||||||
|
setSizes[tp]++;
|
||||||
|
labyrinth->at[i][sets[tmp][h]].dp = tp;
|
||||||
|
}
|
||||||
|
setSizes[tmp] = 0;
|
||||||
|
|
||||||
|
int* s = (int*)malloc(sizeof(int));
|
||||||
|
*s = tmp;
|
||||||
|
queuePush(freeSets, s);
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][tmp].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][tmp].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomly makes cells go down
|
||||||
|
for(j=0; j<m && i<n-1; j++){
|
||||||
|
// all but last cell
|
||||||
|
for(h=setSizes[j]; h>1; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = sets[j][tmp];
|
||||||
|
sets[j][tmp] = sets[j][h-1];
|
||||||
|
|
||||||
|
if(rand()%ELLER_COST){
|
||||||
|
labyrinth->at[i][tp].south = 1;
|
||||||
|
labyrinth->at[i+1][tp].dp = labyrinth->at[i][tp].dp;
|
||||||
|
down[j] = 1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][tp].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][tp].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// last cell
|
||||||
|
if(setSizes[j] && (!down[j] || rand()%ELLER_COST)){
|
||||||
|
labyrinth->at[i][sets[j][0]].south = 1;
|
||||||
|
labyrinth->at[i+1][sets[j][0]].dp = labyrinth->at[i][sets[j][0]].dp;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][sets[j][0]].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][sets[j][0]].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
queueFree(freeSets);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
|
@ -0,0 +1,149 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
|
||||||
|
void find_path_r(Labyrinth* labyrinth, int i, int j);
|
||||||
|
|
||||||
|
Labyrinth* newLabyrinth(int n, int m){
|
||||||
|
if(n<1 || m<1 || (n==1 && m==1)){
|
||||||
|
printf("COGLIONE\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* labyrinth = (Labyrinth* )malloc(sizeof(Labyrinth));
|
||||||
|
labyrinth->x_size = n;
|
||||||
|
labyrinth->y_size = m;
|
||||||
|
|
||||||
|
labyrinth->at = (Node** )malloc(n*sizeof(Node* ));
|
||||||
|
for(int i=0; i<m; i++)
|
||||||
|
labyrinth->at[i] = (Node *)malloc(m*sizeof(Node));
|
||||||
|
|
||||||
|
for(int i=0; i<n; i++){
|
||||||
|
for(int j=0; j<m; j++){
|
||||||
|
labyrinth->at[i][j].dp = -1;
|
||||||
|
labyrinth->at[i][j].east = 0;
|
||||||
|
labyrinth->at[i][j].south = 0;
|
||||||
|
labyrinth->at[i][j].visited = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
labyrinth->x_size = n;
|
||||||
|
labyrinth->y_size = m;
|
||||||
|
|
||||||
|
labyrinth->start.x = rand()%n;
|
||||||
|
labyrinth->start.y = rand()%m;
|
||||||
|
|
||||||
|
do{
|
||||||
|
labyrinth->end.x = rand()%n;
|
||||||
|
labyrinth->end.y = rand()%m;
|
||||||
|
}while(labyrinth->start.x == labyrinth->end.x && labyrinth->start.y == labyrinth->end.y);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
||||||
|
|
||||||
|
void resetLabyrinth(Labyrinth* labyrinth){
|
||||||
|
for(int i=0; i<labyrinth->x_size; i++){
|
||||||
|
for(int j=0; j<labyrinth->y_size; j++){
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
labyrinth->at[i][j].visited = 0;
|
||||||
|
labyrinth->at[i][j].dp = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
labyrinth->at[labyrinth->start.x][labyrinth->start.y].color = BLUE;
|
||||||
|
labyrinth->at[labyrinth->end.x][labyrinth->end.y].color = BLUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeLabyrinth(Labyrinth* labyrinth){
|
||||||
|
for(int i=0; i<labyrinth->x_size; i++)
|
||||||
|
free(labyrinth->at[i]);
|
||||||
|
free(labyrinth->at);
|
||||||
|
free(labyrinth);
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_path(Labyrinth* labyrinth){
|
||||||
|
find_path_r(labyrinth, labyrinth->end.x, labyrinth->end.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_path_r(Labyrinth* labyrinth, int i, int j){
|
||||||
|
labyrinth->at[i][j].color = BLUE;
|
||||||
|
// printLab(labyrinth);
|
||||||
|
|
||||||
|
if(i==labyrinth->start.x && j==labyrinth->start.y)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(labyrinth->at[i][j].direction == UP)
|
||||||
|
find_path_r(labyrinth, i+1, j);
|
||||||
|
else if(labyrinth->at[i][j].direction == RIGHT)
|
||||||
|
find_path_r(labyrinth, i, j-1);
|
||||||
|
else if(labyrinth->at[i][j].direction == DOWN)
|
||||||
|
find_path_r(labyrinth, i-1, j);
|
||||||
|
else if(labyrinth->at[i][j].direction == LEFT)
|
||||||
|
find_path_r(labyrinth, i, j+1);
|
||||||
|
else{
|
||||||
|
printf("ERROR IN FIND_PATH\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printLab(Labyrinth* labyrinth){
|
||||||
|
printLabWith(labyrinth, '|', '-', '+');
|
||||||
|
}
|
||||||
|
|
||||||
|
void printLabWith(Labyrinth* labyrinth, char wall, char floor, char corner){
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
system("clear");
|
||||||
|
|
||||||
|
for(j=0; j<labyrinth->y_size; j++){
|
||||||
|
printf("%c%c", corner, floor);
|
||||||
|
}
|
||||||
|
printf("%c\n", corner);
|
||||||
|
|
||||||
|
for(i=0; i<labyrinth->x_size; i++){
|
||||||
|
printf("%c", wall);
|
||||||
|
|
||||||
|
for(j=0; j<labyrinth->y_size; j++){
|
||||||
|
if(labyrinth->at[i][j].color == BLUE)
|
||||||
|
printf("\033[104m \033[49m");
|
||||||
|
else if(labyrinth->at[i][j].color == GREEN)
|
||||||
|
printf("\033[102m \033[49m");
|
||||||
|
else if(labyrinth->at[i][j].color == ORANGE)
|
||||||
|
printf("\033[103m \033[49m");
|
||||||
|
else
|
||||||
|
printf(" ");
|
||||||
|
|
||||||
|
if(labyrinth->at[i][j].east && j<labyrinth->y_size-1){
|
||||||
|
if(labyrinth->at[i][j+1].color == BLUE && labyrinth->at[i][j].color == BLUE)
|
||||||
|
printf("\033[104m \033[49m");
|
||||||
|
else if((labyrinth->at[i][j+1].color == GREEN || labyrinth->at[i][j+1].color == BLUE) && (labyrinth->at[i][j].color == GREEN || labyrinth->at[i][j].color == BLUE))
|
||||||
|
printf("\033[102m \033[49m");
|
||||||
|
else if(!(labyrinth->at[i][j+1].color == WHITE) && !(labyrinth->at[i][j].color == WHITE))
|
||||||
|
printf("\033[103m \033[49m");
|
||||||
|
else
|
||||||
|
printf(" ");
|
||||||
|
}else
|
||||||
|
printf("%c", wall);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
for(j=0; j<labyrinth->y_size; j++){
|
||||||
|
printf("%c", corner);
|
||||||
|
if(labyrinth->at[i][j].south && i<labyrinth->x_size-1){
|
||||||
|
if(labyrinth->at[i][j].color == BLUE && labyrinth->at[i+1][j].color == BLUE)
|
||||||
|
printf("\033[104m \033[49m");
|
||||||
|
else if((labyrinth->at[i][j].color == GREEN || labyrinth->at[i][j].color == BLUE) && (labyrinth->at[i+1][j].color == GREEN || labyrinth->at[i+1][j].color == BLUE))
|
||||||
|
printf("\033[102m \033[49m");
|
||||||
|
else if(!(labyrinth->at[i][j].color == WHITE) && !(labyrinth->at[i+1][j].color == WHITE))
|
||||||
|
printf("\033[103m \033[49m");
|
||||||
|
else
|
||||||
|
printf(" ");
|
||||||
|
}else
|
||||||
|
printf("%c", floor);
|
||||||
|
}
|
||||||
|
printf("%c\n", corner);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
usleep(50000);
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "../include/dijkstra.h"
|
||||||
|
#include "../include/dfs.h"
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/binaryTree.h"
|
||||||
|
#include "../include/depthFirst.h"
|
||||||
|
#include "../include/prim.h"
|
||||||
|
#include "../include/eller.h"
|
||||||
|
#include "../include/water.h"
|
||||||
|
#include "../include/bfs.h"
|
||||||
|
|
||||||
|
#define N 15
|
||||||
|
#define M 15
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
srand(time(0));
|
||||||
|
|
||||||
|
int n = N, m = M;
|
||||||
|
|
||||||
|
Labyrinth* labyrinth1 = prim(n, m);
|
||||||
|
|
||||||
|
dijkstra(labyrinth1);
|
||||||
|
printLab(labyrinth1);
|
||||||
|
resetLabyrinth(labyrinth1);
|
||||||
|
|
||||||
|
|
||||||
|
removeLabyrinth(labyrinth1);
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/coor.h"
|
||||||
|
#include "../include/prim.h"
|
||||||
|
|
||||||
|
Labyrinth* prim_(int n, int m, bool show);
|
||||||
|
|
||||||
|
Labyrinth* prim(int n, int m){
|
||||||
|
return prim_(n, m, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* primShow(int n, int m){
|
||||||
|
return prim_(n, m, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// creates a labyrinth using prim's algorithm
|
||||||
|
Labyrinth* prim_(int n, int m, bool show){
|
||||||
|
int i, j, h, tmp, tp;
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
int arr[4] = {0, 1, 2, 3};
|
||||||
|
|
||||||
|
coor frontier[n*m];
|
||||||
|
coor current = {.x = rand()%n, .y = rand()%m};
|
||||||
|
i = current.x;
|
||||||
|
j = current.y;
|
||||||
|
int frontierSize = 0;
|
||||||
|
|
||||||
|
do{
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
labyrinth->at[i][j].visited = 1;
|
||||||
|
|
||||||
|
// adds frontier cells
|
||||||
|
if(i > 0 && labyrinth->at[i-1][j].dp < 0){
|
||||||
|
frontier[frontierSize].x = i-1;
|
||||||
|
frontier[frontierSize].y = j;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i-1][j].dp = 1;
|
||||||
|
}
|
||||||
|
if(j < m-1 && labyrinth->at[i][j+1].dp < 0){
|
||||||
|
frontier[frontierSize].x = i;
|
||||||
|
frontier[frontierSize].y = j+1;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i][j+1].dp = 1;
|
||||||
|
}
|
||||||
|
if(i < n-1 && labyrinth->at[i+1][j].dp < 0){
|
||||||
|
frontier[frontierSize].x = i+1;
|
||||||
|
frontier[frontierSize].y = j;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i+1][j].dp = 1;
|
||||||
|
}
|
||||||
|
if(j > 0 && labyrinth->at[i][j-1].dp < 0){
|
||||||
|
frontier[frontierSize].x = i;
|
||||||
|
frontier[frontierSize].y = j-1;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i][j-1].dp = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomly selects a frontier cell
|
||||||
|
tmp = rand()%frontierSize;
|
||||||
|
current = frontier[tmp];
|
||||||
|
frontier[tmp] = frontier[frontierSize-1];
|
||||||
|
frontierSize--;
|
||||||
|
|
||||||
|
i = current.x;
|
||||||
|
j = current.y;
|
||||||
|
|
||||||
|
// randomly joins the selected cell to another cell
|
||||||
|
for(h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = 1;
|
||||||
|
for(h=0; h<4 && tmp; h++){
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].visited){
|
||||||
|
labyrinth->at[i-1][j].south = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<m-1 && labyrinth->at[i][j+1].visited){
|
||||||
|
labyrinth->at[i][j].east = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<n-1 && labyrinth->at[i+1][j].visited){
|
||||||
|
labyrinth->at[i][j].south = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].visited){
|
||||||
|
labyrinth->at[i][j-1].east = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}while(frontierSize);
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
|
@ -0,0 +1,127 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/priority_queue.h"
|
||||||
|
|
||||||
|
|
||||||
|
void priority_queueFixheap(priority_queue* q, int i);
|
||||||
|
void priority_queueHeapify(priority_queue* queue, int i);
|
||||||
|
void priority_queueSwap(void** a, void** b);
|
||||||
|
|
||||||
|
priority_queue* newPriority_queue(bool (*compare)()){
|
||||||
|
priority_queue* pq = (priority_queue*)malloc(sizeof(priority_queue));
|
||||||
|
pq->size = 0;
|
||||||
|
pq->capacity = 0;
|
||||||
|
pq->growthFactor = PRIORITY_QUEUE_INIT_GROWTH_FACT;
|
||||||
|
|
||||||
|
pq->compare = compare;
|
||||||
|
pq->toString = NULL;
|
||||||
|
|
||||||
|
return pq;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queuePush(priority_queue* pq, void* element){
|
||||||
|
if(pq->size == pq->capacity){
|
||||||
|
if(pq->capacity)
|
||||||
|
priority_queueResize(pq, pq->capacity * pq->growthFactor);
|
||||||
|
else
|
||||||
|
priority_queueResize(pq, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pq->at[pq->size] = element;
|
||||||
|
pq->size++;
|
||||||
|
|
||||||
|
priority_queueFixheap(pq, pq->size-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueFixheap(priority_queue* pq, int i){
|
||||||
|
if(!i)
|
||||||
|
return;
|
||||||
|
if(pq->compare(pq->at[(i-1)/2], pq->at[i]))
|
||||||
|
priority_queueSwap(pq->at[i], pq->at[(i-1)/2]);
|
||||||
|
|
||||||
|
priority_queueFixheap(pq, (i-1)/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queuePop(priority_queue* pq){
|
||||||
|
if(!pq->size){
|
||||||
|
fprintf(stderr, "POP ON EMPTY PRIORITY_QUEUE\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
priority_queueSwap(&pq->at[0], &pq->at[pq->size-1]);
|
||||||
|
free(pq->at[pq->size-1]);
|
||||||
|
pq->size--;
|
||||||
|
priority_queueHeapify(pq, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueHeapify(priority_queue* pq, int i){
|
||||||
|
if(i >= (pq->size)/2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int minson;
|
||||||
|
if(pq->size-1 == 2*i+1 || !pq->compare(pq->at[2*i+1], pq->at[2*i+2]))
|
||||||
|
minson = 2*i+1;
|
||||||
|
else
|
||||||
|
minson = 2*i+2;
|
||||||
|
|
||||||
|
if(pq->compare(pq->at[i], pq->at[minson])){
|
||||||
|
priority_queueSwap(&pq->at[i], &pq->at[minson]);
|
||||||
|
priority_queueHeapify(pq, minson);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueSwap(void** a, void** b){
|
||||||
|
void* tmp = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueResize(priority_queue* pq, int capacity){
|
||||||
|
for(int i=pq->size; i>capacity; i--)
|
||||||
|
free(pq->at[i-1]);
|
||||||
|
|
||||||
|
void **at = realloc(pq->at, sizeof(void *) * capacity);
|
||||||
|
pq->at = at;
|
||||||
|
pq->capacity = capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueClear(priority_queue* pq){
|
||||||
|
priority_queueResize(pq, 0);
|
||||||
|
pq->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueFree(priority_queue* pq){
|
||||||
|
priority_queueClear(pq);
|
||||||
|
free(pq->at);
|
||||||
|
free(pq);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueShrink_to_fit(priority_queue* pq){
|
||||||
|
priority_queueResize(pq, pq->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool priority_queueIsEmpty(priority_queue* pq){
|
||||||
|
return pq->size > 0 ? false : true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* priority_queueTop(priority_queue* pq){
|
||||||
|
if(!pq->size){
|
||||||
|
fprintf(stderr, "PRIORITY_QUEUE IS EMPTY\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pq->at[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queuePrint(priority_queue* pq){
|
||||||
|
if(pq->toString == NULL){
|
||||||
|
fprintf(stderr, "TOSTRING NOT DEFINED\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i=0; i<pq->size; i++)
|
||||||
|
pq->toString(pq->at[i]);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../include/queue.h"
|
||||||
|
|
||||||
|
queue* newQueue(){
|
||||||
|
queue* tmp = (queue*)malloc(sizeof(queue));
|
||||||
|
tmp->size = 0;
|
||||||
|
|
||||||
|
tmp->toString = NULL;
|
||||||
|
tmp->front = NULL;
|
||||||
|
tmp->back = NULL;
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queuePush(queue* q, void* element){
|
||||||
|
queue_element* qel = (queue_element*)malloc(sizeof(queue_element));
|
||||||
|
qel->element = element;
|
||||||
|
qel->next = NULL;
|
||||||
|
|
||||||
|
if(queueIsEmpty(q)){
|
||||||
|
q->front = qel;
|
||||||
|
q->back = qel;
|
||||||
|
}else{
|
||||||
|
q->back->next = qel;
|
||||||
|
q->back = qel;
|
||||||
|
}
|
||||||
|
|
||||||
|
q->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queuePrint(queue* q){
|
||||||
|
if(q->toString == NULL){
|
||||||
|
fprintf(stderr, "TOSTRING NOT DEFINED\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_element* tmp = q->front;
|
||||||
|
while(tmp){
|
||||||
|
q->toString(tmp->element);
|
||||||
|
tmp = tmp->next;
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void queuePop(queue* q){
|
||||||
|
if(queueIsEmpty(q)){
|
||||||
|
fprintf(stderr, "QUEUE IS EMTPY\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_element* tmp = q->front->next;
|
||||||
|
free(q->front->element);
|
||||||
|
free(q->front);
|
||||||
|
q->front = tmp;
|
||||||
|
q->size--;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool queueIsEmpty(queue* q){
|
||||||
|
return q->size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueMerge(queue* q1, queue* q2){
|
||||||
|
q1->back->next = q2->front;
|
||||||
|
q1->size += q2->size;
|
||||||
|
q2->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueClear(queue* q){
|
||||||
|
queue_element* tmp = q->front, *tp;
|
||||||
|
while(tmp){
|
||||||
|
tp = tmp->next;
|
||||||
|
// free(tmp->element);
|
||||||
|
free(tmp);
|
||||||
|
tmp = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
q->front = NULL;
|
||||||
|
q->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueFree(queue* q){
|
||||||
|
queueClear(q);
|
||||||
|
free(q);
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/water.h"
|
||||||
|
#include "../include/priority_queue.h"
|
||||||
|
|
||||||
|
bool compare(coor* c1, coor* c2){
|
||||||
|
return c1->x < c2->x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void water(Labyrinth *labyrinth){
|
||||||
|
int i, j, h, n = labyrinth->x_size, m = labyrinth->y_size;
|
||||||
|
|
||||||
|
priority_queue(pq, compare); // DA METTERE RBTREE (mai)
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef BFS_H
|
||||||
|
#define BFS_H
|
||||||
|
|
||||||
|
void bfs(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef BINARY_TREE_H
|
||||||
|
#define BINARY_TREE_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* binaryTree(int n, int m);
|
||||||
|
Labyrinth* binaryTreeShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef COOR_H
|
||||||
|
#define COOR_H
|
||||||
|
|
||||||
|
typedef struct coor{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} coor;
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef DEPTH_FIRST_H
|
||||||
|
#define DEPTH_FIRST_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* depthFirst(int n, int m);
|
||||||
|
Labyrinth* depthFirstShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef DFS_H
|
||||||
|
#define DFS_H
|
||||||
|
|
||||||
|
void dfs(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef DIJKSTRA_H
|
||||||
|
#define DIJKSTRA_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
void dijkstra(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef ELLER_H
|
||||||
|
#define ELLER_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* eller(int n, int m);
|
||||||
|
Labyrinth* ellerShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,49 @@
|
||||||
|
#ifndef LABYRINTH_H
|
||||||
|
#define LABYRINTH_H
|
||||||
|
|
||||||
|
#include "coor.h"
|
||||||
|
|
||||||
|
enum Color{
|
||||||
|
WHITE,
|
||||||
|
GREEN,
|
||||||
|
ORANGE,
|
||||||
|
BLUE
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Direction{
|
||||||
|
UP,
|
||||||
|
RIGHT,
|
||||||
|
DOWN,
|
||||||
|
LEFT
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct Node{
|
||||||
|
short south;
|
||||||
|
short east;
|
||||||
|
|
||||||
|
short visited;
|
||||||
|
int dp;
|
||||||
|
|
||||||
|
enum Color color;
|
||||||
|
enum Direction direction;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
typedef struct Labyrinth {
|
||||||
|
Node** at;
|
||||||
|
int x_size;
|
||||||
|
int y_size;
|
||||||
|
|
||||||
|
coor start;
|
||||||
|
coor end;
|
||||||
|
} Labyrinth;
|
||||||
|
|
||||||
|
Labyrinth* newLabyrinth(int n, int m);
|
||||||
|
void resetLabyrinth(Labyrinth* Labyrinth);
|
||||||
|
void removeLabyrinth(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
void find_path(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
void printLab(Labyrinth* labyrinth);
|
||||||
|
void printLabWith(Labyrinth* labyrinth, char wall, char floor, char corner);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef PRIM_H
|
||||||
|
#define PRIM_H
|
||||||
|
|
||||||
|
#include "labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* prim(int n, int m);
|
||||||
|
Labyrinth* primShow(int n, int m);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,40 @@
|
||||||
|
#ifndef PRIORITY_QUEUE_H
|
||||||
|
#define PRIORITY_QUEUE_H
|
||||||
|
|
||||||
|
#define PRIORITY_QUEUE_INIT_GROWTH_FACT 2
|
||||||
|
|
||||||
|
#define priority_queue(pq, compare) priority_queue* pq = newPriority_queue(compare);
|
||||||
|
|
||||||
|
typedef struct priority_queue{
|
||||||
|
void** at;
|
||||||
|
int capacity;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
int growthFactor;
|
||||||
|
|
||||||
|
void (*toString)();
|
||||||
|
bool (*compare)();
|
||||||
|
} priority_queue;
|
||||||
|
|
||||||
|
|
||||||
|
priority_queue* newPriority_queue(bool (*compare)());
|
||||||
|
|
||||||
|
void priority_queueResize(priority_queue* v, int capacity);
|
||||||
|
|
||||||
|
void priority_queueClear(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queuePush(priority_queue* pq, void* element);
|
||||||
|
|
||||||
|
void priority_queueFree(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queueShrink_to_fit(priority_queue* pq);
|
||||||
|
|
||||||
|
bool priority_queueIsEmpty(priority_queue* pq);
|
||||||
|
|
||||||
|
void* priority_queueTop(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queuePop(priority_queue* pq);
|
||||||
|
|
||||||
|
void priority_queuePrint(priority_queue* pq);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,50 @@
|
||||||
|
#ifndef QUEUE_H
|
||||||
|
#define QUEUE_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macro to create a new queue.
|
||||||
|
*/
|
||||||
|
#define queue(q) queue* q = newQueue();
|
||||||
|
|
||||||
|
typedef struct queue_element{
|
||||||
|
void* next;
|
||||||
|
void* element;
|
||||||
|
} queue_element;
|
||||||
|
|
||||||
|
typedef struct queue{
|
||||||
|
|
||||||
|
int size; /* The number of at in use */
|
||||||
|
queue_element* front; /* pointer to the first element of the queue */
|
||||||
|
queue_element* back; /* pointer to the back element of the queue */
|
||||||
|
|
||||||
|
void (*toString)(); /* Pointer to toString function */
|
||||||
|
} queue;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Creates new queue.
|
||||||
|
*/
|
||||||
|
queue* newQueue();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Adds a new element to the queue.
|
||||||
|
*/
|
||||||
|
void queuePush(queue* q, void* element);
|
||||||
|
|
||||||
|
void queuePrint(queue* q);
|
||||||
|
|
||||||
|
void queuePop(queue* q);
|
||||||
|
|
||||||
|
bool queueIsEmpty(queue* q);
|
||||||
|
|
||||||
|
void queueMerge(queue* q1, queue* q2);
|
||||||
|
|
||||||
|
void queueClear(queue* q);
|
||||||
|
|
||||||
|
void queueFree(queue* q);
|
||||||
|
|
||||||
|
void* queueTop(queue* q);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef WATER_H
|
||||||
|
#define WATHER_H
|
||||||
|
|
||||||
|
void water(Labyrinth* labyrinth);
|
||||||
|
|
||||||
|
#endif
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,87 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/bfs.h"
|
||||||
|
#include "../include/queue.h"
|
||||||
|
#include "../include/coor.h"
|
||||||
|
|
||||||
|
void bfs(Labyrinth* labyrinth){
|
||||||
|
int i, j;
|
||||||
|
int arr[4] = {0, 1, 2, 3}, tmp, tp;
|
||||||
|
coor* cor = (coor*)malloc(sizeof(coor));
|
||||||
|
coor* test;
|
||||||
|
cor->x = labyrinth->start.x;
|
||||||
|
cor->y = labyrinth->start.y;
|
||||||
|
queue(q);
|
||||||
|
queuePush(q, cor);
|
||||||
|
|
||||||
|
while(!queueIsEmpty(q)){
|
||||||
|
test = (coor*)q->front->element;
|
||||||
|
i = test->x;
|
||||||
|
j = test->y;
|
||||||
|
queuePop(q);
|
||||||
|
|
||||||
|
if(i==labyrinth->end.x && j==labyrinth->end.y){
|
||||||
|
queueFree(q);
|
||||||
|
find_path(labyrinth);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i!=labyrinth->start.x || j!=labyrinth->start.y)
|
||||||
|
labyrinth->at[i][j].color = GREEN;
|
||||||
|
|
||||||
|
printLab(labyrinth);
|
||||||
|
|
||||||
|
labyrinth->at[i][j].visited = true;
|
||||||
|
|
||||||
|
|
||||||
|
for(int h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// goes in the direction if possible
|
||||||
|
for(int h=0; h<4; h++){
|
||||||
|
coor* cor2 = (coor*)malloc(sizeof(coor));
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].south && !labyrinth->at[i-1][j].visited){
|
||||||
|
if(i-1!=labyrinth->end.x && j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i-1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i-1][j].direction = UP;
|
||||||
|
cor2->x = i-1;
|
||||||
|
cor2->y = j;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<labyrinth->y_size-1 && labyrinth->at[i][j].east && !labyrinth->at[i][j+1].visited){
|
||||||
|
if(i!=labyrinth->end.x && j+1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j+1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j+1].direction = RIGHT;
|
||||||
|
cor2->x = i;
|
||||||
|
cor2->y = j+1;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<labyrinth->y_size-1 && labyrinth->at[i][j].south && !labyrinth->at[i+1][j].visited){
|
||||||
|
if(i+1!=labyrinth->end.x && j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i+1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i+1][j].direction = DOWN;
|
||||||
|
cor2->x = i+1;
|
||||||
|
cor2->y = j;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].east && !labyrinth->at[i][j-1].visited){
|
||||||
|
if(i!=labyrinth->end.x && j-1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j-1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j-1].direction = LEFT;
|
||||||
|
cor2->x = i;
|
||||||
|
cor2->y = j-1;
|
||||||
|
queuePush(q, cor2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/binaryTree.h"
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
|
||||||
|
Labyrinth* binaryTree_r(int n, int m, bool show);
|
||||||
|
|
||||||
|
Labyrinth* binaryTree(int n, int m){
|
||||||
|
return binaryTree_r(n, m, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* binaryTreeShow(int n, int m){
|
||||||
|
return binaryTree_r(n, m, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// creates a labyrinth using the binaryTree algorithm
|
||||||
|
Labyrinth* binaryTree_r(int n, int m, bool show){
|
||||||
|
int i, j, tmp;
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
labyrinth->at[0][0].east = 1;
|
||||||
|
labyrinth->at[0][0].south = 1;
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[0][0].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[0][0].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=1; i<n; i++){
|
||||||
|
labyrinth->at[i-1][0].south = 1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i-1][0].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i-1][0].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(j=1; j<m; j++){
|
||||||
|
labyrinth->at[0][j-1].east = 1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[0][j-1].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[0][j-1].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=1; i<n; i++){
|
||||||
|
for(j=1; j<m; j++){
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = rand()%2;
|
||||||
|
if(tmp)
|
||||||
|
labyrinth->at[i-1][j].south = 1;
|
||||||
|
else
|
||||||
|
labyrinth->at[i][j-1].east = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/depthFirst.h"
|
||||||
|
|
||||||
|
void depthFirst_r(int i, int j, Labyrinth* labyrinth, bool show);
|
||||||
|
|
||||||
|
// dfs search algorithm
|
||||||
|
Labyrinth* depthFirst(int n, int m){
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
depthFirst_r(rand()%n, rand()%m, labyrinth, false);
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* depthFirstShow(int n, int m){
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
depthFirst_r(rand()%n, rand()%m, labyrinth, true);
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ricursive function used by depthFirst
|
||||||
|
void depthFirst_r(int i, int j, Labyrinth* labyrinth, bool show){
|
||||||
|
labyrinth->at[i][j].visited = 1;
|
||||||
|
labyrinth->at[i][j].dp = -1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int arr[4] = {0, 1, 2, 3}, tmp, tp;
|
||||||
|
for(int h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int h=0; h<4; h++){
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && !labyrinth->at[i-1][j].visited){
|
||||||
|
labyrinth->at[i-1][j].south = 1;
|
||||||
|
depthFirst_r(i-1, j, labyrinth, show);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<labyrinth->y_size-1 && !labyrinth->at[i][j+1].visited){
|
||||||
|
labyrinth->at[i][j].east = 1;
|
||||||
|
depthFirst_r(i, j+1, labyrinth, show);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<labyrinth->x_size-1 && !labyrinth->at[i+1][j].visited){
|
||||||
|
labyrinth->at[i][j].south = 1;
|
||||||
|
depthFirst_r(i+1, j, labyrinth, show);
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && !labyrinth->at[i][j-1].visited){
|
||||||
|
labyrinth->at[i][j-1].east = 1;
|
||||||
|
depthFirst_r(i, j-1, labyrinth, show);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/dfs.h"
|
||||||
|
|
||||||
|
short dfs_r(Labyrinth* labyrinth, int i, int j);
|
||||||
|
|
||||||
|
void dfs(Labyrinth* labyrinth){
|
||||||
|
dfs_r(labyrinth, labyrinth->start.x, labyrinth->start.y);
|
||||||
|
find_path(labyrinth);
|
||||||
|
}
|
||||||
|
|
||||||
|
short dfs_r(Labyrinth* labyrinth, int i, int j){
|
||||||
|
if(i==labyrinth->end.x && j==labyrinth->end.y)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if(i!=labyrinth->start.x || j!=labyrinth->start.y)
|
||||||
|
labyrinth->at[i][j].color = GREEN;
|
||||||
|
|
||||||
|
printLab(labyrinth);
|
||||||
|
|
||||||
|
labyrinth->at[i][j].visited = 1;
|
||||||
|
|
||||||
|
// randomly generates order of direction to try
|
||||||
|
int arr[4] = {0, 1, 2, 3}, tmp, tp;
|
||||||
|
for(int h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// goes in the direction if possible
|
||||||
|
for(int h=0; h<4; h++){
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].south && !labyrinth->at[i-1][j].visited){
|
||||||
|
labyrinth->at[i-1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i-1][j].direction = UP;
|
||||||
|
if(dfs_r(labyrinth, i-1, j))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<labyrinth->y_size-1 && labyrinth->at[i][j].east && !labyrinth->at[i][j+1].visited){
|
||||||
|
labyrinth->at[i][j+1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j+1].direction = RIGHT;
|
||||||
|
if(dfs_r(labyrinth, i, j+1))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<labyrinth->y_size-1 && labyrinth->at[i][j].south && !labyrinth->at[i+1][j].visited){
|
||||||
|
labyrinth->at[i+1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i+1][j].direction = DOWN;
|
||||||
|
if(dfs_r(labyrinth, i+1, j))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].east && !labyrinth->at[i][j-1].visited){
|
||||||
|
labyrinth->at[i][j-1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j-1].direction = LEFT;
|
||||||
|
if(dfs_r(labyrinth, i, j-1))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,117 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/dijkstra.h"
|
||||||
|
#include "../include/priority_queue.h"
|
||||||
|
|
||||||
|
typedef struct element{
|
||||||
|
int dist;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} element;
|
||||||
|
|
||||||
|
bool compare_element(element* e1, element* e2){
|
||||||
|
return e1->dist > e2->dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
void toString(element* e){
|
||||||
|
printf("(%d) %d %d\n", e->dist, e->x, e->y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dijkstra_r(Labyrinth* labyrinth, int i, int j, int dist, priority_queue* queue);
|
||||||
|
|
||||||
|
// dijkstra search algorithm
|
||||||
|
void dijkstra(Labyrinth* labyrinth){
|
||||||
|
priority_queue(queue, compare_element);
|
||||||
|
queue->toString = toString;
|
||||||
|
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = 0;
|
||||||
|
tmp->x = labyrinth->start.x;
|
||||||
|
tmp->y = labyrinth->start.y;
|
||||||
|
labyrinth->at[tmp->x][tmp->y].dp = 0;
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
int a, b, c;
|
||||||
|
|
||||||
|
while(!priority_queueIsEmpty(queue)){
|
||||||
|
a = ((element*)priority_queueTop(queue))->dist;
|
||||||
|
b = ((element*)priority_queueTop(queue))->x;
|
||||||
|
c = ((element*)priority_queueTop(queue))->y;
|
||||||
|
|
||||||
|
priority_queuePop(queue);
|
||||||
|
|
||||||
|
dijkstra_r(labyrinth, b, c, a, queue);
|
||||||
|
printLab(labyrinth);
|
||||||
|
}
|
||||||
|
priority_queueFree(queue);
|
||||||
|
|
||||||
|
find_path(labyrinth);
|
||||||
|
printf("test\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ricursive function used by dijkstra
|
||||||
|
void dijkstra_r(Labyrinth* labyrinth, int i, int j, int dist, priority_queue* queue){
|
||||||
|
|
||||||
|
if(i==labyrinth->end.x && j==labyrinth->end.y){
|
||||||
|
priority_queueClear(queue);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(dist)
|
||||||
|
labyrinth->at[i][j].color = GREEN;
|
||||||
|
|
||||||
|
// adds all four directions to the priority_queue
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].south && labyrinth->at[i-1][j].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i-1;
|
||||||
|
tmp->y = j;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i-1!=labyrinth->end.x || j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i-1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i-1][j].direction = UP;
|
||||||
|
labyrinth->at[i-1][j].dp = dist+1;
|
||||||
|
}
|
||||||
|
if(j<labyrinth->y_size-1 && labyrinth->at[i][j].east && labyrinth->at[i][j+1].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i;
|
||||||
|
tmp->y = j+1;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i!=labyrinth->end.x || j+1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j+1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j+1].direction = RIGHT;
|
||||||
|
labyrinth->at[i][j+1].dp = dist+1;
|
||||||
|
}
|
||||||
|
if(i<labyrinth->x_size-1 && labyrinth->at[i][j].south && labyrinth->at[i+1][j].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i+1;
|
||||||
|
tmp->y = j;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i+1!=labyrinth->end.x || j!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i+1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i+1][j].direction = DOWN;
|
||||||
|
labyrinth->at[i+1][j].dp = dist+1;
|
||||||
|
}
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].east && labyrinth->at[i][j-1].dp<0){
|
||||||
|
element* tmp = (element*)malloc(sizeof(element));
|
||||||
|
tmp->dist = dist+1;
|
||||||
|
tmp->x = i;
|
||||||
|
tmp->y = j-1;
|
||||||
|
|
||||||
|
priority_queuePush(queue, tmp);
|
||||||
|
|
||||||
|
if(i!=labyrinth->end.x || j-1!=labyrinth->end.y)
|
||||||
|
labyrinth->at[i][j-1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j-1].direction = LEFT;
|
||||||
|
labyrinth->at[i][j-1].dp = dist+1;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,137 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/eller.h"
|
||||||
|
#include "../include/queue.h"
|
||||||
|
|
||||||
|
#define ELLER_COST 2
|
||||||
|
|
||||||
|
Labyrinth* eller_(int n, int m, bool show);
|
||||||
|
|
||||||
|
Labyrinth* eller(int n, int m){
|
||||||
|
return eller_(n, m, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* ellerShow(int n, int m){
|
||||||
|
return eller_(n, m, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* eller_(int n, int m, bool show){
|
||||||
|
int i, j, h, tmp, tp;
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
int sets[m][m];
|
||||||
|
int setSizes[m];
|
||||||
|
|
||||||
|
queue(freeSets);
|
||||||
|
|
||||||
|
int arr[m];
|
||||||
|
for(i=0; i<m; i++){
|
||||||
|
int* s = (int*)malloc(sizeof(int));
|
||||||
|
*s = i;
|
||||||
|
queuePush(freeSets, s);
|
||||||
|
setSizes[i] = 0;
|
||||||
|
|
||||||
|
arr[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int down[m];
|
||||||
|
|
||||||
|
|
||||||
|
printLab(labyrinth);
|
||||||
|
|
||||||
|
for(i=0; i<n; i++){
|
||||||
|
// reset sets
|
||||||
|
for(j=0; j<m; j++){
|
||||||
|
setSizes[j] = 0;
|
||||||
|
down[j] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// gives cells with no set a set
|
||||||
|
for(j=0; j<m; j++){
|
||||||
|
if(labyrinth->at[i][j].dp < 0){
|
||||||
|
int* tmp = (int*)freeSets->front->element;
|
||||||
|
labyrinth->at[i][j].dp = *tmp;
|
||||||
|
queuePop(freeSets);
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = labyrinth->at[i][j].dp;
|
||||||
|
sets[tmp][setSizes[tmp]] = j;
|
||||||
|
setSizes[tmp]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomizes order
|
||||||
|
for(j=m; j>0; j--){
|
||||||
|
tmp = rand() % j;
|
||||||
|
tp = arr[tmp];
|
||||||
|
arr[tmp] = arr[j-1];
|
||||||
|
arr[j-1] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomly merge cells
|
||||||
|
for(j=0; j<m; j++){
|
||||||
|
tmp = arr[j];
|
||||||
|
if(tmp < m-1 && labyrinth->at[i][tmp].dp != labyrinth->at[i][tmp+1].dp && (rand()%ELLER_COST || i==n-1)){
|
||||||
|
labyrinth->at[i][tmp].east = 1;
|
||||||
|
|
||||||
|
tp = labyrinth->at[i][tmp].dp;
|
||||||
|
tmp = labyrinth->at[i][tmp+1].dp;
|
||||||
|
for(h=0; h<setSizes[tmp]; h++){
|
||||||
|
sets[tp][setSizes[tp]] = sets[tmp][h];
|
||||||
|
setSizes[tp]++;
|
||||||
|
labyrinth->at[i][sets[tmp][h]].dp = tp;
|
||||||
|
}
|
||||||
|
setSizes[tmp] = 0;
|
||||||
|
|
||||||
|
int* s = (int*)malloc(sizeof(int));
|
||||||
|
*s = tmp;
|
||||||
|
queuePush(freeSets, s);
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][tmp].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][tmp].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomly makes cells go down
|
||||||
|
for(j=0; j<m && i<n-1; j++){
|
||||||
|
// all but last cell
|
||||||
|
for(h=setSizes[j]; h>1; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = sets[j][tmp];
|
||||||
|
sets[j][tmp] = sets[j][h-1];
|
||||||
|
|
||||||
|
if(rand()%ELLER_COST){
|
||||||
|
labyrinth->at[i][tp].south = 1;
|
||||||
|
labyrinth->at[i+1][tp].dp = labyrinth->at[i][tp].dp;
|
||||||
|
down[j] = 1;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][tp].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][tp].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// last cell
|
||||||
|
if(setSizes[j] && (!down[j] || rand()%ELLER_COST)){
|
||||||
|
labyrinth->at[i][sets[j][0]].south = 1;
|
||||||
|
labyrinth->at[i+1][sets[j][0]].dp = labyrinth->at[i][sets[j][0]].dp;
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][sets[j][0]].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][sets[j][0]].color = WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
queueFree(freeSets);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
|
@ -0,0 +1,149 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
|
||||||
|
void find_path_r(Labyrinth* labyrinth, int i, int j);
|
||||||
|
|
||||||
|
Labyrinth* newLabyrinth(int n, int m){
|
||||||
|
if(n<1 || m<1 || (n==1 && m==1)){
|
||||||
|
printf("COGLIONE\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* labyrinth = (Labyrinth* )malloc(sizeof(Labyrinth));
|
||||||
|
labyrinth->x_size = n;
|
||||||
|
labyrinth->y_size = m;
|
||||||
|
|
||||||
|
labyrinth->at = (Node** )malloc(n*sizeof(Node* ));
|
||||||
|
for(int i=0; i<m; i++)
|
||||||
|
labyrinth->at[i] = (Node *)malloc(m*sizeof(Node));
|
||||||
|
|
||||||
|
for(int i=0; i<n; i++){
|
||||||
|
for(int j=0; j<m; j++){
|
||||||
|
labyrinth->at[i][j].dp = -1;
|
||||||
|
labyrinth->at[i][j].east = 0;
|
||||||
|
labyrinth->at[i][j].south = 0;
|
||||||
|
labyrinth->at[i][j].visited = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
labyrinth->x_size = n;
|
||||||
|
labyrinth->y_size = m;
|
||||||
|
|
||||||
|
labyrinth->start.x = rand()%n;
|
||||||
|
labyrinth->start.y = rand()%m;
|
||||||
|
|
||||||
|
do{
|
||||||
|
labyrinth->end.x = rand()%n;
|
||||||
|
labyrinth->end.y = rand()%m;
|
||||||
|
}while(labyrinth->start.x == labyrinth->end.x && labyrinth->start.y == labyrinth->end.y);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
||||||
|
|
||||||
|
void resetLabyrinth(Labyrinth* labyrinth){
|
||||||
|
for(int i=0; i<labyrinth->x_size; i++){
|
||||||
|
for(int j=0; j<labyrinth->y_size; j++){
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
labyrinth->at[i][j].visited = 0;
|
||||||
|
labyrinth->at[i][j].dp = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
labyrinth->at[labyrinth->start.x][labyrinth->start.y].color = BLUE;
|
||||||
|
labyrinth->at[labyrinth->end.x][labyrinth->end.y].color = BLUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeLabyrinth(Labyrinth* labyrinth){
|
||||||
|
for(int i=0; i<labyrinth->x_size; i++)
|
||||||
|
free(labyrinth->at[i]);
|
||||||
|
free(labyrinth->at);
|
||||||
|
free(labyrinth);
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_path(Labyrinth* labyrinth){
|
||||||
|
find_path_r(labyrinth, labyrinth->end.x, labyrinth->end.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_path_r(Labyrinth* labyrinth, int i, int j){
|
||||||
|
labyrinth->at[i][j].color = BLUE;
|
||||||
|
// printLab(labyrinth);
|
||||||
|
|
||||||
|
if(i==labyrinth->start.x && j==labyrinth->start.y)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(labyrinth->at[i][j].direction == UP)
|
||||||
|
find_path_r(labyrinth, i+1, j);
|
||||||
|
else if(labyrinth->at[i][j].direction == RIGHT)
|
||||||
|
find_path_r(labyrinth, i, j-1);
|
||||||
|
else if(labyrinth->at[i][j].direction == DOWN)
|
||||||
|
find_path_r(labyrinth, i-1, j);
|
||||||
|
else if(labyrinth->at[i][j].direction == LEFT)
|
||||||
|
find_path_r(labyrinth, i, j+1);
|
||||||
|
else{
|
||||||
|
printf("ERROR IN FIND_PATH\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printLab(Labyrinth* labyrinth){
|
||||||
|
printLabWith(labyrinth, '|', '-', '+');
|
||||||
|
}
|
||||||
|
|
||||||
|
void printLabWith(Labyrinth* labyrinth, char wall, char floor, char corner){
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
system("clear");
|
||||||
|
|
||||||
|
for(j=0; j<labyrinth->y_size; j++){
|
||||||
|
printf("%c%c", corner, floor);
|
||||||
|
}
|
||||||
|
printf("%c\n", corner);
|
||||||
|
|
||||||
|
for(i=0; i<labyrinth->x_size; i++){
|
||||||
|
printf("%c", wall);
|
||||||
|
|
||||||
|
for(j=0; j<labyrinth->y_size; j++){
|
||||||
|
if(labyrinth->at[i][j].color == BLUE)
|
||||||
|
printf("\033[104m \033[49m");
|
||||||
|
else if(labyrinth->at[i][j].color == GREEN)
|
||||||
|
printf("\033[102m \033[49m");
|
||||||
|
else if(labyrinth->at[i][j].color == ORANGE)
|
||||||
|
printf("\033[103m \033[49m");
|
||||||
|
else
|
||||||
|
printf(" ");
|
||||||
|
|
||||||
|
if(labyrinth->at[i][j].east && j<labyrinth->y_size-1){
|
||||||
|
if(labyrinth->at[i][j+1].color == BLUE && labyrinth->at[i][j].color == BLUE)
|
||||||
|
printf("\033[104m \033[49m");
|
||||||
|
else if((labyrinth->at[i][j+1].color == GREEN || labyrinth->at[i][j+1].color == BLUE) && (labyrinth->at[i][j].color == GREEN || labyrinth->at[i][j].color == BLUE))
|
||||||
|
printf("\033[102m \033[49m");
|
||||||
|
else if(!(labyrinth->at[i][j+1].color == WHITE) && !(labyrinth->at[i][j].color == WHITE))
|
||||||
|
printf("\033[103m \033[49m");
|
||||||
|
else
|
||||||
|
printf(" ");
|
||||||
|
}else
|
||||||
|
printf("%c", wall);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
for(j=0; j<labyrinth->y_size; j++){
|
||||||
|
printf("%c", corner);
|
||||||
|
if(labyrinth->at[i][j].south && i<labyrinth->x_size-1){
|
||||||
|
if(labyrinth->at[i][j].color == BLUE && labyrinth->at[i+1][j].color == BLUE)
|
||||||
|
printf("\033[104m \033[49m");
|
||||||
|
else if((labyrinth->at[i][j].color == GREEN || labyrinth->at[i][j].color == BLUE) && (labyrinth->at[i+1][j].color == GREEN || labyrinth->at[i+1][j].color == BLUE))
|
||||||
|
printf("\033[102m \033[49m");
|
||||||
|
else if(!(labyrinth->at[i][j].color == WHITE) && !(labyrinth->at[i+1][j].color == WHITE))
|
||||||
|
printf("\033[103m \033[49m");
|
||||||
|
else
|
||||||
|
printf(" ");
|
||||||
|
}else
|
||||||
|
printf("%c", floor);
|
||||||
|
}
|
||||||
|
printf("%c\n", corner);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
usleep(50000);
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "../include/dijkstra.h"
|
||||||
|
#include "../include/dfs.h"
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/binaryTree.h"
|
||||||
|
#include "../include/depthFirst.h"
|
||||||
|
#include "../include/prim.h"
|
||||||
|
#include "../include/eller.h"
|
||||||
|
#include "../include/water.h"
|
||||||
|
#include "../include/bfs.h"
|
||||||
|
|
||||||
|
#define N 15
|
||||||
|
#define M 15
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
srand(time(0));
|
||||||
|
|
||||||
|
int n = N, m = M;
|
||||||
|
|
||||||
|
Labyrinth* labyrinth1 = prim(n, m);
|
||||||
|
|
||||||
|
water(labyrinth1);
|
||||||
|
printLab(labyrinth1);
|
||||||
|
resetLabyrinth(labyrinth1);
|
||||||
|
|
||||||
|
|
||||||
|
removeLabyrinth(labyrinth1);
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/coor.h"
|
||||||
|
#include "../include/prim.h"
|
||||||
|
|
||||||
|
Labyrinth* prim_(int n, int m, bool show);
|
||||||
|
|
||||||
|
Labyrinth* prim(int n, int m){
|
||||||
|
return prim_(n, m, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Labyrinth* primShow(int n, int m){
|
||||||
|
return prim_(n, m, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// creates a labyrinth using prim's algorithm
|
||||||
|
Labyrinth* prim_(int n, int m, bool show){
|
||||||
|
int i, j, h, tmp, tp;
|
||||||
|
Labyrinth* labyrinth = newLabyrinth(n, m);
|
||||||
|
|
||||||
|
int arr[4] = {0, 1, 2, 3};
|
||||||
|
|
||||||
|
coor frontier[n*m];
|
||||||
|
coor current = {.x = rand()%n, .y = rand()%m};
|
||||||
|
i = current.x;
|
||||||
|
j = current.y;
|
||||||
|
int frontierSize = 0;
|
||||||
|
|
||||||
|
do{
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
labyrinth->at[i][j].visited = 1;
|
||||||
|
|
||||||
|
// adds frontier cells
|
||||||
|
if(i > 0 && labyrinth->at[i-1][j].dp < 0){
|
||||||
|
frontier[frontierSize].x = i-1;
|
||||||
|
frontier[frontierSize].y = j;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i-1][j].dp = 1;
|
||||||
|
}
|
||||||
|
if(j < m-1 && labyrinth->at[i][j+1].dp < 0){
|
||||||
|
frontier[frontierSize].x = i;
|
||||||
|
frontier[frontierSize].y = j+1;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i][j+1].dp = 1;
|
||||||
|
}
|
||||||
|
if(i < n-1 && labyrinth->at[i+1][j].dp < 0){
|
||||||
|
frontier[frontierSize].x = i+1;
|
||||||
|
frontier[frontierSize].y = j;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i+1][j].dp = 1;
|
||||||
|
}
|
||||||
|
if(j > 0 && labyrinth->at[i][j-1].dp < 0){
|
||||||
|
frontier[frontierSize].x = i;
|
||||||
|
frontier[frontierSize].y = j-1;
|
||||||
|
frontierSize++;
|
||||||
|
labyrinth->at[i][j-1].dp = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomly selects a frontier cell
|
||||||
|
tmp = rand()%frontierSize;
|
||||||
|
current = frontier[tmp];
|
||||||
|
frontier[tmp] = frontier[frontierSize-1];
|
||||||
|
frontierSize--;
|
||||||
|
|
||||||
|
i = current.x;
|
||||||
|
j = current.y;
|
||||||
|
|
||||||
|
// randomly joins the selected cell to another cell
|
||||||
|
for(h=3; h>0; h--){
|
||||||
|
tmp = rand()%h;
|
||||||
|
tp = arr[h];
|
||||||
|
arr[h] = arr[tmp];
|
||||||
|
arr[tmp] = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = 1;
|
||||||
|
for(h=0; h<4 && tmp; h++){
|
||||||
|
if(arr[h]==0){
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].visited){
|
||||||
|
labyrinth->at[i-1][j].south = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==1){
|
||||||
|
if(j<m-1 && labyrinth->at[i][j+1].visited){
|
||||||
|
labyrinth->at[i][j].east = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==2){
|
||||||
|
if(i<n-1 && labyrinth->at[i+1][j].visited){
|
||||||
|
labyrinth->at[i][j].south = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}else if(arr[h]==3){
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].visited){
|
||||||
|
labyrinth->at[i][j-1].east = 1;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}while(frontierSize);
|
||||||
|
|
||||||
|
if(show){
|
||||||
|
labyrinth->at[i][j].color = ORANGE;
|
||||||
|
printLab(labyrinth);
|
||||||
|
labyrinth->at[i][j].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
resetLabyrinth(labyrinth);
|
||||||
|
|
||||||
|
return labyrinth;
|
||||||
|
}
|
|
@ -0,0 +1,127 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/priority_queue.h"
|
||||||
|
|
||||||
|
|
||||||
|
void priority_queueFixheap(priority_queue* q, int i);
|
||||||
|
void priority_queueHeapify(priority_queue* queue, int i);
|
||||||
|
void priority_queueSwap(void** a, void** b);
|
||||||
|
|
||||||
|
priority_queue* newPriority_queue(bool (*compare)()){
|
||||||
|
priority_queue* pq = (priority_queue*)malloc(sizeof(priority_queue));
|
||||||
|
pq->size = 0;
|
||||||
|
pq->capacity = 0;
|
||||||
|
pq->growthFactor = PRIORITY_QUEUE_INIT_GROWTH_FACT;
|
||||||
|
|
||||||
|
pq->compare = compare;
|
||||||
|
pq->toString = NULL;
|
||||||
|
|
||||||
|
return pq;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queuePush(priority_queue* pq, void* element){
|
||||||
|
if(pq->size == pq->capacity){
|
||||||
|
if(pq->capacity)
|
||||||
|
priority_queueResize(pq, pq->capacity * pq->growthFactor);
|
||||||
|
else
|
||||||
|
priority_queueResize(pq, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pq->at[pq->size] = element;
|
||||||
|
pq->size++;
|
||||||
|
|
||||||
|
priority_queueFixheap(pq, pq->size-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueFixheap(priority_queue* pq, int i){
|
||||||
|
if(!i)
|
||||||
|
return;
|
||||||
|
if(pq->compare(pq->at[(i-1)/2], pq->at[i]))
|
||||||
|
priority_queueSwap(pq->at[i], pq->at[(i-1)/2]);
|
||||||
|
|
||||||
|
priority_queueFixheap(pq, (i-1)/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queuePop(priority_queue* pq){
|
||||||
|
if(!pq->size){
|
||||||
|
fprintf(stderr, "POP ON EMPTY PRIORITY_QUEUE\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
priority_queueSwap(&pq->at[0], &pq->at[pq->size-1]);
|
||||||
|
free(pq->at[pq->size-1]);
|
||||||
|
pq->size--;
|
||||||
|
priority_queueHeapify(pq, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueHeapify(priority_queue* pq, int i){
|
||||||
|
if(i >= (pq->size)/2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int minson;
|
||||||
|
if(pq->size-1 == 2*i+1 || !pq->compare(pq->at[2*i+1], pq->at[2*i+2]))
|
||||||
|
minson = 2*i+1;
|
||||||
|
else
|
||||||
|
minson = 2*i+2;
|
||||||
|
|
||||||
|
if(pq->compare(pq->at[i], pq->at[minson])){
|
||||||
|
priority_queueSwap(&pq->at[i], &pq->at[minson]);
|
||||||
|
priority_queueHeapify(pq, minson);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueSwap(void** a, void** b){
|
||||||
|
void* tmp = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueResize(priority_queue* pq, int capacity){
|
||||||
|
for(int i=pq->size; i>capacity; i--)
|
||||||
|
free(pq->at[i-1]);
|
||||||
|
|
||||||
|
void **at = realloc(pq->at, sizeof(void *) * capacity);
|
||||||
|
pq->at = at;
|
||||||
|
pq->capacity = capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueClear(priority_queue* pq){
|
||||||
|
priority_queueResize(pq, 0);
|
||||||
|
pq->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueFree(priority_queue* pq){
|
||||||
|
priority_queueClear(pq);
|
||||||
|
free(pq->at);
|
||||||
|
free(pq);
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queueShrink_to_fit(priority_queue* pq){
|
||||||
|
priority_queueResize(pq, pq->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool priority_queueIsEmpty(priority_queue* pq){
|
||||||
|
return pq->size > 0 ? false : true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* priority_queueTop(priority_queue* pq){
|
||||||
|
if(!pq->size){
|
||||||
|
fprintf(stderr, "PRIORITY_QUEUE IS EMPTY\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pq->at[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
void priority_queuePrint(priority_queue* pq){
|
||||||
|
if(pq->toString == NULL){
|
||||||
|
fprintf(stderr, "TOSTRING NOT DEFINED\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i=0; i<pq->size; i++)
|
||||||
|
pq->toString(pq->at[i]);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
|
@ -0,0 +1,99 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../include/queue.h"
|
||||||
|
|
||||||
|
queue* newQueue(){
|
||||||
|
queue* tmp = (queue*)malloc(sizeof(queue));
|
||||||
|
tmp->size = 0;
|
||||||
|
|
||||||
|
tmp->toString = NULL;
|
||||||
|
tmp->front = NULL;
|
||||||
|
tmp->back = NULL;
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queuePush(queue* q, void* element){
|
||||||
|
queue_element* qel = (queue_element*)malloc(sizeof(queue_element));
|
||||||
|
qel->element = element;
|
||||||
|
qel->next = NULL;
|
||||||
|
|
||||||
|
if(queueIsEmpty(q)){
|
||||||
|
q->front = qel;
|
||||||
|
q->back = qel;
|
||||||
|
}else{
|
||||||
|
q->back->next = qel;
|
||||||
|
q->back = qel;
|
||||||
|
}
|
||||||
|
|
||||||
|
q->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queuePrint(queue* q){
|
||||||
|
if(q->toString == NULL){
|
||||||
|
fprintf(stderr, "TOSTRING NOT DEFINED\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_element* tmp = q->front;
|
||||||
|
while(tmp){
|
||||||
|
q->toString(tmp->element);
|
||||||
|
tmp = tmp->next;
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void queuePop(queue* q){
|
||||||
|
if(queueIsEmpty(q)){
|
||||||
|
fprintf(stderr, "QUEUE IS EMTPY\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_element* tmp = q->front->next;
|
||||||
|
free(q->front->element);
|
||||||
|
free(q->front);
|
||||||
|
q->front = tmp;
|
||||||
|
q->size--;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool queueIsEmpty(queue* q){
|
||||||
|
return q->size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueMerge(queue* q1, queue* q2){
|
||||||
|
if(queueIsEmpty(q1))
|
||||||
|
q1->front = q2->front;
|
||||||
|
else
|
||||||
|
q1->back->next = q2->front;
|
||||||
|
|
||||||
|
q1->back = q2->back;
|
||||||
|
q1->size += q2->size;
|
||||||
|
|
||||||
|
q2->front = NULL;
|
||||||
|
q2->back = NULL;
|
||||||
|
q2->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueClear(queue* q){
|
||||||
|
queue_element* tmp = q->front, *tp;
|
||||||
|
while(tmp){
|
||||||
|
tp = tmp->next;
|
||||||
|
free(tmp->element);
|
||||||
|
free(tmp);
|
||||||
|
tmp = tp;
|
||||||
|
}
|
||||||
|
|
||||||
|
q->front = NULL;
|
||||||
|
q->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queueFree(queue* q){
|
||||||
|
queueClear(q);
|
||||||
|
free(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* queueTop(queue* q){
|
||||||
|
return q->front;
|
||||||
|
}
|
|
@ -0,0 +1,163 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "../include/labyrinth.h"
|
||||||
|
#include "../include/water.h"
|
||||||
|
#include "../include/priority_queue.h"
|
||||||
|
#include "../include/queue.h"
|
||||||
|
|
||||||
|
bool compare_water(coor* c1, coor* c2){
|
||||||
|
return c1->x < c2->x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pp(coor* c1){
|
||||||
|
printf("%d %d", c1->x, c1->y);
|
||||||
|
}
|
||||||
|
|
||||||
|
int qmin(queue* q);
|
||||||
|
|
||||||
|
void expand(coor* c, Labyrinth* labyrinth, queue* pq);
|
||||||
|
|
||||||
|
void water(Labyrinth *labyrinth){
|
||||||
|
int i, min;
|
||||||
|
|
||||||
|
queue(pq); // DA METTERE RBTREE (mai)
|
||||||
|
coor* tmp = (coor*)malloc(sizeof(coor));
|
||||||
|
tmp->x = labyrinth->start.x;
|
||||||
|
tmp->y = labyrinth->start.y;
|
||||||
|
labyrinth->at[tmp->x][tmp->y].dp = 0;
|
||||||
|
|
||||||
|
queuePush(pq, tmp);
|
||||||
|
pq->toString = pp; // TMP
|
||||||
|
|
||||||
|
queue(q);
|
||||||
|
q->toString = pp;
|
||||||
|
|
||||||
|
|
||||||
|
while(!queueIsEmpty(pq)){
|
||||||
|
printf("size: %d\n", pq->size);
|
||||||
|
queuePrint(pq);
|
||||||
|
|
||||||
|
min = qmin(pq);
|
||||||
|
printf("min: %d\n", min);
|
||||||
|
queue_element* qelem = pq->front;
|
||||||
|
queue_element* prev = NULL;
|
||||||
|
|
||||||
|
while(qelem){
|
||||||
|
|
||||||
|
printf("cons: ");
|
||||||
|
pp(qelem->element);
|
||||||
|
printf("\n");
|
||||||
|
if(((coor*)qelem->element)->x == min){
|
||||||
|
if(((coor*)(qelem->element))->x == labyrinth->end.x && ((coor*)(qelem->element))->y == labyrinth->end.y)
|
||||||
|
return;
|
||||||
|
|
||||||
|
printf("in elem: ");
|
||||||
|
pp(qelem->element);
|
||||||
|
printf("\n");
|
||||||
|
expand(qelem->element, labyrinth, q);
|
||||||
|
labyrinth->at[((coor*)qelem->element)->x][((coor*)qelem->element)->y].color = GREEN;
|
||||||
|
|
||||||
|
if(prev)
|
||||||
|
prev->next = qelem->next;
|
||||||
|
else
|
||||||
|
pq->front = qelem->next;
|
||||||
|
|
||||||
|
pq->size--;
|
||||||
|
|
||||||
|
if(!qelem->next)
|
||||||
|
pq->back = prev;
|
||||||
|
|
||||||
|
free(qelem->element);
|
||||||
|
free(qelem);
|
||||||
|
|
||||||
|
}else{
|
||||||
|
prev = qelem;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(prev)
|
||||||
|
qelem = prev->next;
|
||||||
|
else if(pq->front)
|
||||||
|
qelem = pq->front->next;
|
||||||
|
else
|
||||||
|
qelem = NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("newqueue: %d\n", pq->size);
|
||||||
|
queuePrint(pq);
|
||||||
|
printf("p: %d\n", q->size);
|
||||||
|
queuePrint(q);
|
||||||
|
|
||||||
|
queueMerge(pq, q);
|
||||||
|
printf("new: %d\n", pq->size);
|
||||||
|
queuePrint(pq);
|
||||||
|
|
||||||
|
printLab(labyrinth);
|
||||||
|
scanf("%d", &i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void expand(coor* c, Labyrinth* labyrinth, queue* q){
|
||||||
|
int i = c->x, j = c->y;
|
||||||
|
|
||||||
|
if(i>0 && labyrinth->at[i-1][j].south && labyrinth->at[i-1][j].dp<0){
|
||||||
|
coor* tmp = (coor*)malloc(sizeof(coor));
|
||||||
|
tmp->x = i-1;
|
||||||
|
tmp->y = j;
|
||||||
|
|
||||||
|
queuePush(q, tmp);
|
||||||
|
|
||||||
|
labyrinth->at[i-1][j].direction = UP;
|
||||||
|
labyrinth->at[i-1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i-1][j].dp = 0;
|
||||||
|
}
|
||||||
|
if(j<labyrinth->y_size-1 && labyrinth->at[i][j].east && labyrinth->at[i][j+1].dp<0){
|
||||||
|
coor* tmp = (coor*)malloc(sizeof(coor));
|
||||||
|
tmp->x = i;
|
||||||
|
tmp->y = j+1;
|
||||||
|
|
||||||
|
queuePush(q, tmp);
|
||||||
|
|
||||||
|
labyrinth->at[i][j+1].direction = UP;
|
||||||
|
labyrinth->at[i][j+1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j+1].dp = 0;
|
||||||
|
}
|
||||||
|
if(i<labyrinth->x_size-1 && labyrinth->at[i][j].south && labyrinth->at[i+1][j].dp<0){
|
||||||
|
coor* tmp = (coor*)malloc(sizeof(coor));
|
||||||
|
tmp->x = i+1;
|
||||||
|
tmp->y = j;
|
||||||
|
|
||||||
|
queuePush(q, tmp);
|
||||||
|
|
||||||
|
labyrinth->at[i+1][j].direction = UP;
|
||||||
|
labyrinth->at[i+1][j].color = ORANGE;
|
||||||
|
labyrinth->at[i+1][j].dp = 0;
|
||||||
|
}
|
||||||
|
if(j>0 && labyrinth->at[i][j-1].east && labyrinth->at[i][j-1].dp<0){
|
||||||
|
coor* tmp = (coor*)malloc(sizeof(coor));
|
||||||
|
tmp->x = i;
|
||||||
|
tmp->y = j-1;
|
||||||
|
|
||||||
|
queuePush(q, tmp);
|
||||||
|
|
||||||
|
labyrinth->at[i][j-1].direction = UP;
|
||||||
|
labyrinth->at[i][j-1].color = ORANGE;
|
||||||
|
labyrinth->at[i][j-1].dp = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int qmin(queue* q){
|
||||||
|
queue_element *tmp = q->front;
|
||||||
|
coor min = {.x = 0};
|
||||||
|
while(tmp){
|
||||||
|
if(compare_water(&min, tmp->element))
|
||||||
|
min.x = ((coor*)tmp->element)->x;
|
||||||
|
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return min.x;
|
||||||
|
}
|
Loading…
Reference in New Issue