Advertisement
Guest User

Generar matriz

a guest
Nov 12th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. #define FILAS_COLUMNAS 3
  6. #define MAX_NUM FILAS_COLUMNAS*FILAS_COLUMNAS
  7.  
  8. int **avance(int **matriz, int x, int y, int numero);
  9.  
  10. int main() {
  11.     int x, y;
  12.     int numero = 1;
  13.    
  14.     // creamos una matriz nula
  15.     int **matriz = malloc(sizeof(int*)*FILAS_COLUMNAS);
  16.     for(x = 0; x < FILAS_COLUMNAS; x++) {
  17.         matriz[x] = malloc(sizeof(int) * 3);
  18.         // limpiamos la matriz
  19.         for(y = 0; y < FILAS_COLUMNAS; y++) matriz[x][y] = 0;
  20.     }
  21.    
  22.     // establecemos la posicion inicial, aleatoria
  23.     srand(time(NULL));
  24.     x = rand() % FILAS_COLUMNAS;
  25.     y = rand() % FILAS_COLUMNAS;
  26.     matriz[x][y] = numero;
  27.    
  28.     matriz = avance(matriz, x, y, 2);
  29.    
  30.     // imprimimos la matriz
  31.     for(x = 0; x < FILAS_COLUMNAS; x++){
  32.         for(y = 0; y < FILAS_COLUMNAS; y++) printf("%d", matriz[x][y]);
  33.         printf("\n");
  34.     }
  35.    
  36.     return 0;
  37. }
  38.  
  39. int terminado(int **matriz) {
  40.     int x, y;
  41.  
  42.     for(x = 0; x < FILAS_COLUMNAS; x++) {
  43.         for(y = 0; y < FILAS_COLUMNAS; y++) {
  44.             // si hay un valor de 0, no ha terminado
  45.             if(matriz[x][y] == 0) return 0;
  46.         }
  47.     }
  48.    
  49.     return 1;
  50. }
  51.  
  52. // empleamos esta funcion recursiva para evitar soluciones que se queden 'encalladas'
  53. int **avance(int **matriz, int x, int y, int numero) {
  54.     int z, w;  
  55.     int **matrizSend = matriz;
  56.     /* esto es para hazer la decision de donde ir aleatoria
  57.        si no, siempre primero intentaria subir, luego bajar...
  58.    
  59.        si da priorizarSubir=1, priorizarIzquierda=0, priorizarVertical=0
  60.        primero intentara ir a la derecha (horizontal, sin priorizar izquierda),
  61.        luego a la izquierda, luego arriba (priorizar subir), luego abajo        */
  62.     int priorizarSubir = rand() % 2, priorizarIzquierda = rand() % 2, priorizarVertical = rand() % 2;
  63.    
  64.     if(numero > MAX_NUM) return matriz;
  65.    
  66.     for(z = 0; z < 4; z++) {
  67.         if((priorizarVertical == 1 && z < 2) || (priorizarVertical == 0 && z >= 2)) {
  68.             if((priorizarSubir == 1 && z%2 == 0) || (priorizarSubir == 0 && z%2 == 1)) {
  69.                 // puedes subir?
  70.                 if(x > 0 && matriz[x-1][y] == 0) {
  71.                     matrizSend = matriz;
  72.                     matrizSend[x-1][y] = numero;
  73.                    
  74.                     matrizSend = avance(matrizSend, x-1, y, numero+1);
  75.                    
  76.                     if(terminado(matrizSend)) return matrizSend;
  77.                 }
  78.             }
  79.             else {
  80.                 // puedes bajar?
  81.                 if(x < FILAS_COLUMNAS - 1 && matriz[x+1][y] == 0) {
  82.                     matrizSend = matriz;
  83.                     matrizSend[x+1][y] = numero;
  84.                    
  85.                     matrizSend = avance(matrizSend, x+1, y, numero+1);
  86.                    
  87.                     if(terminado(matrizSend)) return matrizSend;
  88.                 }
  89.             }
  90.         }
  91.         else {
  92.             if((priorizarIzquierda == 1 && z%2 == 0) || (priorizarIzquierda == 0 && z%2 == 1)) {
  93.                 // puedes ir a la izquierda?
  94.                 if(y > 0 && matriz[x][y-1] == 0) {
  95.                     matrizSend = matriz;
  96.                     matrizSend[x][y-1] = numero;
  97.                    
  98.                     matrizSend = avance(matrizSend, x, y-1, numero+1);
  99.                    
  100.                     if(terminado(matrizSend)) return matrizSend;
  101.                 }
  102.             }
  103.             else {
  104.                 // puedes ir a la derecha?
  105.                 if(y < FILAS_COLUMNAS - 1 && matriz[x][y+1] == 0) {
  106.                     matrizSend = matriz;
  107.                     matrizSend[x][y+1] = numero;
  108.                    
  109.                     matrizSend = avance(matrizSend, x, y+1, numero+1);
  110.                    
  111.                     if(terminado(matrizSend)) return matrizSend;
  112.                 }
  113.             }
  114.         }
  115.     }
  116.    
  117.     matriz[x][y] = 0;
  118.     return matriz;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement