AcWing 756. 蛇形矩阵——Go
原题链接
困难
作者:
404
,
2019-05-05 17:37:30
,
所有人可见
,
阅读 1034
package main
import "fmt"
func main() {
tmap := make([][]int, 105)
for i:=0; i<105; i++ {
tmap[i] = make([]int, 105)
}
n, m := 0, 0
fmt.Scanf("%d %d", &n, &m)
curI := 0
curJ := -1
all := n * m
i := 1
for i<=all {
for i <= all && curJ + 1 < m && tmap[curI ][curJ + 1] == 0 {
curJ++
tmap[curI][curJ] = i
i++
}
for i <= all && curI + 1 < n && tmap[curI + 1][curJ] == 0 {
curI++
tmap[curI][curJ] = i
i++
}
for i<=all && curJ - 1>= 0 && tmap[curI][curJ -1] == 0 {
curJ--
tmap[curI][curJ] = i
i++
}
for i<=all && curI - 1>= 0 && tmap[curI - 1][curJ] == 0 {
curI--
tmap[curI][curJ] = i
i++
}
}
for i:=0; i<n; i++ {
for j:=0; j<m; j++ {
fmt.Printf("%d ", tmap[i][j])
}
fmt.Println()
}
}