Golang program to print a matrix in Spiral Format
Draw 2D matrix, print all elements of the given matrix in Spiral format. Given a number n, print a n x n spiral matrix (of numbers from 1 to n x n) in clockwise direction using O(1) space.
Example
package main
import (
"fmt"
)
func spiral(n int) []int {
left, top, right, bottom := 0, 0, n-1, n-1
sz := n * n
s := make([]int, sz)
i := 0
for left < right {
// work right, along top
for c := left; c <= right; c++ {
s[top*n+c] = i
i++
}
top++
// work down right side
for r := top; r <= bottom; r++ {
s[r*n+right] = i
i++
}
right--
if top == bottom {
break
}
// work left, along bottom
for c := right; c >= left; c-- {
s[bottom*n+c] = i
i++
}
bottom--
// work up left side
for r := bottom; r >= top; r-- {
s[r*n+left] = i
i++
}
left++
}
// center (last) element
s[top*n+left] = i
return s
}
func main() {
num := 5
len := 2
for i, draw := range spiral(num) {
fmt.Printf("%*d ", len, draw)
if i%num == num-1 {
fmt.Println("")
}
}
}
Most Helpful This Week
How to read current directory using Readdir?
How do you handle HTTP client server compression in Go?
Various examples of printing and formatting in Golang
Interface embedding and calling interface methods from another package in Go (Golang)
How to convert Struct fields into Map String?
Golang read file line by line to string
How to check specific field exist in struct?
How do you handle HTTP requests in Go?
How to build a map of struct and append values to it?
How do you set headers in an HTTP response in Go?