From 57b6d692fd29afef498f6d0c59b511f3ea5d51ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Korpesio?= Date: Wed, 19 Jan 2022 22:29:10 +0100 Subject: [PATCH] prvy --- sk1a/main.c | 11 +- sk1a/main.o | Bin 7496 -> 7496 bytes sk1a/maze.c | 145 ++++++++++++------------- sk1a/maze.o | Bin 7064 -> 9304 bytes sk1a/program | Bin 21808 -> 22312 bytes sk2a/readme.md | 284 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 356 insertions(+), 84 deletions(-) create mode 100644 sk2a/readme.md diff --git a/sk1a/main.c b/sk1a/main.c index 2072a82..b273422 100644 --- a/sk1a/main.c +++ b/sk1a/main.c @@ -10,16 +10,15 @@ void print_solution(char* matrix,int sz){ printf("\n"); } } - } int main(){ char tmaze[SZ+1][SZ+1]={ - "*x ", - " xx ", - " x ", - " x ", - " ", + "*x ", + " x ", + " x ", + " x ", + " ", }; char maze[SZ*SZ]; memset(maze,' ',SZ*SZ); diff --git a/sk1a/main.o b/sk1a/main.o index bdb4139e3266442c8dca19db25a9c3fb7c6d5d0b..18d8162ed1880c83b104d847134819ae136d4ff8 100644 GIT binary patch delta 39 pcmX?Mb;4=`3!{`mMcQ%KRSFEpUDqfmfH@mLoJ|T9o8=g_WB?>z4d?&> delta 39 pcmX?Mb;4=`3!_wpLfUcHRSFEpUDqfmfH@mLoJ|S}o8=g_WB?-n4Uhl; diff --git a/sk1a/maze.c b/sk1a/maze.c index 8a501ce..38467cf 100644 --- a/sk1a/maze.c +++ b/sk1a/maze.c @@ -3,86 +3,75 @@ #include "maze.h" #include -int solve_maze(char* maze,int size) { - int i = 0; - int p, l, h, d = 0; - while(i != size*size-1){ - //if (i % size - 1 != 0){l = 1;} - //else if (i < size*size - size){h = 1;} - //else if (i % size){p = 1;} - //else if (i >= size){d =1;} - if (maze[i + 1] == ' ' /*&& i % size - 1 != 0 */&& l != 1) { - maze[i + 1] = '*'; - i++; - p = 0; - l = 0; - h = 0; - d = 0; - } else if (maze[i + size] == ' ' /*&& i < (size*size - size) != 0 */&& h != 1) { - maze[i + size] = '*'; - i = i + size; - p = 0; - l = 0; - h = 0; - d = 0; - } else if (maze[i - 1] == ' ' /*&& i % size != 0 */&& p != 1) { - maze[i - 1] = '*'; - i--; - p = 0; - l = 0; - h = 0; - d = 0; - } else if (maze[i - size] == ' ' /*&& i >= size */&& d != 1) { - maze[i - size] = '*'; - i = i - size; - p = 0; - l = 0; - h = 0; - d = 0; +int issafe(char* maze, int x, int y, int size) { + if ((x >= 0 && x < size) && (y >= 0 && y < size) && maze[x * size + y] == ' '){ + return 1; + } + return 0; +} + +int backtracking(char* maze, int x, int y, int size){ + if(x == size - 1 && y == size - 1 && maze[x * size + y] == ' '){ + maze[x * size + y] = '*'; + return 1; + } + + if(issafe(maze, x, y, size) == 1) { + maze[x * size + y] = '*'; + + if (backtracking(maze, x, y + 1, size) == 1) { + return 1; + } + if (backtracking(maze, x + 1, y, size) == 1) { + return 1; + } + if (backtracking(maze, x - 1, y, size) == 1) { + return 1; + } + if (backtracking(maze, x, y - 1, size) == 1) { + return 1; + } + maze[x * size + y] = ' '; + } + return 0; +} + +int solve_maze(char* maze, int size) { + if (maze[0] == '*'){ + assert(maze[0] == '*'); + maze[0] = ' '; + } + int x,y = 0; + while(x * size + y <= size*size - 1 ){ + printf("%d\n%d", x * size + y, y); + //issafe(maze, x, y, size); + if (maze[x * size + y + 1] == ' ' /*&& y != 5*/){ + assert(maze[x * size + y + 1] == ' '); + maze[x * size + y + 1] = '*'; + y++; + + } + if (maze[(x + 1) * size + y] == ' ' /*&& x != 5*/){ + assert(maze[(x + 1) * size + y] == ' '); + maze[(x + 1) * size + y] = '*'; + x++; + + } + if (maze[x * size + y - 1] == ' ' && y != 0){ + assert(maze[x * size + y - 1] == ' '); + maze[x * size + y - 1] = '*'; + y--; + + } + if (maze[(x - 1) * size + y] == ' ' /*&& x != 0*/){ + assert(maze[(x - 1) * size + y] == ' '); + maze[(x - 1) * size + y] = '*'; + x--; + } else{ - while(1){ - - if (maze[i + 1] == ' ' && i != size - 1 && l != 1) { - //i++; - break; - } - if (maze[i + size] == ' ' && i != size - 1 && h != 1) { - //i = i + size; - break; - } - if (maze[i - 1] == ' ' && i != 0 && p != 1) { - //i--; - break; - } - if (maze[i - size] == ' ' && i > size && d != 1) { - //i = i - size; - break; - } - p = 0; - l = 0; - h = 0; - d = 0; - if (maze[i + 1] == '*' && i != size - 1) { - maze[i] = ' '; - i++; - p = 1; - } else if (maze[i + size] == '*' && i != size - 1) { - maze[i] = ' '; - i = i + size; - d = 1; - } else if (maze[i - 1] == '*' && i != 0) { - maze[i] = ' '; - i--; - l = 1; - } else if (maze[i - size] == '*' && i > size) { - maze[i] = ' '; - i = i - size; - h = 1; - } - } + backtracking(maze,x , y, size); } } - maze[0] = '*'; - return 1; +return 1; } diff --git a/sk1a/maze.o b/sk1a/maze.o index 9dc2321b228971902a3e36291ffd96736a3adf66..1f464dfbb19ff650b6bdf61c7c230c619a61376f 100644 GIT binary patch literal 9304 zcmb_idu&_P89&#~v7LwW?jy7-F3_eg+ezsoX`2!n+$N>3L9?~A<<_yU6H~_yzW0W- zTL)_fYOtcE65G0|V`WSl+A6^wn!1OkLI7%~Bn2c{LM|^Tv^!a|lguWw$OknGKJ4ut z`&;+e(Wx=}`dII^vF($iN3WBS4Y|nm^^wV=t45DrBO{?)k;q|hC^H1=V4 zLEo<6J|?)8@ga?SQryT4q`=eo(DY4ZKowk0&+JlUGca@10hZ{CYND@>*;l~&Rh*P% zc5A+A-P%3w$AtW1DO|e8S7W<-yaOwiD3pAK1YV8YKh@lilOHG_;mq8nOQ*Q3$RTe3uCBCI(>sMk6;gkQhCu<-?Whvp9&G zN)&=vJ7W+t8DK$7VGzy;FcZ^7nMjTvy+N!}`6!Yrgo5+)>GB~BU?_t;A6IXd4+ArH zGl@bFT{8wTlK~dQl=j}N-1aNE-Wxf4ilsr^Q@O~Mn&;2qvf}A_5muIVq5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$*sY@w|sg*hv(|xU1#B!d3_Mf z|8w8kg5L^wwx<-j@r*7nbm>LbgVc>YLQ_~L{;*ssG{Y;1Y>3XIY=n@(!5ZD*EXoih zC@69ZjrexyU3h7D2vHBdU3xd`g~N!t;ds%f9=m$ztJTJ$v7^Hu+xgJ%cjFM}{9cdt zb6V5crs{+trI`HW6sN;e#j(&?9PfD!Wdh!rUQ5xmRSI3j?$9q};9`?K{zkUK$kZS1 zn(aHPzV<_37B#4kw4tZ?^IhsT1StP?Y=S1(pvRZ4)C zrEY^4r8sFZ32YF15?#CUH7^QXMtVXuoL}m;#F8(aJn1Kv&cf*;(Nprfm)=POs9^sZ zC&S36VM6Rf>^XEEW}Y<6CE{bCOj3qbE?oFiANdqMQ^TiyfLa(IJ4;~k`PiAz=wd+n zBXp6#CSQJ$+Hgwj?A`=s&nNcHmWTo}n7qYFDqq`1Q#iZd`t~OK>BIT=cgl~m*jx^ffjt6V64oTZF269y}@ zM0>7+QbAmsWy!fqK_BR#bnQZyRG`3Y^%uZ2gbU!n+H|ohph$MXAes%ADrlVm`&Lf< EHzK^B6#xJL diff --git a/sk2a/readme.md b/sk2a/readme.md new file mode 100644 index 0000000..37b1c38 --- /dev/null +++ b/sk2a/readme.md @@ -0,0 +1,284 @@ +#include +#include +#include +#include +#include "compressor.h" +#define BUFSIZE 1024 +#define SIZE 256 +#define MAX_WORDS 100000 + +// One codeword is previous index - new index -character + +// Trie (a.k.a prefix tree) +struct dict { +int index; +// Character is encoded as non zero value in characters +struct dict* characters[SIZE]; +}; + +// Free trie +void delete(struct dict* tree){ //skopirovane z lz78, vymaze strukturu co sa pouziva v lz78 +if (tree == NULL){ //maze to pamat stromu +return; +} +for (int i = 0 ;i < SIZE; i++){ +delete(tree-> characters[i]); +} +free(tree); +} + +void get_words (struct dict* tree,char* words[]){ //posielam cely strom a smernik na dvojrozmerne pole +if (tree == NULL){ +return; +} +for (int i = 0 ;i < SIZE; i++){ //prejdeme vsetkych potomkov +if (tree-> characters[i] > 0){ //zistime ci tam je nieco naalokovane +int need_size = snprintf(NULL,0,"-%d-%c\n",tree->index,i); //kolko pamate +words[tree->characters[i]->index] = calloc(need_size + 1,sizeof(char)); //naalokujeme pamat +memset(words[tree->characters[i]->index],0,need_size + 1); +sprintf(words[tree->characters[i]->index],"-%d-%c\n",tree->index,i); //napise sa vo formate -index predosleho notu - aktualny znak + + get_words(tree-> characters[i],words); //zavolam na kazdeho potomka a potom na dalsich potomkov alebo syna + } + } +} + +int mylz78compress(char* buff,int size,char** outbuf){ +// https://medium.com/@dbudhrani/how-data-compression-works-exploring-lz78-e97e539138 +// The trie represents the current phrase +// The first node represents the empty string with index 0 +struct dict* root = calloc(1,sizeof(struct dict)); +struct dict* current = root; +int index = 1; +int c = 0; +char *last = NULL; +for(int i = 0; i < size; i++){ +c = buff[i]; +// Current character is in dictionary, continue searching +if (current->characters[c] > 0){ +// ak este nieco ostalo a toto je koniec +if (i == size - 1){ // pridem na koniec suboru a musim si to zapamatat, inac to stratim +int sz_needed = snprintf(NULL,0,"-%d-%c\n",current->index,c); //kolko bajtov to bude zaberat +last = calloc(sz_needed + 1,sizeof(char)); //alokuje do do last +sprintf(last,"-%d-%c\n",current->index,c); // +} + + current = current->characters[c]; + } + else { + current->characters[c] = calloc(1,sizeof(struct dict)); + current->characters[c]->index = index; + index += 1; + current = root; + } + } + + char **words = calloc(index,sizeof(char*)); // z stromu vytvori format + get_words(root,words); + + int size_needed = 0; + for(int i=1; i < index; i++){ //nastavime velkost aku bude mat vystupny buffer + if(words[i] != NULL){ + size_needed += strlen(words[i]); //poscitavame velkosti vsetkych stringov + } + } + + if (last != NULL){ //mame string last? + size_needed += strlen(last); //priratam + } + + char *temp = calloc(size_needed + 10,sizeof(char)); //alokujem velkost, padalo mi to ked som mal mensie cislo ako 10 + int pos = 0; //pamatam kde som to uz vypisal + for(int i=0; i < index; i++){ + if (words[i] != NULL){ + strcpy(temp + pos,words[i]); //nebude sa to pisat na zaciatok ale az za tym + pos += strlen(words[i]); + } + } + + if (last != NULL){ // zapisem aj last + strcpy(temp + pos,last); + pos += strlen(last); + } + + delete(root); //nepotrebujeme strom, uvolnime pamat + free(words); + + *outbuf = temp; //adresa sa zapise do outbuffer + return strlen(temp); +} + + +int myrlcompress(char* buff,int size,char** outbuf){ +// 2 * size je urcite dost, kedze v najhorsom pripade bude kazdy znak osamote +*outbuf = calloc(2 * size + 1,sizeof(char)); //smernik na smernik, napisem adresu novej pamate +memset(*outbuf,0,size + 1); //vymaze + + int head = -1; + int length = 0; + int pos = 0; + for(int i = 0; i < size; i++){ + int c = buff[i]; + + if (c == head){ + length += 1; + if (i == size - 1){ //posledne pismenko + (*outbuf)[pos] = head; + (*outbuf)[pos + 1] = length; + pos += 2; //aby som vedel kde to zapisovat + break; + } + } + else { + if (head != -1){ + (*outbuf)[pos] = head; + (*outbuf)[pos + 1] = length; + pos += 2; + } + if (i == size - 1){ //posledne pismenko + (*outbuf)[pos] = c; + (*outbuf)[pos + 1] = 1; + pos += 2; + break; + } + + head = c; + length = 1; + } + + // ak sme blizko preteceniu, vypiseme aktualny znak + if (length == 255){ //nemozme ist viac, vynulujem a idem od nuly + (*outbuf)[pos] = head; + (*outbuf)[pos + 1] = length; + pos += 2; + head = -1; + length = 0; + } + } + + return pos; //velkost vystupneho buffera +} + + +void compress(FILE* infile,FILE* outfile){ +fseek(infile, 0, SEEK_END); //vstupny subor, a chceme presunut kurzor na konci suboru +int insize = ftell(infile) + 1; // na akej pozicii je kurzor, zistime velkost suboru +rewind(infile); //vrati kurzor na zaciatok +char *buffer = calloc(insize,sizeof(char)); //alokuje pamat + + memset(buffer,0,insize); + insize = fread(buffer,sizeof(char),insize - 1,infile); //nacita obsah do bufferu + + if (insize == 0){ + assert(!ferror(infile)); + } + + char *tempbuf = NULL; // az vo funkcii to alokujem + int tempsize = myrlcompress(buffer,insize,&tempbuf); //vstupny buffer, velkost a adresu bufferu + + char *outbuf = NULL; + int outsize = mylz78compress(tempbuf,tempsize,&outbuf); + + if (outsize > 0){ + fwrite(outbuf,sizeof(char),outsize,outfile); //napise sa do suboru, z akeho buffera do akeho suboru + } + + free(buffer); + free(tempbuf); + free(outbuf); +} + + +int myrldecompress(char* buff,int size,char** outbuf){ +*outbuf = calloc(size + 1,sizeof(char)); +memset(*outbuf,0,size + 1); + + int pos = 0; + for(int i = 0; i < size; i+=2){ + char c = buff[i]; + char length = buff[i + 1]; + + // ak nam nebude stacit miesto v *outbuf, treba allocovat viac + while((length + pos) > (size - 1)){ + size *= 2; + char *tempbuf = calloc(size,sizeof(char)); + memset(tempbuf,'\0',size); + strcpy(tempbuf,*outbuf); + free(*outbuf); + *outbuf = tempbuf; + } + + for (int j = 0; j < length; j++){ //tolko krat ho pridavam, kolko mam cisielko + (*outbuf)[pos] = c; + pos += 1; + } + } + + return pos; +} + + +int mylz78decompress(char* buff,int size,char** outbuf){ //nepotrebujem pouzivat strom +*outbuf = calloc(size,sizeof(char)); //taka ista velkost ako vstup +memset(*outbuf,0,size); +char *words[MAX_WORDS]; //dvojrozmerne pole +words[0] = ""; +char c; +int index = 1; +int number; +int pos = 0; +int bytes_read, bytes_read_all = 0; //citame vstupny buffer, necitame po bajtoch, viac ciferne cisla + + while (sscanf(buff + bytes_read_all,"-%d-%c\n%n",&number,&c,&bytes_read) == 2){ //formatovacie citanie, pusaveme o bajty, ktore sa precitali ak vrati ine cislo ako 2 tak je nakocni + bytes_read_all += bytes_read; //o tolko sme sa posunuli + + words[index] = calloc(strlen(words[number]) + 2,sizeof(char*)); //vo words sa vytvoria prefixi + strcpy(words[index],words[number]); //nakopirujeme words number a pridame nakoniec c + words[index][strlen(words[number])] = c; + + // ak nam nebude stacit miesto v *outbuf, treba allocovat viac + while((strlen(words[index]) + pos) > (size - 1)){ + size *= 2; + char *tempbuf = calloc(size,sizeof(char)); + memset(tempbuf,'\0',size); + strcpy(tempbuf,*outbuf); + free(*outbuf); + *outbuf = tempbuf; + } + + strcpy(*outbuf + pos,words[index]); //nakopiruje najnovsie slovo + pos += strlen(words[index]); + index += 1; + } + + return strlen(*outbuf); //vratim velkost +} + + +void decompress(FILE* infile,FILE* outfile){ +fseek(infile, 0, SEEK_END); +int insize = ftell(infile) + 1; +rewind(infile); +char *buffer = calloc(insize,sizeof(char)); + + memset(buffer,0,insize); + insize = fread(buffer,sizeof(char),insize - 1,infile); + + if (insize == 0){ + assert(!ferror(infile)); + } + + char *tempbuf = NULL; + int tempsize = mylz78decompress(buffer,insize,&tempbuf); //najprv lz78 + + char *outbuf = NULL; + int outsize = myrldecompress(tempbuf,tempsize,&outbuf); + + if (outsize > 0){ + fwrite(outbuf,sizeof(char),outsize,outfile); + } + + free(buffer); + free(tempbuf); + free(outbuf); +} \ No newline at end of file