結果

問題 No.401 数字の渦巻き
ユーザー ducktailducktail
提出日時 2018-07-08 14:22:28
言語 Haskell
(9.8.2)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 995 bytes
コンパイル時間 12,648 ms
コンパイル使用メモリ 193,476 KB
実行使用メモリ 17,328 KB
最終ジャッジ日時 2023-09-21 10:09:56
合計ジャッジ時間 14,707 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,436 KB
testcase_01 AC 3 ms
7,504 KB
testcase_02 AC 3 ms
7,504 KB
testcase_03 AC 3 ms
7,536 KB
testcase_04 AC 3 ms
7,700 KB
testcase_05 AC 3 ms
7,676 KB
testcase_06 AC 4 ms
7,860 KB
testcase_07 AC 3 ms
7,844 KB
testcase_08 AC 3 ms
8,020 KB
testcase_09 AC 3 ms
8,096 KB
testcase_10 AC 4 ms
8,320 KB
testcase_11 AC 4 ms
8,652 KB
testcase_12 AC 4 ms
8,864 KB
testcase_13 AC 5 ms
9,012 KB
testcase_14 AC 5 ms
9,172 KB
testcase_15 AC 5 ms
9,968 KB
testcase_16 AC 5 ms
10,408 KB
testcase_17 AC 6 ms
10,628 KB
testcase_18 AC 6 ms
11,064 KB
testcase_19 AC 6 ms
11,432 KB
testcase_20 AC 8 ms
11,820 KB
testcase_21 AC 12 ms
12,288 KB
testcase_22 AC 12 ms
13,844 KB
testcase_23 AC 12 ms
14,804 KB
testcase_24 AC 12 ms
14,724 KB
testcase_25 AC 13 ms
15,980 KB
testcase_26 AC 13 ms
17,220 KB
testcase_27 AC 12 ms
15,780 KB
testcase_28 AC 23 ms
17,328 KB
testcase_29 AC 12 ms
15,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Applicative ((<$>))
import Control.Monad (forM_)
import Data.Vector.Unboxed (Vector, (!), (//))
import qualified Data.Vector.Unboxed as V
import Text.Printf

main :: IO ()
main = do
  n <- readLn
  printV n $ solve n

solve :: Int -> Vector Int
solve n = f 1 (V.replicate (n*n) 0) (0,0) drs
  where drs = cycle [(0,1),(1,0),(0,-1),(-1,0)]
        f x v (i, j) ((cdi, cdj):(ndi, ndj):rs) | x > n * n = v
                                                | i == n || j == n || i == (-1) || j == (-1) || v ! (n*i+j) > 0 = f x v (i-cdi+ndi, j-cdj+ndj) ((ndi, ndj):rs)
                                                | otherwise = f (x+1) (v // [(n*i+j, x)]) (i+cdi,j+cdj) ((cdi, cdj):(ndi, ndj):rs)

printV :: Int -> Vector Int -> IO ()
printV n v = do
  forM_ [0 .. n-1] $ \i -> do
    forM_ [0 .. n-1] $ \j -> do
      if j == 0 then printf "%03d" (v ! (n * i + j))
        else if j == n - 1 then printf " %03d\n" (v ! (n * i + j))
             else printf " %03d" (v ! (n * i + j))
0