結果

問題 No.401 数字の渦巻き
ユーザー papinianuspapinianus
提出日時 2016-08-31 13:40:46
言語 PHP
(8.3.4)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,636 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 30,772 KB
実行使用メモリ 31,116 KB
最終ジャッジ日時 2024-04-26 21:31:58
合計ジャッジ時間 2,545 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
31,096 KB
testcase_01 AC 38 ms
30,632 KB
testcase_02 AC 39 ms
30,924 KB
testcase_03 AC 38 ms
31,008 KB
testcase_04 AC 41 ms
30,792 KB
testcase_05 AC 40 ms
31,112 KB
testcase_06 AC 38 ms
31,016 KB
testcase_07 AC 38 ms
30,700 KB
testcase_08 AC 39 ms
31,016 KB
testcase_09 AC 39 ms
30,928 KB
testcase_10 AC 39 ms
30,892 KB
testcase_11 AC 40 ms
30,880 KB
testcase_12 AC 38 ms
30,968 KB
testcase_13 AC 39 ms
30,628 KB
testcase_14 AC 41 ms
31,004 KB
testcase_15 AC 39 ms
31,004 KB
testcase_16 AC 39 ms
30,768 KB
testcase_17 AC 40 ms
30,944 KB
testcase_18 AC 39 ms
30,892 KB
testcase_19 AC 39 ms
30,888 KB
testcase_20 AC 40 ms
30,700 KB
testcase_21 AC 40 ms
30,936 KB
testcase_22 AC 40 ms
31,104 KB
testcase_23 AC 40 ms
31,116 KB
testcase_24 AC 39 ms
30,968 KB
testcase_25 AC 38 ms
30,848 KB
testcase_26 AC 40 ms
30,920 KB
testcase_27 AC 41 ms
30,932 KB
testcase_28 AC 41 ms
30,812 KB
testcase_29 AC 40 ms
30,888 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