void sud::cherche(bool debug) { int ret, bon; int i, j, k; sud C; bool fini; while (!(ret = affecte(bon, debug))) { if (debug) { cerr << "bon : " << bon << endl; //affich(); } } switch (ret) { case -1 : if (debug) { cerr << "Argh !" << endl; } break; case 1 : if (debug) { cerr << "Ok pas fini" << endl; } fini = false; for (i = 0; i < N && !fini; i++) for (j = 0; j < N; j++) { cerr << i << ':' << j << ':' << V[i][j] << " ! "; if (V[i][j] == 0) { fini = true; break; } } for (k = 0; k < N; k++) if (B[i][j][k]) { copier(C); C.V[i][j] = k + 1; C.simplif(i,j); if (debug) { cerr << "trouve 5 : "; cerr << "V[" << i << "][" << j<< "] = " << k + 1 << endl; affich(); C.affich(); } //C.affich(); C.cherche(debug); } break; case 2 : if (debug) { cerr << "Ok fini" << endl; cerr << "bon : " << bon << endl; affich(); } break; default : cerr << "???" << endl; } cerr << endl; }
// Supprime la valeur contenue dans la case (i,j) des lignes, colonnes // et sous-carres correspondant void sud::simplif(int i, int j, bool debug) { int k, l; if (debug) { affich(); cerr << "simplif : " << i << ':' << j << endl; } for (k = 0; k < N; k++) { B[i][k][V[i][j]-1] = false; B[k][j][V[i][j]-1] = false; B[i][j][k] = false; } for (k = (i / rN) * rN; k < (i / rN) * rN + rN; k++) for (l = (j / rN) * rN; l < (j / rN) * rN + rN; l++) B[k][l][V[i][j]-1] = false; }
// Affecte les valeurs sures (une seule possibilite) et detecte les // impossibilites : // Retour : -1 impossibilite (pas de valeurs possibles) // 0 au moins une affectation // 1 pas d'affectation // 2 fini int sud::affecte(int & nb_bon, bool debug) { int ret = 1; int i, j, k, o, p; int nb, nbc, nbl, kv, kc, kl; nb_bon = N2; for (i = 0; i < N; i++) for (j = 0; j < N; j++) if (V[i][j] == 0) nb_bon--; //cout << "bon deb : " << nb_bon << endl; for (i = 0; i < N; i++) for (j = 0; j < N; j++) { if (V[i][j] == 0) { nb = 0; for (k = 0; k < N; k++) // nb valeurs possibles if (B[i][j][k]) { nb++; kv = k+1; } if (nb == 0) { if (debug) cerr << "PB 1 : " << i << ':' << j << endl; return -1; } if (nb == 1) { ret = 0; V[i][j] = kv; nb_bon++; if (debug) { cerr << "trouve 1 : "; cerr << "V[" << i << "][" << j<< "] = " << kv << endl; affich(); } if (nb_bon == N2) return 2; simplif(i, j); } for (k = 0; k < N; k++) { // nb possibilites pour une valeur nbl = nbc = 0; for (o = 0; o < N; o++) { if (V[o][j] == k + 1) // Ignorer les cas ou la valeur est deja rangee ! nbc = 2; if (B[o][j][k]) { kc = o; nbc++; } if (V[i][o] == k + 1) nbl = 2; if (B[i][o][k]) { kl = o; nbl++; } } if (debug) cerr << i << ':' << j << ':' << k << ':' << nbc << ':' << nbl << endl; if (nbc == 0) { if (debug) cerr << "PB 2 : " << i << ':' << j << ':' << kc + 1 << endl; return -1; } if (nbc == 1 && V[kc][j] == 0) { ret = 0; V[kc][j] = k + 1; nb_bon++; if (debug) { cerr << "trouve 2 : "; cerr << "V[" << kc << "][" << j<< "] = " << k + 1 << endl; affich(); } if (nb_bon == N2) return 2; simplif(kc, j); } if (nbl == 0) { if (debug) cerr << "PB 3 : " << i << ':' << j << ':' << k + 1 << endl; return -1; } if (nbl == 1 && V[i][kl] == 0) { ret = 0; V[i][kl] = k + 1; nb_bon++; if (debug) { cerr << "trouve 3 : "; cerr << "V[" << i << "][" << kl<< "] = " << k + 1 << endl; affich(); } if (nb_bon == N2) return 2; simplif(i, kl); } nb = 0; for (o = (i / rN) * rN; o < (i / rN) * rN + rN; o++) for (p = (j / rN) * rN; p < (j / rN) * rN + rN; p++) { if (B[o][p][k]) { kc = o; kl = p; nb++; } if (V[o][p] == k + 1) nb = 2; } if (nb == 0) { if (debug) cerr << "PB 4 : " << i << ':' << j << ':' << kv + 1 << endl; return -1; } if (nb == 1 && V[kc][kl] == 0) { ret = 0; V[kc][kl] = k + 1; nb_bon++; if (debug) { cerr << "trouve 4 : "; cerr << "V[" << i << "][" << j<< "] = " << kv + 1 << endl; affich(); } if (nb_bon == N2) return 2; simplif(kc, kl); } } } } return ret; }
int main (int argc, char * argv[]) { char expr[50]; double xmax,ymax; int x,y; int tab[400] = {0}; int choix; /*partie pour l'evaluation de la fonction*/ printf("-------------------------------\n"); printf("1°) lignes \n"); printf("2°) points \n"); printf("-------------------------------\n"); scanf("%d",&choix); getc(stdin); printf("Veuillez rentrer les tailles max de votre fenetre xmax,ymax \n"); scanf("%lf %lf",&xmax,&ymax); getc(stdin); printf("Veuillez rentrer l'expression desire\n"); fgets(expr,49,stdin); calcul(expr,xmax,ymax,tab); dpy = XOpenDisplay(0); if(!dpy) { printf("souci à l'ouverture de la fenetre \n"); exit(EXIT_FAILURE); } ecran = DefaultScreen(dpy); root = DefaultRootWindow(dpy); /* ------- Recup. couleur de l'avant plan et arriere plan ------------ */ bpx = BlackPixel(dpy,ecran); wpx = WhitePixel(dpy,ecran); /* ------- Creation de la fenetre: Epaiss 6 pixels, L 400 H 400 ------ */ win = XCreateSimpleWindow(dpy,root,0,0,400,400,6,wpx,bpx); XStoreName(dpy,win,"TP 7: Graphisme 3D sous X-Windows"); /* ------- Definition des evenements qui les concernent -------------- */ XSelectInput(dpy,win,ButtonPressMask | ButtonReleaseMask | Button1MotionMask | KeyPressMask | ExposureMask ); /*---------------------------------------------------------------------*/ XMapWindow(dpy,win); /* ------- Creation des contextes standards -------------------------- */ gcv.foreground = wpx; /* Contexte standard */ gcv.background = bpx; gcv.line_width = 1; gcv.function = GXcopy; gcmask = GCForeground | GCBackground | GCLineWidth | GCFunction; gcontext = XCreateGC(dpy,win,gcmask,&gcv); /* ------------------------------------------------------------------- */ while(!sortie) { XNextEvent(dpy,&ev); switch(ev.type) { case Expose : affich(expr,tab,choix); break; case KeyPress : sortie = 1; break; } } XCloseDisplay(dpy); return(EXIT_SUCCESS); }