1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
Option Explicit
Type chemin
aboutir As Boolean
itineraire As String
End Type
Enum Direction
[_First] = 1
DiagonaleHaut = 1
Avant = 2
DiagonaleBas = 3
[_Last] = 3
End Enum
Sub ListeDesSolutions()
Dim epsilon As Long
Dim str As String
Dim r As Integer
Dim sh As Worksheet
Dim r_min As Integer
Dim r_max As Integer
Dim c_min As Integer
Dim c_max As Integer
Dim che As chemin
' Ecart maximum accepté entre chaque valeur
Do
str = InputBox("Veuillez saisir la tolérance")
If IsNumeric(str) Then
epsilon = CLng(str)
If epsilon > 0 Then
Exit Do
End If
End If
Loop While True
Set sh = ActiveSheet
r_min = sh.UsedRange.Row
r_max = sh.UsedRange.Row + sh.UsedRange.Rows.Count - 1
c_min = sh.UsedRange.Column
c_max = sh.UsedRange.Column + sh.UsedRange.Columns.Count - 1
If c_max = c_min Then Exit Sub
For r = r_min To r_max
che = ChercherChemin(sh, epsilon, r, c_min, r_min, c_min + 1, r_max, c_max)
If che.aboutir Then Debug.Print sh.Cells(r, 1).Address & ";" & che.itineraire
Next r
Set sh = Nothing
End Sub
Function ChercherChemin(ByRef sh As Worksheet, _
epsilon As Long, _
r As Integer, c As Integer, _
r_min As Integer, c_min As Integer, _
r_max As Integer, c_max As Integer) As chemin
Dim str As String
Dim val As Double
Dim r_ctrl As Integer
Dim c_ctrl As Integer
Dim che As chemin
Dim d As Direction
If c = c_max Then
che.aboutir = True
che.itineraire = ""
ChercherChemin = che
Exit Function
End If
If c + 1 <= c_max Then
c_ctrl = c + 1
val = sh.Cells(r, c)
For d = Direction.[_First] To Direction.[_Last]
r_ctrl = 0
If d = Direction.DiagonaleHaut And r - 1 >= r_min Then r_ctrl = r - 1
If d = Direction.Avant Then r_ctrl = r
If d = Direction.DiagonaleBas And r + 1 <= r_max Then r_ctrl = r + 1
If r_ctrl > 0 Then
If Abs(val - sh.Cells(r_ctrl, c_ctrl)) <= epsilon Then
che = ChercherChemin(sh, epsilon, r_ctrl, c_ctrl, r_min, c_min + 1, r_max, c_max)
If che.aboutir Then
che.itineraire = sh.Cells(r_ctrl, c_ctrl).Address & ";" & che.itineraire
ChercherChemin = che
Exit Function
End If
End If
End If
Next d
End If
che.aboutir = False
che.itineraire = ""
ChercherChemin = che
End Function |
Partager