Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to check if a 3x3 grid is a valid Sudoku grid in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

' Sudoku solution must satisfy all of the following rules:
' Each of the digits 1-9 must occur once in each row.
' Each of the digits 1-9 must occur once in each column.
' Each of the digits 1-9 must occur once in each 3x3 grid.

Class SudokuValidator
    Public Shared Function IsValidSudoku3x3Grid(ByVal grid As Integer(,)) As Boolean
        Dim rows As Integer = grid.GetLength(0)
        Dim cols As Integer = grid.GetLength(1)
		
        If rows <> 3 OrElse cols <> 3 Then Return False
        
		Dim seen As HashSet(Of Integer) = New HashSet(Of Integer)()

        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                Dim num As Integer = grid(i, j)
                If num < 1 OrElse num > 9 OrElse seen.Contains(num) Then Return False
                seen.Add(num)
            Next
        Next

        Return True
    End Function

	Public Shared Sub Main()
        Dim grid As Integer(,) = {
        {5, 3, 4},
        {6, 7, 2},
        {1, 9, 8}}

        If IsValidSudoku3x3Grid(grid) Then
            Console.WriteLine("The grid is a valid Sudoku grid!")
        Else
            Console.WriteLine("The grid is NOT a valid Sudoku grid!")
        End If
    End Sub
End Class


 
' run
'
' The grid is a valid Sudoku grid!
'

 



answered May 30, 2025 by avibootz

Related questions

1 answer 100 views
1 answer 125 views
1 answer 114 views
1 answer 85 views
1 answer 126 views
...