Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------q(·W·)p--------------------------------------
- //-------------------------------------------------------------------------------------
- // Sacha 15/03/2017
- //-------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------
- #include <stdlib.h>
- #include <stdio.h>
- #define ALTO_DEFAULT 4
- #define ANCHO_DEFAULT 4
- //-------------------------------------------------------------------------------------
- //STRUCTS
- //-------------------------------------------------------------------------------------
- struct matriz{
- char ** m;
- unsigned int h;
- unsigned int w;
- };
- //-------------------------------------------------------------------------------------
- //FUNCIONES
- //-------------------------------------------------------------------------------------
- void buscar_camino(struct matriz*,unsigned int, unsigned int, unsigned int);
- void print_mat(struct matriz*);
- void liberar_mat(struct matriz*);
- struct matriz* copiar_mat_static(struct matriz *, char [ALTO_DEFAULT][ANCHO_DEFAULT]);
- struct matriz* copiar_mat(struct matriz*,struct matriz*);
- //-------------------------------------------------------------------------------------
- //GLOBALES
- //-------------------------------------------------------------------------------------
- char matriz_default[ALTO_DEFAULT][ANCHO_DEFAULT] = {
- {1,1,1,0},
- {0,0,0,0},
- {0,1,1,0},
- {0,0,1,0}
- };
- unsigned int result = 0;
- unsigned int profundidad_max = -1;
- struct matriz mat_ganadora;
- int main(int argc, char ** argv){
- unsigned int i = 0;
- unsigned int j = 0;
- struct matriz mat;
- copiar_mat_static(&mat,matriz_default);
- print_mat(&mat);
- for(i = 0; i < mat.w; i++){
- if(!(mat.m[0][i])){
- buscar_camino(&mat,0,i,1);
- }
- }
- printf("Resultados: %d\tMejor profundidad: %d\n",result,profundidad_max);
- print_mat(&mat_ganadora);
- fflush(stdout);
- liberar_mat(&mat);
- liberar_mat(&mat_ganadora);
- return 0;
- }
- void buscar_camino(struct matriz * mat ,unsigned int i, unsigned int j, unsigned int profundidad){
- if(i == mat->h - 1){
- result++;
- if(profundidad < profundidad_max){
- profundidad_max = profundidad;
- copiar_mat(&mat_ganadora,mat);
- mat_ganadora.m[i][j] = 9;
- }
- return;
- }
- mat->m[i][j] = 9;
- if( i > 0 && !mat->m[i-1][j]){
- buscar_camino(mat,i-1,j,profundidad+1);
- }
- if( j > 0 && !mat->m[i][j-1]){
- buscar_camino(mat,i,j-1,profundidad+1);
- }
- if( j < mat->w - 1 && !mat->m[i][j+1]){
- buscar_camino(mat,i,j+1,profundidad+1);
- }
- if(!mat->m[i+1][j]){
- buscar_camino(mat,i+1,j,profundidad+1);
- }
- mat->m[i][j] = 0;
- }
- void print_mat(struct matriz * mat){
- unsigned int i;
- unsigned int j;
- for(i = 0; i < mat->h; i++){
- for(j = 0; j < mat->w; j++){
- printf("%d",mat->m[i][j]);
- }
- printf("\n");
- }
- }
- void liberar_mat( struct matriz * mat ){
- unsigned int i;
- for(i = 0; i < mat->h; i++){
- free((void*)mat->m[i]);
- }
- free((void*)mat->m);
- }
- struct matriz * copiar_mat_static(struct matriz * dest, char orig[ALTO_DEFAULT][ANCHO_DEFAULT]){
- unsigned int i;
- unsigned int j;
- dest->w = ANCHO_DEFAULT;
- dest->h = ALTO_DEFAULT;
- dest->m = (char**) malloc(dest->h * sizeof(char*));
- for(i = 0; i < dest->h; i++){
- dest->m[i] = (char*) malloc(dest->w * sizeof(char));
- for(j = 0; j < dest->w; j++){
- dest->m[i][j] = orig[i][j];
- }
- }
- return dest;
- }
- struct matriz * copiar_mat(struct matriz * dest, struct matriz * orig){
- unsigned int i;
- unsigned int j;
- dest->h = orig->h;
- dest->w = orig->w;
- dest->m = (char**) malloc(dest->h * sizeof(char*));
- for(i = 0; i < dest->h; i++){
- dest->m[i] = (char*) malloc(dest->w * sizeof(char));
- for(j = 0; j < dest->w; j++){
- dest->m[i][j] = orig->m[i][j];
- }
- }
- return dest;
- }
Advertisement
Add Comment
Please, Sign In to add comment