Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #define FILAS_COLUMNAS 3
- #define MAX_NUM FILAS_COLUMNAS*FILAS_COLUMNAS
- int **avance(int **matriz, int x, int y, int numero);
- int main() {
- int x, y;
- int numero = 1;
- // creamos una matriz nula
- int **matriz = malloc(sizeof(int*)*FILAS_COLUMNAS);
- for(x = 0; x < FILAS_COLUMNAS; x++) {
- matriz[x] = malloc(sizeof(int) * 3);
- // limpiamos la matriz
- for(y = 0; y < FILAS_COLUMNAS; y++) matriz[x][y] = 0;
- }
- // establecemos la posicion inicial, aleatoria
- srand(time(NULL));
- x = rand() % FILAS_COLUMNAS;
- y = rand() % FILAS_COLUMNAS;
- matriz[x][y] = numero;
- matriz = avance(matriz, x, y, 2);
- // imprimimos la matriz
- for(x = 0; x < FILAS_COLUMNAS; x++){
- for(y = 0; y < FILAS_COLUMNAS; y++) printf("%d", matriz[x][y]);
- printf("\n");
- }
- return 0;
- }
- int terminado(int **matriz) {
- int x, y;
- for(x = 0; x < FILAS_COLUMNAS; x++) {
- for(y = 0; y < FILAS_COLUMNAS; y++) {
- // si hay un valor de 0, no ha terminado
- if(matriz[x][y] == 0) return 0;
- }
- }
- return 1;
- }
- // empleamos esta funcion recursiva para evitar soluciones que se queden 'encalladas'
- int **avance(int **matriz, int x, int y, int numero) {
- int z, w;
- int **matrizSend = matriz;
- /* esto es para hazer la decision de donde ir aleatoria
- si no, siempre primero intentaria subir, luego bajar...
- si da priorizarSubir=1, priorizarIzquierda=0, priorizarVertical=0
- primero intentara ir a la derecha (horizontal, sin priorizar izquierda),
- luego a la izquierda, luego arriba (priorizar subir), luego abajo */
- int priorizarSubir = rand() % 2, priorizarIzquierda = rand() % 2, priorizarVertical = rand() % 2;
- if(numero > MAX_NUM) return matriz;
- for(z = 0; z < 4; z++) {
- if((priorizarVertical == 1 && z < 2) || (priorizarVertical == 0 && z >= 2)) {
- if((priorizarSubir == 1 && z%2 == 0) || (priorizarSubir == 0 && z%2 == 1)) {
- // puedes subir?
- if(x > 0 && matriz[x-1][y] == 0) {
- matrizSend = matriz;
- matrizSend[x-1][y] = numero;
- matrizSend = avance(matrizSend, x-1, y, numero+1);
- if(terminado(matrizSend)) return matrizSend;
- }
- }
- else {
- // puedes bajar?
- if(x < FILAS_COLUMNAS - 1 && matriz[x+1][y] == 0) {
- matrizSend = matriz;
- matrizSend[x+1][y] = numero;
- matrizSend = avance(matrizSend, x+1, y, numero+1);
- if(terminado(matrizSend)) return matrizSend;
- }
- }
- }
- else {
- if((priorizarIzquierda == 1 && z%2 == 0) || (priorizarIzquierda == 0 && z%2 == 1)) {
- // puedes ir a la izquierda?
- if(y > 0 && matriz[x][y-1] == 0) {
- matrizSend = matriz;
- matrizSend[x][y-1] = numero;
- matrizSend = avance(matrizSend, x, y-1, numero+1);
- if(terminado(matrizSend)) return matrizSend;
- }
- }
- else {
- // puedes ir a la derecha?
- if(y < FILAS_COLUMNAS - 1 && matriz[x][y+1] == 0) {
- matrizSend = matriz;
- matrizSend[x][y+1] = numero;
- matrizSend = avance(matrizSend, x, y+1, numero+1);
- if(terminado(matrizSend)) return matrizSend;
- }
- }
- }
- }
- matriz[x][y] = 0;
- return matriz;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement