結果

問題 No.401 数字の渦巻き
ユーザー atkrym
提出日時 2016-10-22 10:33:33
言語 Java
(openjdk 23)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 2,344 ms
コンパイル使用メモリ 78,024 KB
実行使用メモリ 44,704 KB
最終ジャッジ日時 2024-11-23 17:04:42
合計ジャッジ時間 8,063 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    private static int[][] ary;
    public static void main(String[] args) throws Exception {
        int n = sc.nextInt();
        move(n);
        show(n);
    }
    
    public static void move(int n) {
        ary = new int[n][n];
        int x = 0;
        int y = 0;
        int[] dx = {0, 1, 0, -1};
        int[] dy = {1, 0, -1, 0};
        int d = 0;
        for (int i = 1;i <= n*n;i++) {
            ary[x][y] = i;
            int nx = x + dx[d];
            int ny = y + dy[d];
            if (nx < 0 || n <= nx || ny < 0 || n <= ny) {
                d = (d+1)%4;
                nx = x + dx[d];
                ny = y + dy[d];
            } else if (ary[nx][ny] > 0) {
                d = (d+1)%4;
                nx = x + dx[d];
                ny = y + dy[d];
            }
            x = nx;
            y = ny;
        }
    }
    
    public static void show(int n) {
        for (int i = 0;i < n;i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0;j < n;j++) {
                sb.append(String.format("%03d", ary[i][j])).append(" ");
            }
            System.out.println(sb.substring(0, sb.length()-1));
        }
    }
}
0