結果

問題 No.401 数字の渦巻き
ユーザー crazybbb3crazybbb3
提出日時 2016-11-27 17:41:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 2,459 bytes
コンパイル時間 5,274 ms
コンパイル使用メモリ 73,652 KB
実行使用メモリ 60,280 KB
最終ジャッジ日時 2023-08-18 08:28:02
合計ジャッジ時間 9,655 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,172 KB
testcase_01 AC 117 ms
53,876 KB
testcase_02 AC 121 ms
55,340 KB
testcase_03 AC 125 ms
55,272 KB
testcase_04 AC 122 ms
55,548 KB
testcase_05 AC 121 ms
55,812 KB
testcase_06 AC 127 ms
55,472 KB
testcase_07 AC 128 ms
55,352 KB
testcase_08 AC 130 ms
55,428 KB
testcase_09 AC 135 ms
55,212 KB
testcase_10 AC 138 ms
55,708 KB
testcase_11 AC 138 ms
55,740 KB
testcase_12 AC 141 ms
55,648 KB
testcase_13 AC 144 ms
55,688 KB
testcase_14 AC 145 ms
55,384 KB
testcase_15 AC 159 ms
55,648 KB
testcase_16 AC 147 ms
55,580 KB
testcase_17 AC 147 ms
55,740 KB
testcase_18 AC 163 ms
55,836 KB
testcase_19 AC 167 ms
55,552 KB
testcase_20 AC 171 ms
55,840 KB
testcase_21 AC 169 ms
55,564 KB
testcase_22 AC 160 ms
55,964 KB
testcase_23 AC 173 ms
55,856 KB
testcase_24 AC 163 ms
55,928 KB
testcase_25 AC 175 ms
55,884 KB
testcase_26 AC 168 ms
56,020 KB
testcase_27 AC 178 ms
55,684 KB
testcase_28 AC 169 ms
56,360 KB
testcase_29 AC 196 ms
60,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    void solve() throws IOException {
        n = ni();

        boolean[][] used = new boolean[n][n];
        int[][] ans = new int[n][n];

        int x = 0;
        int y = 0;
        int mode = 0;

        for (int i = 1; i <= n * n; i++) {
            ans[x][y] = i;
            used[x][y] = true;

            if (!isin(x + dx[mode], y + dy[mode]) || used[x + dx[mode]][y + dy[mode]]) {
                mode = (mode + 1) % 4;
            }

            x += dx[mode];
            y += dy[mode];
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j > 0) out.print(" ");
                out.print(String.format("%03d", ans[i][j]));
            }
            out.println();
        }
    }

    int n;

    int[] dy = {1, 0, -1, 0};
    int[] dx = {0, 1, 0, -1};

    boolean isin(int x, int y) {
        return 0 <= x && x <= n - 1 && 0 <= y && y <= n - 1;
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }

    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}
0