Skip to content

Instantly share code, notes, and snippets.

@snowmerak
Last active January 31, 2021 06:34
Show Gist options
  • Save snowmerak/8df7ada088a79033b94a5280cd746fe4 to your computer and use it in GitHub Desktop.
Save snowmerak/8df7ada088a79033b94a5280cd746fe4 to your computer and use it in GitHub Desktop.
snail
package main
import "fmt"
func main() {
n := 0
fmt.Scanf("%d\n", &n)
arr := make([][]int, n)
cur := 1
topPad, leftPad, rightPad, botPad := 0, -1, n, n
for i := 0; i < n; i++ {
arr[i] = make([]int, n)
}
pos := uint8(0)
x, y := 0, 0
for {
if n*n < cur {
break
}
switch pos {
case 0:
for i := x; i < rightPad; i++ {
arr[i][y] = cur
cur++
x = i
}
y++
rightPad--
pos = 1
case 1:
for i := y; i < botPad; i++ {
arr[x][i] = cur
cur++
y = i
}
x--
botPad--
pos = 2
case 2:
for i := x; i > leftPad; i-- {
arr[i][y] = cur
cur++
x = i
}
y--
leftPad++
pos = 3
case 3:
for i := y; i > topPad; i-- {
arr[x][i] = cur
cur++
y = i
}
x++
topPad++
pos = 0
}
}
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
fmt.Printf("%3d ", arr[j][i])
}
fmt.Println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment