initial commit

This commit is contained in:
Lorenzo Bianchi 2023-04-20 13:16:53 +02:00
parent a0b9f25368
commit 54b28682b1
82 changed files with 2921 additions and 0 deletions

56
.vscode/settings.json vendored Normal file
View File

@ -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"
}
}

29
Makefile Normal file
View File

@ -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

2
TODO.txt Normal file
View File

@ -0,0 +1,2 @@
- water
- emplace

BIN
a.out Executable file

Binary file not shown.

55
bakcup/.vscode/settings.json vendored Normal file
View File

@ -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"
}
}

29
bakcup/Makefile Normal file
View File

@ -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

2
bakcup/TODO.txt Normal file
View File

@ -0,0 +1,2 @@
- water
- emplace

BIN
bakcup/a.out Executable file

Binary file not shown.

6
bakcup/include/bfs.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef BFS_H
#define BFS_H
void bfs(Labyrinth* labyrinth);
#endif

View File

@ -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

9
bakcup/include/coor.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef COOR_H
#define COOR_H
typedef struct coor{
int x;
int y;
} coor;
#endif

View File

@ -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

6
bakcup/include/dfs.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef DFS_H
#define DFS_H
void dfs(Labyrinth* labyrinth);
#endif

View File

@ -0,0 +1,8 @@
#ifndef DIJKSTRA_H
#define DIJKSTRA_H
#include "labyrinth.h"
void dijkstra(Labyrinth* labyrinth);
#endif

9
bakcup/include/eller.h Normal file
View File

@ -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

View File

@ -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

9
bakcup/include/prim.h Normal file
View File

@ -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

View File

@ -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

50
bakcup/include/queue.h Normal file
View File

@ -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

6
bakcup/include/water.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef WATER_H
#define WATHER_H
void water(Labyrinth* labyrinth);
#endif

BIN
bakcup/obj/bfs.o Normal file

Binary file not shown.

BIN
bakcup/obj/binaryTree.o Normal file

Binary file not shown.

BIN
bakcup/obj/depthFirst.o Normal file

Binary file not shown.

BIN
bakcup/obj/dfs.o Normal file

Binary file not shown.

BIN
bakcup/obj/dijkstra.o Normal file

Binary file not shown.

BIN
bakcup/obj/eller.o Normal file

Binary file not shown.

BIN
bakcup/obj/labyrith.o Normal file

Binary file not shown.

BIN
bakcup/obj/main.o Normal file

Binary file not shown.

BIN
bakcup/obj/prim.o Normal file

Binary file not shown.

Binary file not shown.

BIN
bakcup/obj/priority_queue.o Normal file

Binary file not shown.

BIN
bakcup/obj/queue.o Normal file

Binary file not shown.

BIN
bakcup/obj/water.o Normal file

Binary file not shown.

87
bakcup/src/bfs.c Normal file
View File

@ -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);
}
}
}
}
}

70
bakcup/src/binaryTree.c Normal file
View File

@ -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;
}

77
bakcup/src/depthFirst.c Normal file
View File

@ -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;
}
}

67
bakcup/src/dfs.c Normal file
View File

@ -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;
}

117
bakcup/src/dijkstra.c Normal file
View File

@ -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;
}
}

137
bakcup/src/eller.c Normal file
View File

@ -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;
}

149
bakcup/src/labyrith.c Normal file
View File

@ -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);
}

30
bakcup/src/main.c Normal file
View File

@ -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);
}

120
bakcup/src/prim.c Normal file
View File

@ -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;
}

127
bakcup/src/priority_queue.c Normal file
View File

@ -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);
}