結果

問題 No.401 数字の渦巻き
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-12-13 16:30:19
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,127 bytes
コンパイル時間 11,130 ms
コンパイル使用メモリ 208,048 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-09 11:56:21
合計ジャッジ時間 12,572 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	n := readInt()
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		m[i] = make([]int, n)
	}

	x := 0
	y := 0
	dx := 0
	dy := 1
	diffs := [][]int{
		[]int{0, 1},
		[]int{0, -1},
		[]int{1, 0},
		[]int{-1, 0},
	}
	for i := 1; i <= n*n; i++ {
		m[x][y] = i

		if !(0 <= x+dx && x+dx < n && 0 <= y+dy && y+dy < n && m[x+dx][y+dy] == 0) {
			for _, diff := range diffs {
				ex := diff[0]
				ey := diff[1]
				if 0 <= x+ex && x+ex < n && 0 <= y+ey && y+ey < n && m[x+ex][y+ey] == 0 {
					dx = ex
					dy = ey
					break
				}
			}
		}
		x += dx
		y += dy
	}

	for _, row := range m {
		line := []string{}
		for _, v := range row {
			line = append(line, fmt.Sprintf("%03d", v))
		}
		fmt.Println(strings.Join(line, " "))
	}
}

var stdin *bufio.Scanner

func read() string {
	if stdin == nil {
		stdin = bufio.NewScanner(os.Stdin)
		stdin.Split(bufio.ScanWords)
		stdin.Buffer(make([]byte, bufio.MaxScanTokenSize), int(math.MaxInt32))
	}
	stdin.Scan()
	return stdin.Text()
}

func readInt() int {
	n, _ := strconv.Atoi(read())
	return n
}
0