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
result
to 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
, andright
to represent the boundaries of the matrix. We initializetop
andleft
to 0, andbottom
andright
to the last row and column of the matrix respectively. - We start a
while
loop that runs as long astop
is less than or equal tobottom
, andleft
is less than or equal toright
. This loop will traverse the matrix in a spiral pattern. - Inside the loop, we use a
for
loop to traverse the top row of the current spiral, fromleft
toright
. We add each element to theresult
list. - We increment
top
to move down to the next row of the spiral. - We use another
for
loop to traverse the right column of the current spiral, fromtop
tobottom
. We add each element to theresult
list. - We decrement
right
to move left to the next column of the spiral. - We use an
if
statement to check if there are more rows in the spiral. If so, we use afor
loop to traverse the bottom row of the current spiral, fromright
toleft
. We add each element to theresult
list. - We decrement
bottom
to move up to the next row of the spiral. - We use another
if
statement to check if there are more columns in the spiral. If so, we use afor
loop to traverse the left column of the current spiral, frombottom
totop
. We add each element to theresult
list. - We increment
left
to move right to the next column of the spiral. - The
while
loop continues until all elements in the matrix have been traversed. At the end, we return theresult
list 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