結果
| 問題 |
No.401 数字の渦巻き
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-07-08 14:22:28 |
| 言語 | Haskell (9.10.1) |
| 結果 |
AC
|
| 実行時間 | 8 ms / 2,000 ms |
| コード長 | 995 bytes |
| コンパイル時間 | 10,373 ms |
| コンパイル使用メモリ | 203,136 KB |
| 実行使用メモリ | 14,336 KB |
| 最終ジャッジ日時 | 2024-07-07 04:15:38 |
| 合計ジャッジ時間 | 11,453 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default [1 of 2] Compiling Main ( Main.hs, Main.o ) [2 of 2] Linking a.out
ソースコード
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))