Imports System
Class GFG
Public Shared Function isSafe(ByVal board As Integer(,), ByVal row As Integer, ByVal col As Integer, ByVal num As Integer) As Boolean
For d As Integer = 0 To board.GetLength(0) - 1
If board(row, d) = num Then
Return False
End If
Next
For r As Integer = 0 To board.GetLength(0) - 1
If board(r, col) = num Then
Return False
End If
Next
Dim sqrt As Integer = CInt(Math.Sqrt(board.GetLength(0)))
Dim boxRowStart As Integer = row - row Mod sqrt
Dim boxColStart As Integer = col - col Mod sqrt
For r As Integer = boxRowStart To boxRowStart + sqrt - 1
For d As Integer = boxColStart To boxColStart + sqrt - 1
If board(r, d) = num Then
Return False
End If
Next
Next
Return True
End Function
Public Shared Function solveSudoku(ByVal board As Integer(,), ByVal n As Integer) As Boolean
Dim row As Integer = -1
Dim col As Integer = -1
Dim isEmpty As Boolean = True
For i As Integer = 0 To n - 1
For j As Integer = 0 To n - 1
If board(i, j) = 0 Then
row = i
col = j
isEmpty = False
Exit For
End If
Next
If Not isEmpty Then
Exit For
End If
Next
If isEmpty Then
Return True
End If
For num As Integer = 1 To n
If isSafe(board, row, col, num) Then
board(row, col) = num
If solveSudoku(board, n) Then
Return True
Else
board(row, col) = 0
End If
End If
Next
Return False
End Function
Public Shared Sub print(ByVal board As Integer(,), ByVal N As Integer)
For r As Integer = 0 To N - 1
For d As Integer = 0 To N - 1
Console.Write(board(r, d))
Console.Write(" ")
Next
Console.Write(vbLf)
If (r + 1) Mod CInt(Math.Sqrt(N)) = 0 Then
Console.Write("")
End If
Next
End Sub
Public Shared Sub Main(ByVal args As String())
Dim board As Integer(,) = New Integer(,) {
{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}}
Dim N As Integer = board.GetLength(0)
If solveSudoku(board, N) Then
print(board, N)
Else
Console.Write("No solution")
End If
End Sub
End Class
Hi @AmitB
I fixed your formatting for the entire block of code (see how it’s done by editing your post )
Also, see this list of solved topics:
https://forum.uipath.com/search?q=invoke%20code%20status%3Asolved
It should give you enough clues to solve the issue.