A spiral matrix is a matrix of m rows and n columns with each cell containing an integer value. The traversal of a spiral matrix is a way of visiting every cell in the matrix in a spiral order, starting from the top-left corner and moving in a clockwise direction.
To traverse a spiral matrix, we start by visiting the top row of the matrix from left to right. Next, we visit the rightmost column of the matrix from top to bottom. Then, we visit the bottom row of the matrix from right to left. Finally, we visit the leftmost column of the matrix from bottom to top. We repeat this process for the remaining inner submatrix until there are no more cells left to visit.
Here is an example of a 4×4 spiral matrix and its traversal:
Matrix:
[ 1 2 3 4 ]
[ 12 13 14 5 ]
[ 11 16 15 6 ]
[ 10 9 8 7 ]
Traversal: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
The traversal order starts at the top-left corner, moves right to the end of the first row, then down to the last column, left to the second-to-last row, up to the second column, and so on until all cells have been visited.
fun spiralTraversal(matrix: Array<IntArray>): List<Int> {
val m = matrix.size
val n = matrix[0].size
val res = mutableListOf<Int>()
var rowStart = 0
var rowEnd = m - 1
var colStart = 0
var colEnd = n - 1
while (rowStart <= rowEnd && colStart <= colEnd) {
// Traverse the top row from left to right
for (i in colStart..colEnd) {
res.add(matrix[rowStart][i])
}
rowStart++
// Traverse the right column from top to bottom
for (i in rowStart..rowEnd) {
res.add(matrix[i][colEnd])
}
colEnd--
// Traverse the bottom row from right to left
if (rowStart <= rowEnd) {
for (i in colEnd downTo colStart) {
res.add(matrix[rowEnd][i])
}
rowEnd--
}
// Traverse the left column from bottom to top
if (colStart <= colEnd) {
for (i in rowEnd downTo rowStart) {
res.add(matrix[i][colStart])
}
colStart++
}
}
return res
}- First, we create an empty mutable list
resultto store the spiral order of the matrix. If the input matrix is empty, we immediately return the empty result. - We then set up four variables
top,bottom,left, andrightto represent the boundaries of the matrix. We initializetopandleftto 0, andbottomandrightto the last row and column of the matrix respectively. - We start a
whileloop that runs as long astopis less than or equal tobottom, andleftis less than or equal toright. This loop will traverse the matrix in a spiral pattern. - Inside the loop, we use a
forloop to traverse the top row of the current spiral, fromlefttoright. We add each element to theresultlist. - We increment
topto move down to the next row of the spiral. - We use another
forloop to traverse the right column of the current spiral, fromtoptobottom. We add each element to theresultlist. - We decrement
rightto move left to the next column of the spiral. - We use an
ifstatement to check if there are more rows in the spiral. If so, we use aforloop to traverse the bottom row of the current spiral, fromrighttoleft. We add each element to theresultlist. - We decrement
bottomto move up to the next row of the spiral. - We use another
ifstatement to check if there are more columns in the spiral. If so, we use aforloop to traverse the left column of the current spiral, frombottomtotop. We add each element to theresultlist. - We increment
leftto move right to the next column of the spiral. - The
whileloop continues until all elements in the matrix have been traversed. At the end, we return theresultlist containing the spiral order of the matrix.
fun spiralTraversal(matrix: Array<IntArray>): List<Int> {
val m = matrix.size
val n = matrix[0].size
val res = mutableListOf<Int>()
var rowStart = 0
var rowEnd = m - 1
var colStart = 0
var colEnd = n - 1
while (rowStart <= rowEnd && colStart <= colEnd) {
// Traverse the top row from left to right
for (i in colStart..colEnd) {
res.add(matrix[rowStart][i])
}
rowStart++
// Traverse the right column from top to bottom
for (i in rowStart..rowEnd) {
res.add(matrix[i][colEnd])
}
colEnd--
// Traverse the bottom row from right to left
if (rowStart <= rowEnd) {
for (i in colEnd downTo colStart) {
res.add(matrix[rowEnd][i])
}
rowEnd--
}
// Traverse the left column from bottom to top
if (colStart <= colEnd) {
for (i in rowEnd downTo rowStart) {
res.add(matrix[i][colStart])
}
colStart++
}
}
return res
}
fun main() {
val matrix = arrayOf(
intArrayOf(1, 2, 3, 4),
intArrayOf(5, 6, 7, 8),
intArrayOf(9, 10, 11, 12),
intArrayOf(13, 14, 15, 16)
)
val result = spiralTraversal(matrix)
println(result.joinToString(", "))
}Output : 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10