Somme minimale pour traverser un carré en diagonale
Une grille carrée étant donnée, remplie d'entiers positifs, on souhaite traverser le carré avec une somme minimale des entiers rencontrés.
Pour cet exercice,
- on souhaite aller du coin supérieur gauche au coin inférieur droit,
- les seuls déplacements autorisés sont →, ↓
\[
\begin{pmatrix}
\mathbf{131} & 673 & 234 & 103 & 18 \\
\mathbf{201} & \mathbf{96} & \mathbf{342} & 965 & 150 \\
630 & 803 & \mathbf{746} & \mathbf{422} & 111 \\
537 & 699 & 497 & \mathbf{121} & 956 \\
805 & 732 & 524 & \mathbf{37} & \mathbf{331}\\
\end{pmatrix}
\]
Le chemin marqué en gras donne une somme de \(2427\) qui est ici le minimum possible pour cet exercice.
Écrire une fonction telle que somme_minimale_1(grille) renvoie la somme minimale spécifiée plus haut. On garantit que la grille est carrée et remplie d'entiers positifs.
Exemples
\[
\begin{pmatrix}
\mathbf{1} & \mathbf{5} & 9 \\
10 & \mathbf{3} & 5 \\
10 & \mathbf{2} & \mathbf{3}
\end{pmatrix}
\]
🐍 Console Python
>>> grille = [[1, 5, 9], [10, 3, 5], [10, 2, 3]]
>>> somme_minimale_1(grille)
14
\[
\begin{pmatrix}
\mathbf{131} & 673 & 234 & 103 & 18 \\
\mathbf{201} & \mathbf{96} & \mathbf{342} & 965 & 150 \\
630 & 803 & \mathbf{746} & \mathbf{422} & 111 \\
537 & 699 & 497 & \mathbf{121} & 956 \\
805 & 732 & 524 & \mathbf{37} & \mathbf{331}\\
\end{pmatrix}
\]
🐍 Console Python
>>> grille = [
... [131, 673, 234, 103, 18],
... [201, 96, 342, 965, 150],
... [630, 803, 746, 422, 111],
... [537, 699, 497, 121, 956],
... [805, 732, 524, 37, 331],
... ]
>>> somme_minimale_1(grille)
2427
Tronquer ou non le feedback dans les terminaux (sortie standard & stacktrace / relancer le code pour appliquer)
Si activé, le texte copié dans le terminal est joint sur une seule ligne avant d'être copié dans le presse-papier
.128013]x,59/f.78rnb _o=ylaepcwgu)vd4613kmhtsP(S0+2[j-i:050D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6/3#4;6!0:5h6e2X5!6g5F6^4A6B0v6D755|72040T7g016668796F6c7d3,5Q7F5m5o6C7y63707u7A7M0^5D6@6a6p7I2_5a5 7R7w7y6z6.7O326c7x7*3u7Q7.3U7E6f7m6t3u6)5,7U716z0U7X5@0^6?5S6y6`6|0m6~7#747C6a777#7c7}7u6z4=7t7+4%7j3=7^6x7o6A5p7#7-8m4/0^807;5g7S816G4{8F6z5~5c6 8y7=0^8x8d7~837#0b8Q3}8t8B8N8D047T856F7W8i3n6`4G8l8R7u4P8c8X6V7p8v8C568W4g8Y8U8h8(8^8u7L8{6#7$965/8Z6s6o0^0V8F8f997v0b5i7@7n6a5)2m0L0A0l8/2 7_7e947r8w7y5u9f9i9h8!568%3=1b4i0v2t2U9M401s422w2z2u0u1J9P0g410`9Z0!0$0(04.
# Tests(insensible à la casse)(Ctrl+I)
(Alt+: ; Ctrl pour inverser les colonnes)
(Esc)