結果

問題 No.401 数字の渦巻き
ユーザー papinianuspapinianus
提出日時 2016-08-31 13:40:46
言語 PHP
(8.3.4)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,636 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 18,460 KB
実行使用メモリ 18,988 KB
最終ジャッジ日時 2023-08-09 06:43:05
合計ジャッジ時間 2,964 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
18,752 KB
testcase_01 AC 16 ms
18,860 KB
testcase_02 AC 16 ms
18,828 KB
testcase_03 AC 16 ms
18,896 KB
testcase_04 AC 16 ms
18,924 KB
testcase_05 AC 16 ms
18,856 KB
testcase_06 AC 16 ms
18,820 KB
testcase_07 AC 16 ms
18,916 KB
testcase_08 AC 16 ms
18,688 KB
testcase_09 AC 16 ms
18,840 KB
testcase_10 AC 16 ms
18,884 KB
testcase_11 AC 16 ms
18,936 KB
testcase_12 AC 15 ms
18,936 KB
testcase_13 AC 16 ms
18,900 KB
testcase_14 AC 16 ms
18,896 KB
testcase_15 AC 16 ms
18,776 KB
testcase_16 AC 16 ms
18,948 KB
testcase_17 AC 16 ms
18,932 KB
testcase_18 AC 16 ms
18,896 KB
testcase_19 AC 16 ms
18,988 KB
testcase_20 AC 16 ms
18,916 KB
testcase_21 AC 16 ms
18,892 KB
testcase_22 AC 16 ms
18,828 KB
testcase_23 AC 16 ms
18,916 KB
testcase_24 AC 16 ms
18,944 KB
testcase_25 AC 16 ms
18,824 KB
testcase_26 AC 16 ms
18,916 KB
testcase_27 AC 17 ms
18,688 KB
testcase_28 AC 17 ms
18,852 KB
testcase_29 AC 16 ms
18,920 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$width = trim(fgets(STDIN));
$max = $width*$width;
$pos = new position(0,0, $width);
for($i = 1; $i <= $max; $i++) {
    $x = $pos->getX();
    $y = $pos->getY();
    position::$map[$y][$x] = substr("00".$i,-3);
    $pos->move();
}
ksort(position::$map);
foreach(position::$map as $yrow) {
    ksort($yrow);
    $ys[] = implode(" ",$yrow);
}
echo implode("\n", $ys);

class position {
    public static $map;
    private $x;
    private $y;
    private $direction = [[1,0],[0,1],[-1,0],[0,-1]];
    private $dirSize = 4;
    private $currentDir = 0;
    private $limit;

    public function getX() {
        return $this->x;
    }
    public function getY() {
        return $this->y;
    }
    public function __construct($x, $y, $limit) {
        $this->x = $x;
        $this->y = $y;
        $this->limit = $limit;
    }
    public function move() {
        if(!$this->movable()) {
            $this->rotate();
        }
        $this->x = $this->x + $this->direction[$this->currentDir][0];
        $this->y = $this->y + $this->direction[$this->currentDir][1];
    }
    public function movable() {
        $nextX = $this->x + $this->direction[$this->currentDir][0];
        if($nextX >= $this->limit || $nextX < 0) {
            return false;
        } 
        $nextY = $this->y + $this->direction[$this->currentDir][1];
        if($nextY >= $this->limit || $nextY < 0) {
            return false;
        }
        if(isset(self::$map[$nextY][$nextX])) {
            return false;
        }
        return true;
    }
    public function rotate() {
        $this->currentDir = ($this->currentDir+1) % $this->dirSize;
    }
}
0