結果

問題 No.401 数字の渦巻き
ユーザー しめはじめしめはじめ
提出日時 2019-07-05 09:01:25
言語 PHP
(8.3.4)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,249 bytes
コンパイル時間 3,787 ms
コンパイル使用メモリ 32,192 KB
実行使用メモリ 32,560 KB
最終ジャッジ日時 2023-10-20 00:15:28
合計ジャッジ時間 6,574 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
32,560 KB
testcase_01 AC 44 ms
32,556 KB
testcase_02 AC 44 ms
32,556 KB
testcase_03 AC 44 ms
32,556 KB
testcase_04 AC 44 ms
32,556 KB
testcase_05 AC 44 ms
32,556 KB
testcase_06 AC 44 ms
32,556 KB
testcase_07 AC 45 ms
32,556 KB
testcase_08 AC 45 ms
32,556 KB
testcase_09 AC 45 ms
32,556 KB
testcase_10 AC 44 ms
32,556 KB
testcase_11 AC 45 ms
32,556 KB
testcase_12 AC 44 ms
32,556 KB
testcase_13 AC 44 ms
32,556 KB
testcase_14 AC 46 ms
32,556 KB
testcase_15 AC 45 ms
32,556 KB
testcase_16 AC 45 ms
32,556 KB
testcase_17 AC 45 ms
32,556 KB
testcase_18 AC 46 ms
32,556 KB
testcase_19 AC 46 ms
32,556 KB
testcase_20 AC 46 ms
32,556 KB
testcase_21 AC 46 ms
32,556 KB
testcase_22 AC 46 ms
32,556 KB
testcase_23 AC 47 ms
32,556 KB
testcase_24 AC 46 ms
32,556 KB
testcase_25 AC 47 ms
32,556 KB
testcase_26 AC 47 ms
32,556 KB
testcase_27 AC 48 ms
32,556 KB
testcase_28 AC 48 ms
32,556 KB
testcase_29 AC 47 ms
32,556 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
// 真ん中のセット
function SetMannaka( $n, &$box ){
    if( $n % 2 == 1 ){
        $box[($n - 1)/2][($n - 1)/2] = $n * $n;
    }else{
        $v = $n * $n;
        $c = $n / 2;
        $box[$c - 1][$c] = $v--;
        $box[$c][$c] = $v--;
        $box[$c][$c - 1] = $v--;
        $box[$c - 1][$c - 1] = $v--;
    }
}

// 左上隅から右へ
function SetYokoMigi( $v, $n, $shu, &$box ){
    //開始位置を取得
    $xpos = $shu - 1;
    $ypos = $shu - 1;
    for( $i = 0; $i < $n - ( $shu - 1 ) * 2; $i++ ){
        $box[$xpos + $i][$ypos] = $v++;
    }
}
// 右下隅から左へ
function SetYokoHidari( $v, $n, $shu, &$box ){
    //開始位置を取得
    $xpos = $n - $shu;
    $ypos = $n - $shu;
    for( $i = 0; $i < $n - ( $shu - 1 ) * 2; $i++ ){
        $box[$xpos - $i][$ypos] = $v++;
    }
}
// 右上隅から下へ
function SetTateShita( $v, $n, $shu, &$box ){
    //開始位置を取得
    $xpos = $n - $shu;
    $ypos = $shu - 1;
    for( $i = 0; $i < $n - ( $shu - 1 ) * 2; $i++ ){
        $box[$xpos][$ypos + $i] = $v++;
    }
}
// 左下隅から上へ
function SetTateUe( $v, $n, $shu, &$box ){
    $xpos = $shu - 1;
    $ypos = $n - $shu;
    // 開始位置をつぶさないために1減らす
    for( $i = 0; $i < $n - ( $shu - 1 ) * 2 - 1; $i++ ){
        $box[$xpos][$ypos - $i] = $v++;
    }
}

// 入力
$n = trim( fgets( STDIN ) );

// 初期化
for( $i = 0; $i < $n; $i++ ){
    for( $j = 0; $j < $n; $j++ ){
        $box[$i][$j] = 0;
    }
}

// 先に中央を
SetMannaka( $n, $box );
//その後周りを埋めていく
for( $shu = 1, $v = 1; $shu < ( $n / 2 ); $shu++ ){
//for( $shu = 0; $shu <= $n * $n; $v++ ){
    SetYokoMigi( $v, $n, $shu, $box );
    $v += $n - ( ( $shu - 1 ) * 2 ) - 1;
    SetTateShita( $v, $n, $shu, $box );
    $v += $n - ( ( $shu - 1 ) * 2 ) - 1;
    SetYokoHidari( $v, $n, $shu, $box );
    $v += $n - ( ( $shu - 1 ) * 2 ) - 1 ;
    SetTateUe( $v, $n, $shu, $box );
    $v += $n - ( ( $shu - 1 ) * 2 ) - 1 ;
}

// 出力
for( $ypos = 0; $ypos < $n; $ypos++ ){
    for( $xpos = 0; $xpos < $n; $xpos++ ){
        printf( "%03d", $box[$xpos][$ypos] );
        if( $xpos >= $n - 1 ){
            printf( "\n" );
        }else{
            printf( " " );
        }
    }
}
0