Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- #include<stdlib.h>
- #define POCET_PRINCEZIEN 3
- #define POCET_TELEPORTOV 10
- #define DLZKA_CESTY 200
- typedef struct pozicia{
- int x;
- int y;
- }POZICIA;
- typedef struct postupnost{
- POZICIA **pozicie;
- int cena;
- int limit;
- int pocet;
- }POSTUPNOST;
- typedef struct krok{
- POZICIA *p; //aktualna pozicia
- POZICIA *z; //predchadzajuci krok
- int index;
- int cena;
- int hotovo;
- }KROK;
- typedef struct okraj{
- KROK **kroky;
- int pocet;
- int limit;
- }OKRAJ;
- int najdivPost(POSTUPNOST *postupnost, POZICIA *poz) {
- int i;
- if(poz == NULL) return -1;
- for( i = 0; i < postupnost->pocet; i++) {
- if((poz->x == postupnost->pozicie[i]->x) && (poz->y == postupnost->pozicie[i]->y)) return i;
- }
- return -1;
- }
- void swapKROKy(OKRAJ *okraj, int pos1, int pos2) {
- KROK *k = okraj->kroky[pos1];
- okraj->kroky[pos1] = okraj->kroky[pos2];
- okraj->kroky[pos2] = k;
- okraj->kroky[pos1]->index = pos1;
- okraj->kroky[pos2]->index = pos2;
- }
- void addPOSTUPNOST(POSTUPNOST *postupnost, POSTUPNOST *postupnost2) {
- int i;
- if(postupnost2 == NULL) return;
- if(postupnost2->pocet == 0) return;
- if(postupnost->pocet == 0) {
- for( i = 0; i < postupnost2->pocet; i++) {
- int a;
- if(postupnost->pocet == postupnost->limit) {
- POZICIA **pozicie = calloc((postupnost->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < postupnost->pocet; a++) {
- pozicie[a] = postupnost->pozicie[a];
- }
- free(postupnost->pozicie);
- postupnost->pozicie = pozicie;
- postupnost->limit = postupnost->limit + DLZKA_CESTY;
- }
- postupnost->pozicie[postupnost->pocet] = postupnost2->pozicie[i];
- postupnost->pocet++;
- }
- postupnost->cena = postupnost2->cena;
- } else if((postupnost->pozicie[postupnost->pocet - 1]->x== postupnost2->pozicie[0]->x) && (postupnost->pozicie[postupnost->pocet - 1]->y== postupnost2->pozicie[0]->y)) {
- for( i = 1; i < postupnost2->pocet; i++) {
- int a;
- if(postupnost->pocet == postupnost->limit) {
- POZICIA **pozicie = calloc((postupnost->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < postupnost->pocet; a++) {
- pozicie[a] = postupnost->pozicie[a];
- }
- free(postupnost->pozicie);
- postupnost->pozicie = pozicie;
- postupnost->limit = postupnost->limit + DLZKA_CESTY;
- }
- postupnost->pozicie[postupnost->pocet] = postupnost2->pozicie[i];
- postupnost->pocet++;
- }
- postupnost->cena = postupnost->cena + postupnost2->cena;
- } else {
- for( i = postupnost2->pocet - 2; i >= 0; i--) {
- int a;
- if(postupnost->pocet == postupnost->limit) {
- POZICIA **pozicie = calloc((postupnost->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < postupnost->pocet; a++) {
- pozicie[a] = postupnost->pozicie[a];
- }
- free(postupnost->pozicie);
- postupnost->pozicie = pozicie;
- postupnost->limit = postupnost->limit + DLZKA_CESTY;
- }
- postupnost->pozicie[postupnost->pocet] = postupnost2->pozicie[i];
- postupnost->pocet++;
- }
- postupnost->cena = postupnost->cena + postupnost2->cena;
- }
- }
- void reHeapUp(OKRAJ *okraj, int index, int findMin) {
- while(index > 0) {
- int parentIndex = (index + 1) / 2 - 1;
- KROK *current = okraj->kroky[index];
- KROK *parent = okraj->kroky[parentIndex];
- if(findMin == 1) {
- if(parent->cena > current->cena) {
- swapKROKy(okraj, index, parentIndex);
- index = parentIndex;
- } else {
- return;
- }
- } else {
- if(parent->cena < current->cena) {
- swapKROKy(okraj, index, parentIndex);
- index = parentIndex;
- } else {
- return;
- }
- }
- }
- }
- void reHeapDown(OKRAJ *okraj, int index, int maxIndex, int findMin) {
- while(index <= maxIndex) {
- int child1Index = (index+1) * 2 - 1 ;
- int child2Index = child1Index + 1;
- KROK *current = okraj->kroky[index];
- KROK *child1 = NULL;
- KROK *child2 = NULL;
- if(child1Index <= maxIndex) child1 = okraj->kroky[child1Index];
- if(child2Index <= maxIndex) child2 = okraj->kroky[child2Index];
- if(findMin == 1) {
- if(child1 != NULL) {
- if(child2 != NULL) {
- if(child1->cena > child2->cena) {
- if(child2->cena < current->cena) {
- swapKROKy(okraj, index, child2Index);
- index = child2Index;
- } else {
- return;
- }
- } else {
- if(child1->cena < current->cena) {
- swapKROKy(okraj, index, child1Index);
- index = child1Index;
- } else {
- return;
- }
- }
- } else {
- if(child1->cena < current->cena) {
- swapKROKy(okraj, index, child1Index);
- index = child1Index;
- } else {
- return;
- }
- }
- } else {
- return;
- }
- } else {
- if(child1 != NULL) {
- if(child2 != NULL) {
- if(child1->cena < child2->cena) {
- if(child2->cena > current->cena) {
- swapKROKy(okraj, index, child2Index);
- index = child2Index;
- } else {
- return;
- }
- } else {
- if(child1->cena > current->cena) {
- swapKROKy(okraj, index, child1Index);
- index = child1Index;
- } else {
- return;
- }
- }
- } else {
- if(child1->cena > current->cena) {
- swapKROKy(okraj, index, child1Index);
- index = child1Index;
- } else {
- return;
- }
- }
- } else {
- return;
- }
- }
- }
- }
- void inicializujPozicie(char **mapa, int n, int m, POZICIA *start, POZICIA *d, POZICIA *vypinac, POSTUPNOST *p, POSTUPNOST **teleporty) {
- int i,j;
- for( i = 0; i < n; i++) {
- for( j = 0; j < m; j++) {
- char c = mapa[i][j];
- //printf("%c",c);
- if(c == 'D') {
- d->x = i;
- d->y = j;
- } else if(c == 'G') {
- vypinac->x = i;
- vypinac->y = j;
- } else if(c == 'P') {
- POZICIA *tmpPoz =malloc(sizeof(POZICIA));
- tmpPoz->x = i;
- tmpPoz->y = j;
- int a;
- if(p->pocet == p->limit) {
- POZICIA **pozicie = calloc((p->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < p->pocet; a++) {
- pozicie[a] = p->pozicie[a];
- }
- free(p->pozicie);
- p->pozicie = pozicie;
- p->limit = p->limit + DLZKA_CESTY;
- }
- p->pozicie[p->pocet] = tmpPoz;
- p->pocet++;
- } else if(('0' <= c) && (c <= '9')) {
- POZICIA *tmpPoz =malloc(sizeof(POZICIA));
- tmpPoz->x = i;
- tmpPoz->y = j;
- int a;
- if(teleporty[c - '0']->pocet == teleporty[c - '0']->limit) {
- POZICIA **pozicie = calloc((teleporty[c - '0']->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < teleporty[c - '0']->pocet; a++) {
- pozicie[a] = teleporty[c - '0']->pozicie[a];
- }
- free(teleporty[c - '0']->pozicie);
- teleporty[c - '0']->pozicie = pozicie;
- teleporty[c - '0']->limit = teleporty[c - '0']->limit + DLZKA_CESTY;
- }
- teleporty[c - '0']->pozicie[teleporty[c - '0']->pocet] = tmpPoz;
- teleporty[c - '0']->pocet++;
- }
- }
- }
- }
- void hladajMozneKROKy(char **mapa, int n, int m
- , KROK ***kroky, POSTUPNOST **teleporty
- , POZICIA *pozFrom, POSTUPNOST *moznePozicie
- , int fungujuTeleporty
- ) {
- moznePozicie->pocet = 0;
- int k,i,j;
- char cFrom = mapa[pozFrom->x][pozFrom->y];
- if((fungujuTeleporty == 1) && ('0' <= cFrom) && (cFrom <= '9')) {
- POSTUPNOST *teleport = teleporty[cFrom - '0'];
- for( k = 0; k < teleport->pocet; k++) {
- int pos = najdivPost(moznePozicie, teleport->pozicie[k]);
- if(pos < 0) {
- int a;
- if(moznePozicie->pocet == moznePozicie->limit) {
- POZICIA **pozicie = calloc((moznePozicie->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < moznePozicie->pocet; a++) {
- pozicie[a] = moznePozicie->pozicie[a];
- }
- free(moznePozicie->pozicie);
- moznePozicie->pozicie = pozicie;
- moznePozicie->limit = moznePozicie->limit + DLZKA_CESTY;
- }
- moznePozicie->pozicie[moznePozicie->pocet] = teleport->pozicie[k];
- moznePozicie->pocet++;
- }
- }
- }
- for( i = -1; i < 2; i++) {
- for( j = -1; j < 2; j++) {
- if((i != 0) && (j != 0)) continue;
- if((i == 0) && (j == 0)) continue;
- int x = pozFrom->x + i;
- int y = pozFrom->y + j;
- if((x < 0) || (y < 0)) continue;
- if((x >= n) || (y >= m)) continue;
- char c = mapa[x][y];
- if(c == 'N') continue;
- int pos = najdivPost(moznePozicie, kroky[x][y]->p);
- if(pos < 0) {
- }
- int a;
- if(moznePozicie->pocet == moznePozicie->limit) {
- POZICIA **pozicie = calloc((moznePozicie->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < moznePozicie->pocet; a++) {
- pozicie[a] = moznePozicie->pozicie[a];
- }
- free(moznePozicie->pozicie);
- moznePozicie->pozicie = pozicie;
- moznePozicie->limit = moznePozicie->limit + DLZKA_CESTY;
- }
- moznePozicie->pozicie[moznePozicie->pocet] = kroky[x][y]->p;
- moznePozicie->pocet++;
- }
- }
- }
- void hladajCestu(char **mapa, int n, int m
- , KROK ***kroky, POSTUPNOST **teleporty, OKRAJ *okraj
- , POZICIA *pozFrom, POSTUPNOST *pozTo
- , int fungujuTeleporty
- ) {
- int i,j;
- for(i = 0; i < n; i++) {
- for(j = 0; j < m; j++) {
- kroky[i][j]->cena = 0;
- kroky[i][j]->z = NULL;
- kroky[i][j]->index = -1;
- kroky[i][j]->hotovo = 0;
- }
- }
- okraj->pocet = 0;
- int b;
- if(okraj->pocet == okraj->limit) {
- KROK **kroky = calloc((okraj->limit+DLZKA_CESTY),sizeof(KROK*));
- for(b = 0; b < okraj->pocet; b++) {
- kroky[b] = okraj->kroky[b];
- }
- free(okraj->kroky);
- okraj->kroky = kroky;
- okraj->limit = okraj->limit + DLZKA_CESTY;
- }
- okraj->kroky[okraj->pocet] = kroky[pozFrom->x][pozFrom->y];
- okraj->pocet++;
- POSTUPNOST *moznePozicie = malloc(sizeof(POSTUPNOST));
- moznePozicie->pozicie = calloc(POCET_TELEPORTOV,sizeof(POZICIA*));
- moznePozicie->pocet = 0;
- moznePozicie->limit = POCET_TELEPORTOV;
- moznePozicie->cena =0;
- while((okraj->pocet > 0) && (pozTo->pocet > 0)) {
- KROK *actual = okraj->kroky[0];
- actual->hotovo = 1;
- okraj->pocet--;
- if(okraj->pocet > 0) {
- okraj->kroky[0] = okraj->kroky[okraj->pocet];
- okraj->kroky[0]->index = 0;
- reHeapDown(okraj, 0, okraj->pocet - 1, 1);
- }
- int pos = najdivPost(pozTo, actual->p);
- if(pos > -1) {
- for( i = pos; i < pozTo->pocet-1; i++) {
- pozTo->pozicie[i] = pozTo->pozicie[i+1];
- }
- pozTo->pocet--;
- }
- hladajMozneKROKy(mapa, n, m, kroky, teleporty, actual->p, moznePozicie, fungujuTeleporty);
- for( i = 0; i < moznePozicie->pocet; i++) {
- POZICIA *nextPos = moznePozicie->pozicie[i];
- KROK *nextKROK = kroky[nextPos->x][nextPos->y];
- if(nextKROK->hotovo == 1) continue;
- int cenaKROKu = 1;
- char c = mapa[nextPos->x][nextPos->y];
- if(c == 'H') cenaKROKu++;
- if(('0' <= c) && (c <= '9') && (fungujuTeleporty == 1)) {
- if(mapa[actual->p->x][actual->p->y] == c) cenaKROKu--;
- }
- if(nextKROK->index > -1) {
- if(nextKROK->cena > (actual->cena + cenaKROKu)) {
- nextKROK->cena = actual->cena + cenaKROKu;
- nextKROK->z = actual->p;
- reHeapUp(okraj, nextKROK->index, 1);
- }
- } else {
- int b;
- if(okraj->pocet == okraj->limit) {
- KROK **kroky = calloc((okraj->limit+DLZKA_CESTY),sizeof(KROK*));
- for(b = 0; b < okraj->pocet; b++) {
- kroky[i] = okraj->kroky[b];
- }
- free(okraj->kroky);
- okraj->kroky = kroky;
- okraj->limit = okraj->limit + DLZKA_CESTY;
- }
- okraj->kroky[okraj->pocet] = nextKROK;
- okraj->pocet++;
- nextKROK->index = okraj->pocet-1;
- nextKROK->z = actual->p;
- nextKROK->cena = actual->cena + cenaKROKu;
- reHeapUp(okraj, okraj->pocet-1, 1);
- }
- }
- }
- }
- void citajCestuZKROKov(KROK ***kroky, POSTUPNOST *cesta, POZICIA *kam) {
- KROK *krok = kroky[kam->x][kam->y];
- if(krok->z != NULL) citajCestuZKROKov(kroky, cesta, krok->z);
- int a;
- if(cesta->pocet == cesta->limit) {
- POZICIA **pozicie = calloc((cesta->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < cesta->pocet; a++) {
- pozicie[a] = cesta->pozicie[a];
- }
- free(cesta->pozicie);
- cesta->pozicie = pozicie;
- cesta->limit = cesta->limit + DLZKA_CESTY;
- }
- cesta->pozicie[cesta->pocet] = krok->p;
- cesta->pocet++;
- cesta->cena = krok->cena;
- }
- int i,j;
- POSTUPNOST ***mapaPOSTUPNOSTi(int size) {
- POSTUPNOST ***mapa = malloc(size*sizeof(POSTUPNOST**));
- for( i = 0; i < size; i++) {
- mapa[i] = malloc(size*sizeof(POSTUPNOST*));
- for( j = 0; j < size; j++) {
- mapa[i][j] = NULL;
- }
- }
- return mapa;
- }
- int calculateCena(POSTUPNOST **array, int size) {
- int suma = 0,i;
- for( i = 0; i < size; i++) {
- if(array[i] == NULL) continue;
- suma = suma + array[i]->cena;
- }
- return suma;
- }
- int combine(POSTUPNOST ***noTele, POSTUPNOST ***tele, int size
- , int index, int fromIndex
- , POSTUPNOST *p, POSTUPNOST *najdene
- , POSTUPNOST **current, POSTUPNOST **minimal, int min
- , int useTele, int useNoTele
- ) {
- int i;
- if((index >= size) || (najdene->pocet >= (size-2))) {
- if(najdene->pocet >= (size-2)) {
- int cena = 0,i;
- for( i = 0; i < size; i++) {
- if(current[i] == NULL) continue;
- cena += current[i]->cena;
- }
- if(cena < min) {
- for( i = 0; i < size; i++) {
- minimal[i] = current[i];
- }
- return cena;
- } else {
- return min;
- }
- } else {
- return min;
- }
- }
- if(useNoTele == 1) {
- for( i = 1; i < size; i++) {
- POSTUPNOST *pos = noTele[fromIndex][i];
- if(pos == NULL) continue;
- if(pos->pocet == 0) continue;
- current[index] = pos;
- if(i > 1) {
- int princezna = najdivPost(najdene, p->pozicie[i-2]);
- if(princezna > -1) continue;
- int a;
- if(najdene->pocet == najdene->limit) {
- POZICIA **pozicie = calloc((najdene->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < najdene->pocet; a++) {
- pozicie[a] = najdene->pozicie[a];
- }
- free(najdene->pozicie);
- najdene->pozicie = pozicie;
- najdene->limit = najdene->limit + DLZKA_CESTY;
- }
- najdene->pozicie[najdene->pocet] = p->pozicie[i-2];
- najdene->pocet++;
- }
- int newUseNoTele = 1;
- if(i == 1) newUseNoTele = 0;
- min = combine(noTele, tele, size
- , index+1, i
- , p, najdene
- , current, minimal, min
- , useTele, newUseNoTele);
- if(i > 1) najdene->pocet--;
- current[index] = NULL;
- }
- }
- if(useTele == 1) {
- for( i = 2; i < size; i++) {
- POSTUPNOST *pos = tele[fromIndex][i];
- if(pos == NULL) continue;
- if(pos->pocet == 0) continue;
- current[index] = pos;
- int princezna = najdivPost(najdene, p->pozicie[i-2]);
- if(princezna > -1) continue;
- int a;
- if(najdene->pocet == najdene->limit) {
- POZICIA **pozicie = calloc((najdene->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < najdene->pocet; a++) {
- pozicie[a] = najdene->pozicie[a];
- }
- free(najdene->pozicie);
- najdene->pozicie = pozicie;
- najdene->limit = najdene->limit + DLZKA_CESTY;
- }
- najdene->pozicie[najdene->pocet] = p->pozicie[i-2];
- najdene->pocet++;
- min = combine(noTele, tele, size
- , index+1, i
- , p, najdene
- , current, minimal, min
- , useTele, useNoTele);
- current[index] = NULL;
- najdene->pocet--;
- }
- }
- return min;
- }
- void printMapa(char **mapa, int n, int m,POSTUPNOST *postupnost) {
- printf(" ---------------------------------------\n");
- POZICIA *poz = malloc(sizeof(POZICIA));
- poz->x=0;
- poz->y=0;
- int i,j,k;
- for( i = 0; i < n; i++) {
- for( j = 0; j < m; j++) {
- char c = mapa[i][j];
- poz->x = i;
- poz->y = j;
- printf("%c",c);
- }
- printf("\n");
- }
- printf(" ---------------------------------------");
- }
- int* zachran_princezne(char **mapa, int n, int m, int t, int *dlzka_cesty) {
- int i,j;
- printMapa(mapa,n,m,NULL);
- POZICIA *start = malloc(sizeof(POZICIA));
- start->x = 0;
- start->y = 0;
- POZICIA *d = malloc(sizeof(POZICIA));
- d->x = -1;
- d->y = -1;
- POZICIA *vypinac = malloc(sizeof(POZICIA));
- vypinac->x = -1;
- vypinac->y = -1;
- POSTUPNOST *p = malloc(sizeof(POSTUPNOST));
- p->pozicie = calloc(POCET_PRINCEZIEN,sizeof(POZICIA*));
- p->pocet = 0;
- p->limit = POCET_PRINCEZIEN;
- p->cena =0;
- POSTUPNOST **teleporty =calloc(POCET_TELEPORTOV,sizeof(POSTUPNOST*));
- for( i = 0; i < POCET_TELEPORTOV; i++) {teleporty[i] = malloc(sizeof(POSTUPNOST));
- teleporty[i]->pozicie = calloc(POCET_TELEPORTOV,sizeof(POZICIA*));
- teleporty[i]->pocet = 0;
- teleporty[i]->limit = POCET_TELEPORTOV;
- teleporty[i]->cena =0;
- }
- POSTUPNOST *vyslednaCesta = malloc(sizeof(POSTUPNOST));
- vyslednaCesta->pozicie = calloc(DLZKA_CESTY,sizeof(POZICIA*));
- vyslednaCesta->pocet = 0;
- vyslednaCesta->limit = DLZKA_CESTY;
- vyslednaCesta->cena =0;
- inicializujPozicie(mapa, n, m, start, d, vypinac, p, teleporty);
- OKRAJ *okraj = malloc(sizeof(OKRAJ));
- okraj->kroky = calloc(DLZKA_CESTY,sizeof(KROK*));
- okraj->pocet = 0;
- okraj->limit = DLZKA_CESTY;
- POSTUPNOST *ciele = malloc(sizeof(POSTUPNOST));
- ciele->pozicie = calloc(4,sizeof(POZICIA*));
- ciele->pocet = 0;
- ciele->limit = 4;
- ciele->cena =0;
- KROK ***kroky = malloc(n*sizeof(KROK**));
- for( i = 0; i < n; i++) {
- kroky[i] = malloc(m*sizeof(KROK*));
- for( j = 0; j < m; j++) {
- kroky[i][j] = malloc(sizeof(KROK));
- kroky[i][j]->p = malloc(sizeof(POZICIA));
- kroky[i][j]->p->x = i;
- kroky[i][j]->p->y = j;
- kroky[i][j]->z = NULL;
- kroky[i][j]->cena = 0;
- kroky[i][j]->index = -1;
- }
- }
- // S -> D, V
- POSTUPNOST *cesta_S_D = malloc(sizeof(POSTUPNOST));
- cesta_S_D->pozicie = calloc(0,sizeof(POZICIA*));
- cesta_S_D->pocet = 0;
- cesta_S_D->limit = 0;
- cesta_S_D->cena =0;
- POSTUPNOST *cesta_S_V = malloc(sizeof(POSTUPNOST));
- cesta_S_V->pozicie = calloc(0,sizeof(POZICIA*));
- cesta_S_V->pocet = 0;
- cesta_S_V->limit = 0;
- cesta_S_V->cena =0;
- POSTUPNOST *cesta_V_D_tele = malloc(sizeof(POSTUPNOST));
- cesta_V_D_tele->pozicie = calloc(0,sizeof(POZICIA*));
- cesta_V_D_tele->pocet = 0;
- cesta_V_D_tele->limit = 0;
- cesta_V_D_tele->cena =0;
- POSTUPNOST *tmpPos = malloc(sizeof(POSTUPNOST));
- tmpPos->pozicie = calloc(DLZKA_CESTY,sizeof(POZICIA*));
- tmpPos->pocet = 0;
- tmpPos->limit = DLZKA_CESTY;
- tmpPos->cena =0;
- POSTUPNOST ***mapaTele = mapaPOSTUPNOSTi(p->pocet + 2);
- POSTUPNOST ***mapaNoTele = mapaPOSTUPNOSTi(p->pocet + 2);
- int ignoreTele = 0;
- if((vypinac->x == -1) || (vypinac->y == -1)) ignoreTele = 1;
- // S -> D, V
- ciele->pocet = 0;
- int a;
- if(ciele->pocet == ciele->limit) {
- POZICIA **pozicie = calloc((ciele->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < ciele->pocet; a++) {
- pozicie[a] = ciele->pozicie[a];
- }
- free(ciele->pozicie);
- ciele->pozicie = pozicie;
- ciele->limit = ciele->limit + DLZKA_CESTY;
- }
- ciele->pozicie[ciele->pocet] = d;
- ciele->pocet++;
- if((vypinac->x != -1) || (vypinac->y != -1)) {
- int a;
- if(ciele->pocet == ciele->limit) {
- POZICIA **pozicie = calloc((ciele->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < ciele->pocet; a++) {
- pozicie[a] = ciele->pozicie[a];
- }
- free(ciele->pozicie);
- ciele->pozicie = pozicie;
- ciele->limit = ciele->limit + DLZKA_CESTY;
- }
- ciele->pozicie[ciele->pocet] = vypinac;
- ciele->pocet++;
- }
- hladajCestu(mapa, n, m, kroky, teleporty, okraj, start, ciele, 0);
- citajCestuZKROKov(kroky, cesta_S_D, d);
- if((vypinac->x != -1) || (vypinac->y != -1)) citajCestuZKROKov(kroky, cesta_S_V, vypinac);
- // V -tele> D, P1, P2, PPOCET_PRINCEZIEN
- if(ignoreTele == 0) {
- ciele->pocet = 0;
- addPOSTUPNOST(ciele, p);
- if((d->x != -1) || (d->y != -1)) {
- int a;
- if(ciele->pocet == ciele->limit) {
- POZICIA **pozicie = calloc((ciele->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < ciele->pocet; a++) {
- pozicie[a] = ciele->pozicie[a];
- }
- free(ciele->pozicie);
- ciele->pozicie = pozicie;
- ciele->limit = ciele->limit + DLZKA_CESTY;
- }
- ciele->pozicie[ciele->pocet] = d;
- ciele->pocet++;
- }
- hladajCestu(mapa, n, m, kroky, teleporty, okraj, vypinac, ciele, 1);
- citajCestuZKROKov(kroky, cesta_V_D_tele, d);
- for( i = 1; i <= p->pocet; i++) {
- tmpPos->pocet = 0;
- citajCestuZKROKov(kroky, tmpPos, p->pozicie[i-1]);
- if(tmpPos->pocet > 0) {
- mapaTele[1][i+1] = tmpPos;
- tmpPos = malloc(sizeof(POSTUPNOST));
- tmpPos->pozicie = calloc(DLZKA_CESTY,sizeof(POZICIA*));
- tmpPos->pocet = 0;
- tmpPos->limit = DLZKA_CESTY;
- tmpPos->cena =0;
- }
- }
- }
- int ignoreNoTele = 0;
- if((ignoreTele == 0) && ((cesta_S_D->cena + 1) > (cesta_S_V->cena + cesta_V_D_tele->cena + 1))) {
- addPOSTUPNOST(vyslednaCesta, cesta_S_V);
- addPOSTUPNOST(vyslednaCesta, cesta_V_D_tele);
- ignoreNoTele = 1;
- } else {
- addPOSTUPNOST(vyslednaCesta, cesta_S_D);
- }
- if(ignoreNoTele == 0) {
- for( i = 0; i < p->pocet+2 ; i++) {
- ciele->pocet = 0;
- addPOSTUPNOST(ciele, p);
- if((vypinac->x != -1) || (vypinac->y != -1)) {
- int a;
- if(ciele->pocet == ciele->limit) {
- POZICIA **pozicie = calloc((ciele->limit+DLZKA_CESTY),sizeof(POZICIA*));
- for( a = 0; a < ciele->pocet; a++) {
- pozicie[a] = ciele->pozicie[a];
- }
- free(ciele->pozicie);
- ciele->pozicie = pozicie;
- ciele->limit = ciele->limit + DLZKA_CESTY;
- }
- ciele->pozicie[ciele->pocet] = vypinac;
- ciele->pocet++;
- }
- POZICIA *fromPoz = d;
- if(i == 1) continue;
- if(i > 1) fromPoz = p->pozicie[i-2];
- hladajCestu(mapa, n, m, kroky, teleporty, okraj, fromPoz, ciele, 0);
- for( j = 0; j < p->pocet + 2; j++) {
- if(i == j) continue;
- POZICIA *toPoz = d;
- if(j == 0) continue;
- if((j == 1) && ((vypinac->x == -1) || (vypinac->y == -1))) continue;
- if(j == 1) toPoz = vypinac;
- if(j > 1) toPoz = p->pozicie[j-2];
- tmpPos->pocet = 0;
- citajCestuZKROKov(kroky, tmpPos, toPoz);
- if(tmpPos->pocet > 0) {
- mapaNoTele[i][j] = tmpPos;
- tmpPos = malloc(sizeof(POSTUPNOST));
- tmpPos->pozicie = calloc(DLZKA_CESTY,sizeof(POZICIA*));
- tmpPos->pocet = 0;
- tmpPos->limit = DLZKA_CESTY;
- tmpPos->cena =0;
- }
- }
- }
- }
- if(ignoreTele == 0) {
- for( i = 0; i < p->pocet+2 ; i++) {
- ciele->pocet = 0;
- if(i == 1) continue;
- addPOSTUPNOST(ciele, p);
- POZICIA *fromPoz = d;
- if(i > 1) fromPoz = p->pozicie[i-2];
- hladajCestu(mapa, n, m, kroky, teleporty, okraj, fromPoz, ciele, 1);
- for( j = 0; j < p->pocet + 2; j++) {
- if(i == j) continue;
- if(j < 2) continue;
- POZICIA *toPoz = p->pozicie[j-2];
- tmpPos->pocet = 0;
- citajCestuZKROKov(kroky, tmpPos, toPoz);
- if(tmpPos->pocet > 0) {
- mapaTele[i][j] = tmpPos;
- tmpPos = malloc(sizeof(POSTUPNOST));
- tmpPos->pozicie = calloc(DLZKA_CESTY,sizeof(POZICIA*));
- tmpPos->pocet = 0;
- tmpPos->limit = DLZKA_CESTY;
- tmpPos->cena =0;
- }
- }
- }
- }
- POSTUPNOST **tmpCesta = malloc((p->pocet+2)*sizeof(POSTUPNOST*));
- POSTUPNOST **minCesta = malloc((p->pocet+2)*sizeof(POSTUPNOST*));
- for( i = 0; i < p->pocet + 2; i++) {
- tmpCesta[i] = NULL;
- minCesta[i] = NULL;
- }
- int useTele = 1;
- int useNoTele = 1;
- if(ignoreNoTele == 1) useNoTele = 0;
- if(ignoreTele == 1) useTele = 0;
- POSTUPNOST *najdenePrincezne = malloc(sizeof(POSTUPNOST));
- najdenePrincezne->pozicie = calloc(POCET_PRINCEZIEN,sizeof(POZICIA*));
- najdenePrincezne->pocet = 0;
- najdenePrincezne->limit = POCET_PRINCEZIEN;
- najdenePrincezne->cena =0;
- combine(mapaNoTele, mapaTele, p->pocet + 2
- , 0, 0
- , p, najdenePrincezne
- , tmpCesta, minCesta, DLZKA_CESTY
- , useTele, useNoTele);
- for( i = 0; i < p->pocet + 2; i++) {
- POSTUPNOST *p = minCesta[i];
- if(p == NULL) continue;
- addPOSTUPNOST(vyslednaCesta, p);
- }
- *dlzka_cesty = vyslednaCesta->pocet;
- int *array = calloc(vyslednaCesta->pocet*2,sizeof(int));
- int num = 0;
- for( i = 0; i < vyslednaCesta->pocet; i++) {
- array[num++] = vyslednaCesta->pozicie[i]->y;
- array[num++] = vyslednaCesta->pozicie[i]->x;
- }
- return array;
- }
- int main() {
- int length;
- int i=0;
- int *res;
- int map_w = 5;
- int map_h = 5;
- char map[5][5] = {
- {'P', 'C', 'N', 'C', 'N'},
- {'C', 'C', '0', 'C', 'P'},
- {'C', '0', 'G', 'H', 'N'},
- {'N', '0', 'N', 'H', 'N'},
- {'N', 'P', 'N', 'N', 'D'}
- };
- char * map_d_r[5] = { map[0], map[1], map[2], map[POCET_PRINCEZIEN], map[4]};
- char ** map_d = map_d_r;
- // char * map_d_r[4] = { map[0], map[1], map[2], map[POCET_PRINCEZIEN]};
- res = zachran_princezne(map_d, map_h, map_w, POCET_TELEPORTOV, &length);
- printf("dlzka cesty %d\n", length);
- for(i=0; i < 2*length; i++) {
- printf("x %d, y %d\n",res[i],res[i+1]);
- i++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement