Skip to main content

Anticlockwise spiral view of diamond matrix elements in vb.net

Anticlockwise spiral view of matrix

Vb program for Anticlockwise spiral view of diamond matrix elements. Here more solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Anticlockwise spiral view of diamond element in matrix
public Class SpiralTraversal
    '  Method which is displaying the Anticlockwise spiral 
    '  Of matrix in diamond(mid) element
    Public Sub spiralView(ByVal matrix As Integer(,),
      ByVal x As Integer, ByVal y As Integer,
        ByVal p As Integer, ByVal q As Integer,
          ByVal k As Integer)
        '  Find the middle column
        Dim midCol As Integer = y + (((q - y) / 2))
        '  Get middle row (valid for odd square matrix)
        Dim midRow As Integer = midCol
        '  Matrix are divided into 4 section
        '  Starting of middle row and column in form of top
        '  Case A
        '  Top to Left-bottom
        Dim i As Integer = midCol
        Dim j As Integer = 0
        while (i > y AndAlso k > 0)
            Console.Write("  {0}", matrix(x + j,i))
            i -= 1
            k -= 1
            j += 1
        End While
        '  Case B
        '  Middle left to middle right bottom
        i = y
        j = 0
        while (i <= midCol AndAlso k > 0)
            Console.Write("  {0}", matrix((midRow) + j,i))
            i += 1
            k -= 1
            j += 1
        End While
        '  Case C
        '  Middle bottom to middle right side 
        i = midCol + 1
        j = 1
        while (i <= q AndAlso k > 0)
            Console.Write("  {0}", matrix((p) - j,i))
            i += 1
            k -= 1
            j += 1
        End While
        '  Case D
        '  Middle right side to  top middle 
        i = q - 1
        j = 1
        while (i > midCol AndAlso k > 0)
            Console.Write("  {0}", matrix((midRow) - j,i))
            i -= 1
            k -= 1
            j += 1
        End While
        if (x + 1 <= p - 1 AndAlso k > 0) Then
            '  Recursive call
            Me.spiralView(matrix, x + 1, 
                          y + 1, p - 1, q - 1, k)
        End If
    End Sub
    Public Sub diamondAnticlockwise(ByVal matrix As Integer(,))
        Dim row As Integer = matrix.GetLength(0)
        Dim col As Integer = matrix.GetLength(1)
        '  Check whether the given matrix is a valid odd shape or not
        if (row <> col OrElse col Mod 2 = 0) Then
            '  When Yes not a valid size
            Console.WriteLine( vbLf &"Not a valid perfect Odd square matrix")
            Return
        End If
        '  (row*col)-((col+1)/2)*4 are provide the value of number of 
        '  Diamond element
        Me.spiralView(matrix, 0, 0, row - 1, 
                      col - 1, (row * col) - (((col + 1) / 2)) * 4)
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As SpiralTraversal = New SpiralTraversal()
        '  Define matrix 
        Dim matrix As Integer(,) =
        {
          {1, 2, 3, 4, 5},
          {6, 7, 8, 9, 10},
          {11, 12, -1, 15, 16},
          {17, 18, 19, 20, 21},
          {22, 23, 24, 25, 26}
		}
        task.diamondAnticlockwise(matrix)
    End Sub
End Class

Output

  3  7  11  18  24  20  16  9  8  12  19  15  -1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment