sahchas

Pathfinding 0

Mar 15th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. //----------------------------------------q(·W·)p--------------------------------------
  2. //-------------------------------------------------------------------------------------
  3. // Sacha 15/03/2017
  4. //-------------------------------------------------------------------------------------
  5. //-------------------------------------------------------------------------------------
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. #define ALTO_DEFAULT 4
  11. #define ANCHO_DEFAULT 4
  12.  
  13. //-------------------------------------------------------------------------------------
  14. //STRUCTS
  15. //-------------------------------------------------------------------------------------
  16. struct matriz{
  17.     char ** m;
  18.     unsigned int h;
  19.     unsigned int w;
  20. };
  21.  
  22. //-------------------------------------------------------------------------------------
  23. //FUNCIONES
  24. //-------------------------------------------------------------------------------------
  25.  
  26. void buscar_camino(struct matriz*,unsigned int, unsigned int, unsigned int);
  27.  
  28. void print_mat(struct matriz*);
  29.  
  30. void liberar_mat(struct matriz*);
  31.  
  32. struct matriz* copiar_mat_static(struct matriz *, char [ALTO_DEFAULT][ANCHO_DEFAULT]);
  33.  
  34. struct matriz* copiar_mat(struct matriz*,struct matriz*);
  35.  
  36. //-------------------------------------------------------------------------------------
  37. //GLOBALES
  38. //-------------------------------------------------------------------------------------
  39.  
  40. char matriz_default[ALTO_DEFAULT][ANCHO_DEFAULT] = {
  41.     {1,1,1,0},
  42.     {0,0,0,0},
  43.     {0,1,1,0},
  44.     {0,0,1,0}
  45. };
  46.  
  47. unsigned int  result = 0;
  48. unsigned int  profundidad_max = -1;
  49.  
  50. struct matriz mat_ganadora;
  51.  
  52. int main(int argc, char ** argv){
  53.  
  54.     unsigned int  i = 0;
  55.     unsigned int  j = 0;
  56.     struct matriz mat;
  57.  
  58.     copiar_mat_static(&mat,matriz_default);
  59.  
  60.     print_mat(&mat);
  61.  
  62.     for(i = 0; i < mat.w; i++){
  63.         if(!(mat.m[0][i])){
  64.             buscar_camino(&mat,0,i,1);
  65.         }
  66.     }
  67.  
  68.     printf("Resultados: %d\tMejor profundidad: %d\n",result,profundidad_max);
  69.  
  70.     print_mat(&mat_ganadora);
  71.  
  72.     fflush(stdout);
  73.  
  74.     liberar_mat(&mat);
  75.     liberar_mat(&mat_ganadora);
  76.  
  77.     return 0;
  78. }
  79.  
  80. void buscar_camino(struct matriz * mat ,unsigned int i, unsigned int j, unsigned int profundidad){
  81.  
  82.     if(i == mat->h - 1){
  83.         result++;
  84.         if(profundidad < profundidad_max){
  85.             profundidad_max = profundidad;
  86.             copiar_mat(&mat_ganadora,mat);
  87.             mat_ganadora.m[i][j] = 9;
  88.         }
  89.         return;
  90.     }
  91.  
  92.     mat->m[i][j] = 9;
  93.  
  94.     if( i > 0 && !mat->m[i-1][j]){
  95.         buscar_camino(mat,i-1,j,profundidad+1);
  96.     }
  97.  
  98.     if( j > 0 && !mat->m[i][j-1]){
  99.         buscar_camino(mat,i,j-1,profundidad+1);
  100.     }
  101.  
  102.     if( j < mat->w - 1 && !mat->m[i][j+1]){
  103.         buscar_camino(mat,i,j+1,profundidad+1);
  104.     }
  105.  
  106.     if(!mat->m[i+1][j]){
  107.         buscar_camino(mat,i+1,j,profundidad+1);
  108.     }
  109.  
  110.     mat->m[i][j] = 0;
  111.  
  112. }
  113.  
  114. void print_mat(struct matriz * mat){
  115.  
  116.     unsigned int i;
  117.     unsigned int j;
  118.  
  119.     for(i = 0; i < mat->h; i++){
  120.         for(j = 0; j < mat->w; j++){
  121.             printf("%d",mat->m[i][j]);
  122.         }
  123.         printf("\n");
  124.     }
  125.  
  126. }
  127.  
  128. void liberar_mat( struct matriz * mat ){
  129.  
  130.     unsigned int i;
  131.  
  132.     for(i = 0; i < mat->h; i++){
  133.         free((void*)mat->m[i]);
  134.     }
  135.     free((void*)mat->m);
  136.  
  137. }
  138.  
  139. struct matriz * copiar_mat_static(struct matriz * dest, char orig[ALTO_DEFAULT][ANCHO_DEFAULT]){
  140.  
  141.     unsigned int i;
  142.     unsigned int j;
  143.  
  144.     dest->w = ANCHO_DEFAULT;
  145.     dest->h = ALTO_DEFAULT;
  146.  
  147.     dest->m = (char**) malloc(dest->h * sizeof(char*));
  148.  
  149.     for(i = 0; i < dest->h; i++){
  150.         dest->m[i] = (char*) malloc(dest->w * sizeof(char));
  151.         for(j = 0; j < dest->w; j++){
  152.             dest->m[i][j] = orig[i][j];
  153.         }
  154.     }
  155.  
  156.     return dest;
  157.  
  158. }
  159.  
  160. struct matriz * copiar_mat(struct matriz * dest, struct matriz * orig){
  161.  
  162.     unsigned int i;
  163.     unsigned int j;
  164.  
  165.     dest->h = orig->h;
  166.     dest->w = orig->w;
  167.  
  168.     dest->m = (char**) malloc(dest->h * sizeof(char*));
  169.  
  170.     for(i = 0; i < dest->h; i++){
  171.         dest->m[i] = (char*) malloc(dest->w * sizeof(char));
  172.         for(j = 0; j < dest->w; j++){
  173.             dest->m[i][j] = orig->m[i][j];
  174.         }
  175.     }
  176.  
  177.     return dest;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment