funcmain() { n, m := 0, 0 fmt.Scanf("%d %d", &n, &m) var g [N][M]int // 右 下 左 上 dx := [...]int{0, 1, 0, -1} dy := [...]int{1, 0, -1, 0} d := 0 row, col := 0, 0 for idx := 1; idx <= n*m; idx++ { g[row][col] = idx x := row + dx[d] y := col + dy[d] if x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0{ row = x col = y }else{ d = (d + 1) % 4 row = row + dx[d] col = col + dy[d] } }
for i :=0 ; i < n ; i++{ for j := 0 ; j < m ; j++{ fmt.Printf("%d ",g[i][j]) } fmt.Println() } }