Skip to content

Commit aa01550

Browse files
committed
solve
1 parent 91db469 commit aa01550

File tree

2 files changed

+98
-117
lines changed

2 files changed

+98
-117
lines changed
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
1+
#include "grid_path.h"
32
#include "dictionnary_lookup.h"
43

5-
void solve(int taille_mot, int rows, int cols, char *lettres, FILE *dico_lex);
4+
// Définition de la structure de données représentant la grille de caractères
5+
// Structure for the grid of characters
6+
// Structure for the grid of characters
7+
typedef struct
8+
{
9+
int rows;
10+
int cols;
11+
char *letters;
12+
} Grid;
13+
14+
// Linked list node structure
15+
typedef struct list List;
16+
struct list
17+
{
18+
List *next;
19+
void *data;
20+
};

server/dictionnary/src/solve.c

Lines changed: 80 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,134 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4-
#include <stdbool.h>
54
#include "include/solve.h"
6-
7-
85
/**
96
* @brief Renvoi les mots valides de la grilel présents dans le dico.lex
7+
* @bug marche pas (too bad)
108
**/
119

1210

13-
// Définition de la structure de données représentant la grille de caractères
14-
typedef struct
11+
// Create a new node for the linked list
12+
List *list_create(void *data)
1513
{
16-
int rows;
17-
int cols;
18-
char *letters;
19-
} Grid;
20-
21-
22-
char **create_matrix(int num_lines, int num_columns, char **lettres) {
23-
char **matrix = malloc(num_lines * sizeof(char *));
24-
for (int i = 0; i < num_lines; i++) {
25-
matrix[i] = malloc(num_columns * sizeof(char));
26-
}
27-
int k = 0;
28-
for (int i = 0; i < num_lines; i++) {
29-
for (int j = 0; j < num_columns; j++) {
30-
matrix[i][j] = lettres[0][k];
31-
k++;
32-
}
14+
List *list = malloc(sizeof(list));
15+
if (list)
16+
{
17+
list->data = data;
18+
list->next = NULL;
3319
}
34-
return matrix;
20+
return list;
3521
}
3622

37-
38-
void solve(int taille_mot, int rows, int cols, char *lettres, FILE *dico_lex) {
39-
40-
printf("taille_mot : %d \n", taille_mot);
41-
printf("num_lines : %d \n", cols);
42-
printf("num_columns : %d \n", rows);
43-
printf("lettres : %s \n", lettres);
44-
printf("dico_lex : %s \n", dico_lex);
45-
46-
char **matrix = create_matrix(rows, cols, &lettres);
47-
//Print matrix
48-
for (int i = 0; i < rows; i++) {
49-
for (int j = 0; j < cols; j++) {
50-
printf("%c ", matrix[i][j]);
51-
}
52-
printf("\n");
53-
}
54-
23+
// Add a new node to the front of the linked list
24+
List *add(List *old, void *data)
25+
{
26+
List *list = list_create(data);
27+
if (list)
28+
list->next = old;
29+
return list;
5530
}
5631

32+
#define MAX_WORD_SIZE 256
5733

58-
void findAllPossibleWordsInGrid(Grid *grid, int row, int col, char *word, int len, FILE *dico_lex) {
59-
60-
int index = 0;
61-
62-
// Creer la liste des mots trouvés
63-
int n = 1000;
64-
char** mots_trouves = malloc(n * sizeof(char*));
65-
66-
for (int i = 0; i < n; i++) {
67-
mots_trouves[i] = NULL;
68-
}
69-
34+
void searchWordInGrid(Grid *grid, int row, int col, char *path, int current_word_len, int MIN_WORD_LEN, List **list_all_words, FILE *dico_lex)
35+
{
36+
if (current_word_len + 1 >= MIN_WORD_LEN)
37+
return;
7038

39+
if (row < 0 || row >= grid->rows || col < 0 || col >= grid->cols)
40+
return;
7141

72-
// On ajoute la lettre actuelle au mot
73-
word[len] = grid->letters[row*grid->cols + col];
74-
len++;
42+
path[current_word_len] = grid->letters[row * grid->cols + col];
7543

76-
if (dictionnary_lookup(dico_lex, word) == 0) {
77-
printf("Mot trouvé : %s \n", word);
78-
// get the size of word
79-
int size = strlen(word);
80-
// allocate memory for the word
81-
mots_trouves[index] = malloc(size * sizeof(char));
82-
strcpy(mots_trouves[index], word);
83-
index++;
44+
if (current_word_len + 1 >= 2 && dictionnary_lookup(dico_lex, path) == 0)
45+
{
46+
char *word = strdup(path);
47+
if (!word)
48+
return;
49+
printf("%s", word);
50+
printf(" curr word len : %d \n", current_word_len + 1);
51+
*list_all_words = add(*list_all_words, word);
8452
}
8553

86-
// On affiche le mot actuel
87-
printf("%s \n", word);
88-
89-
90-
// On marque la case actuelle comme visitée
91-
grid->letters[row*grid->cols + col] = '*';
92-
93-
// On parcourt les 8 cases adjacentes
94-
for (int i = row-1; i <= row+1; i++) {
95-
for (int j = col-1; j <= col+1; j++) {
96-
// On vérifie que la case est dans la grille
97-
if (i >= 0 && i < grid->rows && j >= 0 && j < grid->cols) {
98-
// On vérifie que la case n'a pas déjà été visitée
99-
if (grid->letters[i*grid->cols + j] != '*') {
100-
// On appelle la fonction récursive
101-
findAllPossibleWordsInGrid(grid, i, j, word, len, dico_lex);
102-
}
54+
for (int i = -1; i <= 1; i++)
55+
{
56+
for (int j = -1; j <= 1; j++)
57+
{
58+
int x = row + i;
59+
int y = col + j;
60+
if (x >= 0 && x < grid->rows && y >= 0 && y < grid->cols)
61+
{
62+
searchWordInGrid(grid, x, y, path, current_word_len + 1, MIN_WORD_LEN, list_all_words, dico_lex);
10363
}
10464
}
10565
}
106-
107-
// On démarque la case actuelle
108-
grid->letters[row*grid->cols + col] = word[len-1];
109-
110-
111-
for (int i = 0; i < n; i++) {
112-
if (mots_trouves[i] != NULL) {
113-
printf("Mot trouvé : %s \n", mots_trouves[i]);
114-
}
115-
}
116-
66+
path[current_word_len] = 0;
11767
}
11868

69+
void findAllWordInGrid(Grid *grid, int start_row, int start_col, List **list_all_words, FILE *dico_lex, int taille_mot)
70+
{
71+
if (!grid || !dico_lex)
72+
return;
73+
char path[MAX_WORD_SIZE] = {0};
74+
// Recursively check all possible paths starting from (start_row, start_col)
75+
searchWordInGrid(grid, start_row, start_col, path, 0, taille_mot, list_all_words, dico_lex);
76+
}
11977

120-
78+
/**
79+
* @bug : MARCHE PAS
80+
***/
12181
int main(int argc, char const *argv[])
12282
{
123-
if (argc < 6) {
83+
if (argc < 6)
84+
{
12485
printf("Usage: %s <dico.lex> <taille_mot> <num_lignes> <num_columns>\n", argv[0]);
12586
return 1;
12687
}
12788

128-
12989
FILE *dico_LEX = fopen(argv[1], "rb");
13090
if (dico_LEX == NULL)
13191
{
13292
printf("Erreur : Erreur lors de l'ouverture du fichier, le chemin spécifié (%s) est-il correct ? \n", argv[1]);
13393
return 0;
13494
}
135-
13695
int taille_mot = atoi(argv[2]);
13796
int rows = atoi(argv[3]);
97+
printf("rows : %d \n", rows);
13898
int cols = atoi(argv[4]);
139-
char lettres[argc-4];
140-
99+
printf("cols : %d \n", cols);
141100
Grid grid;
142101
grid.rows = rows;
143102
grid.cols = cols;
144-
grid.letters = malloc(rows*cols*sizeof(char));
145-
103+
grid.letters = malloc(rows * cols * sizeof(char));
104+
char letters[argc - 4];
105+
for (int i = 5; i < argc; i++)
106+
{
107+
if ((i - 1) % 4 == 0 && i != 5)
108+
printf("\n");
146109

147-
for (int i =5; i<argc; i++) {
110+
letters[i - 5] = argv[i][0];
148111
if (strcmp(argv[i], "QU") == 0)
149-
lettres[i-5] = *"~";
150-
else
151-
lettres[i-5] = argv[i][0];
112+
{
113+
letters[i - 5] = '&';
114+
printf("%c ", letters[i - 5]);
115+
continue;
116+
}
117+
printf("%c ", letters[i - 5]);
118+
letters[i - 5] = argv[i][0];
152119
}
120+
strcpy(grid.letters, letters);
121+
122+
List *list_all_words = NULL;
153123

154-
strcpy(grid.letters, lettres);
155-
printf("\n");
156-
// Affichage de la grille
157-
printf("Grille de %d lignes et %d colonnes: ", rows, cols);
158-
for (size_t i = 0; i < rows*cols; i++)
124+
for (int i = 0; i < rows; i++)
159125
{
160-
printf("%c ", grid.letters[i]);
126+
for (int j = 0; j < cols; j++)
127+
{
128+
findAllWordInGrid(&grid, i, j, &list_all_words, dico_LEX, taille_mot);
129+
}
161130
}
162-
printf("\n");
163-
164-
findAllPossibleWordsInGrid(&grid, rows, cols, lettres, taille_mot, dico_LEX);
165131

166-
solve(taille_mot, rows, cols, lettres, dico_LEX);
167132

133+
// solve(taille_mot, num_lines, num_columns, lettres, dico_LEX);
168134
}

0 commit comments

Comments
 (0)