Von |
Jörg |
Datum |
26. Februar 2021 um 21:07:43 |
Antwort |
Hallo, der Solver funktioniert, aber:
habe die Warnung: "Die Funktion gibt nicht für alle Codepfade einen Wert zurück" Wie kann ich das beheben?Function SolveSudoku(matrix(,) As Integer, hmatrix(,) As String) As Boolean Dim row As Integer, col As Integer, num As Integer, k As Integer, h As Integer, startrow As Integer, startcol As Integer, s As Integer, f As Integer Dim checkBlankSpaces As Boolean = False 'Kandidaten je Matrix-Zelle, die nicht eine vorgegebene Zahl enthält, ermitteln For row = 0 To 8 For col = 0 To 8 'Zeileninfo If (hmatrix(row, col)).Length > 1 Then For k = 0 To 8 If (hmatrix(row, k)).Length = 1 Then hmatrix(row, col) = hmatrix(row, col).Replace(hmatrix(row, k), "") End If Next End If If hmatrix(row, col).Length = 1 Then Exit For If (hmatrix(row, col)).Length = 0 Then MessageBox.Show("Rätsel nicht lösbar.") Exit Function End If 'Spalteninfo If (hmatrix(row, col)).Length > 1 Then For k = 0 To 8 If (hmatrix(k, col)).Length = 1 Then hmatrix(row, col) = hmatrix(row, col).Replace(hmatrix(k, col), "") End If Next End If If hmatrix(row, col).Length = 1 Then Exit For If (hmatrix(row, col)).Length = 0 Then MessageBox.Show("Rätsel nicht lösbar.") Exit Function End If 'Quadratinfo If (hmatrix(row, col)).Length > 1 Then startrow = row - (row Mod 3) startcol = col - (col Mod 3) For h = startrow To startrow + 2 For k = startcol To startcol + 2 If (hmatrix(h, k)).Length = 1 Then hmatrix(row, col) = hmatrix(row, col).Replace(hmatrix(h, k), "") End If Next Next End If If hmatrix(row, col).Length = 1 Then Exit For If (hmatrix(row, col)).Length = 0 Then MessageBox.Show("Rätsel nicht lösbar.") Exit Function End If Next Next ' verify If sudoku Is already solved And If Not solved, ' get Next "blank" space position For row = 0 To 8 For col = 0 To 8 If matrix(row, col) = UNASSIGNED Then checkBlankSpaces = True Exit For End If Next col If checkBlankSpaces = True Then Exit For End If Next row ' no more "blank" spaces means the puzzle Is solved If checkBlankSpaces = False Then Return True End If 'Try To fill "blank" space With correct num 'anzahl schritte s = hmatrix(row, col).Length For f = 0 To s - 1 num = CInt(hmatrix(row, col).Substring(f, 1)) 'isSafe checks that num isn't already present 'In the row, column, Or 3x3 box (see below) If IsSafe(matrix, row, col, num) Then matrix(row, col) = num If SolveSudoku(matrix, hmatrix) = True Then Return True End If 'If num Is placed In incorrect position, 'mark As "blank" again Then backtrack With 'a different num matrix(row, col) = UNASSIGNED End If Next Return False End Function |
|