1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VRAI 1
#define FAUX 0
#define CHEMIN 0
#define MUR 1
#define ENTREE 2
#define SORTIE 3
#define PERSO 4
#define BOARD_W 8
#define BOARD_H 6
//Définitions de structure
typedef struct Coordonnees{
int x;
int y;
}Coordonnees;
//Définition des fonctions
void display(int board[][BOARD_W]);
void init(int matrix[][BOARD_W], int* vector);
int find_path(int board[][BOARD_W], int fromX, int fromY, Coordonnees *resol[], int* taille_reso);
//Fonction principale, contient le corps de la résolution de chemin
int main(int argc, char** argv){
//Initialisation du plateau test
int tested_value[BOARD_H * BOARD_W] = {
1,1,1,1,1,1,1,1,
1,1,0,0,0,0,1,1,
2,0,0,1,1,0,1,1,
1,0,1,1,1,0,0,3,
1,0,1,1,1,1,1,1,
1,1,1,1,1,1,1,1
};
int board[BOARD_H][BOARD_W] = {0};
init(board,tested_value);
display(board);
int taille_path = 0;
int* ptr_taille = &taille_path;
Coordonnees* tab_res[100] = {0};
printf("Board[2][0] = %d\n",board[2][0]);
find_path(board, 2, 0, tab_res, ptr_taille);
return 0;
}
//Fonction copiant le contenu d'un vecteur dans une matrice
void init(int matrix[][BOARD_W], int* vector){
int i,j,k = 0;
for(i = 0; i < BOARD_H; i++){
for(j = 0; j < BOARD_W; j++){
matrix[i][j] = vector[k++];
}
}
}
//Fonction d'affichage du tableau
void display(int board[][BOARD_W]){
int i,j = 0;
for(i = 0; i < BOARD_H; i++){
for(j = 0; j < BOARD_W; j++){
printf(" %d ",board[i][j]);
}
printf("\n");
}
}
int find_path(int board[][BOARD_W], int fromY, int fromX, Coordonnees *resol[], int* taille_reso){
int suite = FAUX;
display(board);
printf("Démarrage d'une récursion\n");
printf("FromX = %d, FromY = %d\n",fromY,fromX);
printf("board[%d][%d] = %d\n",fromY,fromX,board[fromY][fromX]);
printf("taille_reso : %d\n",*taille_reso);
//Test Sortie
if (board[fromY][fromX] == SORTIE)
return VRAI;
//Test haut
if (fromY - 1>=0 && board[fromY - 1][fromX] == CHEMIN){
printf("Test haut\n");
suite = VRAI;
resol[*taille_reso] = (Coordonnees*) malloc(sizeof(Coordonnees));
resol[*taille_reso]->x = fromX;
resol[*taille_reso]->y = fromY - 1;
*taille_reso++;
if (!find_path(board, fromY - 1, fromX, resol, taille_reso)){
suite = FAUX;
free(resol[*taille_reso]);
*taille_reso--;
}
}
//Test bas
if (fromY + 1< BOARD_H && board[fromY + 1][fromX] == CHEMIN){
printf("Test bas\n");
suite = VRAI;
printf("Resol : %p\n",resol[*taille_reso]);
resol[*taille_reso] = (Coordonnees*) malloc(sizeof(Coordonnees));
resol[*taille_reso]->x = fromX;
resol[*taille_reso]->y = fromY + 1;
*taille_reso++;
if (!find_path(board, fromY + 1, fromX, resol, taille_reso)){
suite = FAUX;
free(resol[*taille_reso]);
*taille_reso--;
}
}
//Test gauche
if (fromX - 1 >=0 && board[fromY][fromX - 1] == CHEMIN){
printf("Test gauche\n");
suite = VRAI;
printf("Resol[%d] : %p\n",*taille_reso,resol[*taille_reso]);
//printf("Resol[%d]->x = %d\n",*taille_reso,resol[*taille_reso]->x);
resol[*taille_reso] = (Coordonnees*) malloc(sizeof(Coordonnees));
resol[*taille_reso]->x = fromX-1;
resol[*taille_reso]->y = fromY;
*taille_reso++;
if (!find_path(board, fromY, fromX - 1, resol, taille_reso)){
suite = FAUX;
free(resol[*taille_reso]);
*taille_reso--;
}
}
//Test droite : si l'on peut aller à droite
if (fromX + 1< BOARD_H && board[fromY][fromX + 1] == CHEMIN){
printf("Test droite\n");
suite = VRAI;
//On ajoute les coordonnees de la case de droite à la solution
resol[*taille_reso] = (Coordonnees*) malloc(sizeof(Coordonnees));
resol[*taille_reso]->x = fromX+1;
resol[*taille_reso]->y = fromY;
*taille_reso++;
//Si le chemin passant par cette nouvelle case ne mène pas à la sortie
if (!find_path(board, fromY, fromX+1, resol, taille_reso)){
//On enlève ses coordonnees du chemin solution
suite = FAUX;
free(resol[*taille_reso]);
*taille_reso--;
}
}
//Test impasse
if (suite == FAUX)
return FAUX;
} |
Partager