36int dx[] = {-1, 1, 0, 0};
37int dy[] = {0, 0, -1, 1};
53 for (i = 0; i < h; i++) {
54 for (j = 0; j < w; j++) {
55 cage_identifiant[i][j] = -1;
59 for (a = 0; a < h; a++) {
60 for (b = 0; b < w; b++) {
61 if (cage_identifiant[a][b] == -1) {
62 char cage_type = cage[a][b];
63 cage_identifiant[a][b] = prochain_id;
65 int **queue = malloc(h * w *
sizeof(
int*));
66 for (c = 0; c < h * w; c++) {
67 queue[c] = malloc(2 *
sizeof(
int));
70 int front = 0, rear = 0;
74 while (front < rear) {
75 int x = queue[front][0], y = queue[front++][1];
77 for (
int d = 0; d < 4; d++) {
78 int nx = x +
dx[d], ny = y +
dy[d];
80 if (nx >= 0 && nx < h && ny >= 0 && ny < w &&
81 cage_identifiant[nx][ny] == -1 && cage[nx][ny] == cage_type) {
82 cage_identifiant[nx][ny] = prochain_id;
84 queue[rear++][1] = ny;
89 for (e = 0; e < h * w; e++) {
116 for (i = 0; i < h; i++) {
117 for (j = 0; j < w; j++) {
118 if (cage_identifiant[i][j] > max_cage_id) {
119 max_cage_id = cage_identifiant[i][j];
123 *nombre_cages = max_cage_id + 1;
125 *cage_sizes = malloc((*nombre_cages) *
sizeof(
int));
127 for (k = 0; k < *nombre_cages; k++) {
128 (*cage_sizes)[k] = 0;
131 for (l = 0; l < h; l++) {
132 for (m = 0; m < w; m++) {
133 int cage_id = cage_identifiant[l][m];
134 (*cage_sizes)[cage_id]++;
138 *tab = malloc((*nombre_cages) *
sizeof(
int**));
140 for (n = 0; n < *nombre_cages; n++) {
141 (*tab)[n] = malloc((*cage_sizes)[n] *
sizeof(
int*));
142 for (o = 0; o < (*cage_sizes)[n]; o++) {
143 (*tab)[n][o] = malloc(2 *
sizeof(
int));
147 int *cage_counter = malloc(*nombre_cages *
sizeof(
int));
149 for (p = 0; p < *nombre_cages; p++) {
153 for (q = 0; q < h; q++) {
154 for (r = 0; r < w; r++) {
155 int cage_id = cage_identifiant[q][r];
156 (*tab)[cage_id][cage_counter[cage_id]][0] = q;
157 (*tab)[cage_id][cage_counter[cage_id]][1] = r;
158 cage_counter[cage_id]++;
181void possible(
int ligne,
int colonne,
int x,
int y,
int cage[],
int** grille,
int*** tab,
int** cage_identifiant,
int possibilite[]) {
182 if (grille[x][y] == 0) {
184 for (i = 0; i < 6; i++) {
185 possibilite[i] = i + 1;
188 for (j = -1; j < 2; j++) {
189 for (k = -1; k < 2; k++) {
190 if(x + j < ligne && y + k < colonne && x+j>-1 && y+k>-1){
191 int val = grille[x + j][y + k];
193 possibilite[val - 1] = 0;
198 int tailleCage = cage[cage_identifiant[x][y]];
200 for (l = tailleCage; l < 6; l++) {
204 for (m = 0; m < tailleCage; m++) {
205 int a = tab[cage_identifiant[x][y]][m][0];
206 int b = tab[cage_identifiant[x][y]][m][1];
207 int val = grille[a][b];
209 possibilite[val - 1] = 0;
215 for (i = 0; i < 6; i++) {
242int resoudre(
int ligne,
int colonne,
int x,
int y,
choix choisi[],
int resolu[][2],
int cage[],
int** grille,
int*** tab,
int** cage_identifiant,
int numero,
int caseRempli,
int decision) {
243 if (caseRempli == ligne * colonne) {
246 int possibilite[6]={0};
247 possible(ligne, colonne, x, y, cage, grille,tab,cage_identifiant,possibilite);
248 int nombrePossibilite = 0;
251 for (i = 0; i < 6; i++) {
252 if (possibilite[i] != 0) {
254 valeur = possibilite[i];
257 if (nombrePossibilite == 0) {
259 for(i=numero-1;resolu[i][0]!=choisi[decision-1].
indice[0] && resolu[i][1]!=choisi[decision-1].
indice[1];i--){
260 grille[resolu[i][0]][resolu[i][1]]=0;
263 int nouvellePossibilite[6]={0};
264 possible(ligne, colonne, choisi[decision-1].indice[0], choisi[decision-1].indice[1], cage, grille,tab,cage_identifiant,nouvellePossibilite);
265 int nouveauNombrePossibilite = 0;
267 for (j = 0; j < 6; j++) {
268 if (nouvellePossibilite[j] != 0) {
269 nouveauNombrePossibilite++;
272 if(choisi[decision-1].nombre!=nouveauNombrePossibilite){
273 int nouvelleValeur=0;
275 for(j=0;j<6 && nouvelleValeur==0;j++){
276 if(nouvellePossibilite[j]!=0){
278 for(k=0;k<choisi[decision-1].
nombre && trouve==0;k++){
279 if(choisi[decision-1].tab[k]==nouvellePossibilite[j]){
284 nouvelleValeur=nouvellePossibilite[j];
288 grille[choisi[decision-1].
indice[0]][choisi[decision-1].
indice[1]]=nouvelleValeur;
289 choisi[decision-1].
tab[choisi[decision-1].
nombre]=nouvelleValeur;
290 choisi[decision-1].
nombre++;
293 int x=choisi[decision-1].
indice[0],y=choisi[decision-1].
indice[1];
295 for (l = 0; l < cage[cage_identifiant[x][y]] && trouve == 0; l++) {
296 int tmp_x = tab[cage_identifiant[x][y]][l][0];
297 int tmp_y = tab[cage_identifiant[x][y]][l][1];
298 if (grille[tmp_x][tmp_y] == 0) {
306 for (k = 0; k < 20 && trouve == 0; k++) {
307 for (l = -1 - k; l < 1 + k && trouve == 0; l++) {
308 for (m = -1 - k; m < 1 + k && trouve == 0; m++) {
309 if (x + l < ligne && y + m < colonne && x+l>-1 && y+m>-1 && grille[x + l][y + m] == 0) {
318 resoudre(ligne, colonne, a, b, choisi, resolu, cage, grille, tab, cage_identifiant, i+1, caseRempli, decision);
321 int nouvellePossibilite[6]={0};
322 possible(ligne, colonne, choisi[decision-2].indice[0], choisi[decision-2].indice[1], cage, grille,tab,cage_identifiant,nouvellePossibilite);
323 int nouveauNombrePossibilite = 0;
325 for (j = 0; j < 6; j++) {
326 if (nouvellePossibilite[j] != 0) {
327 nouveauNombrePossibilite++;
332 while(nouveauNombrePossibilite==choisi[decision-j].nombre){
333 for(;resolu[k][0]!=choisi[decision-j].
indice[0] && resolu[k][1]!=choisi[decision-j].
indice[1];k--){
334 grille[resolu[k][0]][resolu[k][1]]=0;
337 choisi[decision-j].
nombre=0;
338 choisi[decision-j].
indice[0]=0;
339 choisi[decision-j].
indice[1]=0;
341 for(m=0;choisi[decision-j].
tab[m]!=0;m++){
342 choisi[decision-j].
tab[m]=0;
345 int nouvellePossibilite[6]={0};
346 possible(ligne, colonne, choisi[decision-j].indice[0], choisi[decision-j].indice[1], cage, grille,tab,cage_identifiant,nouvellePossibilite);
347 int nouveauNombrePossibilite = 0;
349 for (l = 0; l < 6; l++) {
350 if (nouvellePossibilite[l] != 0) {
351 nouveauNombrePossibilite++;
355 int nouvelleValeur=0;
357 for(m=0;m<6 && nouvelleValeur==0;m++){
358 if(nouvellePossibilite[m]!=0){
360 for(n=0;n<choisi[decision-j+1].
nombre && trouve==0;n++){
361 if(choisi[decision-j+1].tab[n]==nouvellePossibilite[m]){
366 nouvelleValeur=nouvellePossibilite[m];
370 grille[choisi[decision-j+1].
indice[0]][choisi[decision-j+1].
indice[1]]=nouvelleValeur;
371 choisi[decision-j+1].
tab[choisi[decision-j+1].
nombre]=nouvelleValeur;
372 choisi[decision-j+1].
nombre++;
375 int x=choisi[decision-j+1].
indice[0],y=choisi[decision-j+1].
indice[1];
377 for (l = 0; l < cage[cage_identifiant[x][y]] && trouve == 0; l++) {
378 int tmp_x = tab[cage_identifiant[x][y]][l][0];
379 int tmp_y = tab[cage_identifiant[x][y]][l][1];
380 if (grille[tmp_x][tmp_y] == 0) {
388 for (k = 0; k < 20 && trouve == 0; k++) {
389 for (l = -1 - k; l < 1 + k && trouve == 0; l++) {
390 for (m = -1 - k; m < 1 + k && trouve == 0; m++) {
391 if (x + l < ligne && y + m < colonne && x+l>-1 && y+m>-1 && grille[x + l][y + m] == 0) {
400 resoudre(ligne, colonne, a, b, choisi, resolu, cage, grille, tab, cage_identifiant, k+1, caseRempli, decision-j+1);
403 else if (nombrePossibilite == 1) {
404 grille[x][y] = valeur;
410 for (j = 0; j < cage[cage_identifiant[x][y]] && trouve == 0; j++) {
411 int tmp_x = tab[cage_identifiant[x][y]][j][0];
412 int tmp_y = tab[cage_identifiant[x][y]][j][1];
413 if (grille[tmp_x][tmp_y] == 0) {
421 for (k = 0; k < 20 && trouve == 0; k++) {
422 for (l = -1 - k; l < 1 + k && trouve == 0; l++) {
423 for (m = -1 - k; m < 1 + k && trouve == 0; m++) {
424 if (x + l < ligne && y + m < colonne && x+l>-1 && y+m>-1 && grille[x + l][y + m] == 0) {
433 resoudre(ligne, colonne, a, b, choisi, resolu, cage, grille, tab, cage_identifiant, numero + 1, caseRempli + 1, decision);
436 grille[x][y] = valeur;
439 choisi[decision].
tab[0] = valeur;
440 choisi[decision].
tab[1] = 0;
441 choisi[decision].
indice[0] = x;
442 choisi[decision].
indice[1] = y;
443 choisi[decision].
nombre=1;
447 for (j = 0; j < cage[cage_identifiant[x][y]] && trouve == 0; j++) {
448 int tmp_x = tab[cage_identifiant[x][y]][j][0];
449 int tmp_y = tab[cage_identifiant[x][y]][j][1];
450 if (grille[tmp_x][tmp_y] == 0) {
458 for (k = 0; k < 20 && trouve == 0; k++) {
459 for (l = -1 - k; l < 1 + k && trouve == 0; l++) {
460 for (m = -1 - k; m < 1 + k && trouve == 0; m++) {
461 if (x + l < ligne && y + m < colonne && x+l>-1 && y+m>-1 && grille[x + l][y + m] == 0) {
470 resoudre(ligne, colonne, a, b, choisi, resolu, cage, grille, tab, cage_identifiant, numero + 1, caseRempli + 1, decision + 1);
489 printf(
"%d",grille[i][j]);
506 printf(
"\nIndices classés par cages :\n");
508 for (i = 0; i < nombre_cages; i++) {
509 printf(
"Cage %d:\n", i);
510 for (j = 0; j < cage_sizes[i]; j++) {
511 printf(
" (%d, %d)\n", tab[i][j][0], tab[i][j][1]);
527 printf(
"\nIdentifiants des cages :\n");
529 for (i = 0; i < h; i++) {
530 for (j = 0; j < w; j++) {
531 printf(
"%d ", cage_identifiant[i][j]);
539 scanf(
"%d%d", &w, &h); fgetc(stdin);
543 char **cage = malloc(
sizeof(
char*) * h);
544 int **cage_identifiant = malloc(
sizeof(
int*) * h);
545 int ** grille=malloc(
sizeof(
int*)*h);
547 for (i = 0; i < h; i++) {
548 cage[i] = malloc(
sizeof(
char) * w);
549 cage_identifiant[i] = malloc(
sizeof(
int) * w);
550 grille[i]=malloc(
sizeof(
int)*w);
555 for (j = 0; j < h; j++) {
557 scanf(
"%[^\n]", line); fgetc(stdin);
558 for (k= 0; k < w * 3; k += 3) {
559 cage[j][k / 3] = line[k];
561 grille[j][k/3]=line[k+1]-
'0';
598 int casePrecedente[400][2];
608 resoudre(h,w,0,0,decision,casePrecedente,cage_sizes,grille,tab,cage_identifiant,0,caseRempli,0);
614 for (o = 0; o < nombre_cages; o++) {
615 for (p = 0; p < cage_sizes[o]; p++) {
623 for (q = 0; q < h; q++) {
625 free(cage_identifiant[q]);
629 free(cage_identifiant);
int resoudre(int ligne, int colonne, int x, int y, choix choisi[], int resolu[][2], int cage[], int **grille, int ***tab, int **cage_identifiant, int numero, int caseRempli, int decision)
complète la grille de suguru
void afficher_grille(int **grille, int h, int w)
affiche la grille
void classifier_indices_par_cages(int **cage_identifiant, int h, int w, int ****tab, int *nombre_cages, int **cage_sizes)
crée un tableau rangé par indice de cages avec tout les indices des cases présents dans la cage
void creer_cages_identifiants(char **cage, int h, int w, int **cage_identifiant)
crée une grille rempli avec les numéros de cages à tous les indices
void possible(int ligne, int colonne, int x, int y, int cage[], int **grille, int ***tab, int **cage_identifiant, int possibilite[])
renvoie les possibilités de chiffres dans une case
void afficher_cages(int **cage_identifiant, int h, int w)
affiche la grille des cages
void afficher_indices_par_cages(int ***tab, int nombre_cages, int *cage_sizes)
affiche tous les indices des cases d'une même cage