Skip to content
Snippets Groups Projects
Verified Commit f6f2995f authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added figs

parent 92e0221e
No related branches found
No related tags found
No related merge requests found
Pipeline #35884 passed
...@@ -896,5 +896,201 @@ rien quicksort(entier tableau[], entier ind_min, entier ind_max) ...@@ -896,5 +896,201 @@ rien quicksort(entier tableau[], entier ind_min, entier ind_max)
\log_2(N)$. \log_2(N)$.
* En moyenne: $\mathcal{O}(N\cdot \log_2(N))$. * En moyenne: $\mathcal{O}(N\cdot \log_2(N))$.
# Le problème des 8-reines
\Huge Le problème des 8-reines
# Problème des 8-reines
* Placer 8 reines sur un échiquier de $8 \times 8$.
* Sans que les reines ne puissent se menacer mutuellement (92 solutions).
## Conséquence
* Deux reines ne partagent pas la même rangée, colonne, ou diagonale.
* Donc chaque solution a **une** reine **par colonne** ou **ligne**.
## Généralisation
* Placer $N$ reines sur un échiquier de $N \times
N$.
- Exemple de **backtracking** (retour en arrière) $\Rightarrow$ récursivité.
![Problème des 8-reines. Source:
[wikipedia](https://fr.wikipedia.org/wiki/Problème_des_huit_dames)](./figs/fig_recursivite_8_reines.png){width=35%}
# Problème des 2-reines
![Le problème des 2 reines n'a pas de solution.](figs/2reines.svg){width=50%}
# Comment trouver les solutions?
* On pose la première reine sur la première case disponible.
* On rend inaccessibles toutes les cases menacées.
* On pose la reine suivante sur la prochaine case non-menacée.
* Jusqu'à ce qu'on puisse plus poser de reine.
* On revient alors en arrière jusqu'au dernier coup où il y avait plus qu'une
possibilité de poser une reine.
* On recommence depuis là.
. . .
* Le jeu prend fin quand on a énuméré *toutes* les possibilités de poser les
reines.
# Problème des 3-reines
![Le problème des 3 reines n'a pas de solution non plus.](figs/3reines.svg)
# Problème des 4-reines
![Le problème des 4 reines a une solution.](figs/4reines.svg)
# Problème des 4-reines, symétrie
![Le problème des 4 reines a une autre solution (symétrie
horizontale).](figs/4reines_sym.svg)
# Problème des 5 reines
## Exercice: Trouver une solution au problème des 5 reines
* Faire une capture d'écran / une photo de votre solution et la poster sur
matrix.
```C
```
# Quelques observations sur le problème
* Une reine par colonne au plus.
* On place les reines sur des colonnes successives.
* On a pas besoin de "regarder en arrière" (on place "devant" uniquement).
* Trois étapes:
* On place une reine dans une case libre.
* On met à jour le tableau.
* Quand on a plus de cases libres on "revient dans le temps" ou c'est qu'on
a réussi.
# Le code du problème des 8 reines (1/5)
## Quelle structure de données?
. . .
Une matrice de booléens fera l'affaire:
```C
bool board[n][n];
```
## Quelles fonctionnalités?
. . .
```C
// Pour chaque ligne placer la reine sur toutes les colonnes
// et compter les solutions
void nbr_solutions(board, column, counter);
// Copier un tableau dans un autre
void copy(board_in, board_out);
// Placer la reine à li, co et rendre inaccessible devant
void placer_devant(board, li, co);
```
# Le code du problème des 8 reines (2/5)
## Le calcul du nombre de solutions
```C
// Calcule le nombre de solutions au problème des <n> reines
rien nbr_solutions(board, column, count)
pour chaque ligne
si la case libre
si column < n - 1
copier board dans un "new" board,
y poser une reine
et mettre à jour ce "new" board
nbr_solutions(new_board, column+1, count)
sinon
on a posé la n-ème et on a gagné
count += 1
```
# Le code du problème des 8 reines (3/5)
## Le calcul du nombre de solutions
```C
// Placer une reine et mettre à jour
rien placer_devant(board, ligne, colonne)
board est occupé à ligne/colonne
toutes les cases des colonnes
suivantes sont mises à jour
```
# Le code du problème des 8 reines (4/5)
## Compris? Alors écrivez le code et postez le!
. . .
## Le nombre de solutions
\footnotesize
```C
// Calcule le nombre de solutions au problème des <n> reines
void nb_sol(int n, bool board[n][n], int co, int *ptr_cpt) {
for (int li = 0; li < n; li++) {
if (board[li][co]) {
if (co < n-1) {
bool new_board[n][n]; // alloué à chaque nouvelle tentative
copy(n, board, new_board);
prises_devant(n, new_board, li, co);
nb_sol(n, new_board, co+1, ptr_cpt);
} else {
*ptr_cpt = (*ptr_cpt)+1;
}
}
}
}
```
# Le code du problème des 8 reines (5/5)
\footnotesize
## Placer devant
```C
// Retourne une copie du tableau <board> complété avec les positions
// prises sur la droite droite par une reine placée en <board(li,co)>
void placer_devant(int n, bool board[n][n], int li, int co) {
board[li][co] = false; // position de la reine
for (int j = 1; j < n-co; j++) {
// horizontale et diagonales à droite de la reine
if (j <= li) {
board[li-j][co+j] = false;
}
board[li][co+j] = false;
if (li+j < n) {
board[li+j][co+j] = false;
}
}
}
```
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="95.24704mm"
height="95.24704mm"
viewBox="0 0 95.24704 95.24704"
version="1.1"
id="svg8"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"
sodipodi:docname="2reines.svg">
<defs
id="defs2">
<marker
inkscape:stockid="Arrow2Lend"
orient="auto"
refY="0"
refX="0"
id="Arrow2Lend"
style="overflow:visible"
inkscape:isstock="true">
<path
id="path1126"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
inkscape:connector-curvature="0" />
</marker>
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="1"
inkscape:cx="371.08618"
inkscape:cy="90.967382"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:window-width="1884"
inkscape:window-height="1052"
inkscape:window-x="36"
inkscape:window-y="0"
inkscape:window-maximized="1">
<inkscape:grid
type="xygrid"
id="grid815"
originx="-18.680396"
originy="-59.695988" />
</sodipodi:namedview>
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-18.680395,-142.05697)">
<g
id="g1101"
transform="translate(-31.537804,-47.306706)"
style="stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:none">
<g
id="g916"
style="stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:none">
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,189.61368 h 47.37352"
id="path821"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,236.9872 h 47.37352"
id="path821-3"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 50.468199,236.9872 V 189.61368"
id="path821-6-7"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 97.841719,236.9872 V 189.61368"
id="path821-6-7-3"
inkscape:connector-curvature="0" />
</g>
<g
id="g916-5"
transform="translate(47.37352)"
style="stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:none">
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,189.61368 h 47.37352"
id="path821-6"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,236.9872 h 47.37352"
id="path821-3-2"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 50.468199,236.9872 V 189.61368"
id="path821-6-7-9"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 97.841719,236.9872 V 189.61368"
id="path821-6-7-3-1"
inkscape:connector-curvature="0" />
</g>
<g
id="g916-2"
transform="translate(0,47.37352)"
style="stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:none">
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,189.61368 h 47.37352"
id="path821-7"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,236.9872 h 47.37352"
id="path821-3-0"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 50.468199,236.9872 V 189.61368"
id="path821-6-7-93"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 97.841719,236.9872 V 189.61368"
id="path821-6-7-3-6"
inkscape:connector-curvature="0" />
</g>
<g
id="g916-0"
transform="translate(47.37352,47.37352)"
style="stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:none">
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,189.61368 h 47.37352"
id="path821-62"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 50.468199,236.9872 h 47.37352"
id="path821-3-6"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 50.468199,236.9872 V 189.61368"
id="path821-6-7-1"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 97.841719,236.9872 V 189.61368"
id="path821-6-7-3-8"
inkscape:connector-curvature="0" />
</g>
<text
id="text975"
y="224.14017"
x="65.009895"
style="font-size:30.53443909px;line-height:1.25;font-family:'Accanthis ADF Std';-inkscape-font-specification:'Accanthis ADF Std';letter-spacing:0px;word-spacing:0px;stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:none"
xml:space="preserve"><tspan
style="stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:none"
y="224.14017"
x="65.009895"
id="tspan973"
sodipodi:role="line">R</tspan></text>
</g>
<path
style="fill:#999999;stroke:none;stroke-width:1.88976383;stroke-miterlimit:4;stroke-dasharray:none"
d="m 252.28925,627.12968 v -88 h 88 88 v 88 88 h -88 -88 z"
id="path1411"
inkscape:connector-curvature="0"
transform="scale(0.26458333)" />
<path
style="fill:#999999;stroke:none;stroke-width:1.88976383;stroke-miterlimit:4;stroke-dasharray:none"
d="m 73.289246,806.12968 v -88 h 88.000004 88 v 88 88 h -88 -88.000004 z"
id="path1413"
inkscape:connector-curvature="0"
transform="scale(0.26458333)" />
<path
style="fill:#999999;stroke:none;stroke-width:1.88976383;stroke-miterlimit:4;stroke-dasharray:none"
d="m 252.28925,806.12968 v -88 h 88 88 v 88 88 h -88 -88 z"
id="path1415"
inkscape:connector-curvature="0"
transform="scale(0.26458333)" />
</g>
</svg>
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
slides/figs/fig_recursivite_8_reines.png

10.9 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment