結果

問題 No.401 数字の渦巻き
ユーザー しめはじめ
提出日時 2019-07-05 09:01:25
言語 PHP
(843.2)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 2,249 bytes
コンパイル時間 1,523 ms
コンパイル使用メモリ 32,404 KB
実行使用メモリ 32,660 KB
最終ジャッジ日時 2024-09-19 20:08:40
合計ジャッジ時間 3,968 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
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