結果

問題 No.401 数字の渦巻き
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-02 04:51:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 4,718 ms
コンパイル使用メモリ 74,052 KB
実行使用メモリ 62,748 KB
最終ジャッジ日時 2023-08-18 12:09:28
合計ジャッジ時間 9,146 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
55,476 KB
testcase_01 AC 117 ms
56,084 KB
testcase_02 AC 114 ms
53,912 KB
testcase_03 AC 119 ms
56,044 KB
testcase_04 AC 119 ms
55,492 KB
testcase_05 AC 122 ms
55,780 KB
testcase_06 AC 130 ms
56,056 KB
testcase_07 AC 135 ms
55,720 KB
testcase_08 AC 132 ms
55,708 KB
testcase_09 AC 137 ms
55,872 KB
testcase_10 AC 141 ms
55,708 KB
testcase_11 AC 150 ms
55,900 KB
testcase_12 AC 148 ms
56,048 KB
testcase_13 AC 166 ms
56,124 KB
testcase_14 AC 180 ms
56,000 KB
testcase_15 AC 164 ms
55,860 KB
testcase_16 AC 169 ms
55,960 KB
testcase_17 AC 159 ms
56,160 KB
testcase_18 AC 176 ms
56,464 KB
testcase_19 AC 182 ms
55,860 KB
testcase_20 AC 193 ms
56,196 KB
testcase_21 AC 190 ms
56,180 KB
testcase_22 AC 187 ms
56,084 KB
testcase_23 AC 185 ms
56,480 KB
testcase_24 AC 200 ms
56,152 KB
testcase_25 AC 202 ms
56,432 KB
testcase_26 AC 195 ms
56,256 KB
testcase_27 AC 199 ms
55,900 KB
testcase_28 AC 199 ms
56,600 KB
testcase_29 AC 238 ms
62,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int [][]map= new int[N][N];
        int num=1;
        int dir=0;
        int x=0;
        int y=0;
        while(num<=N*N){
            int t=dir%4;
            if(t==0){
                map[y][x]=num;
                num++;
                if(x==N-1||map[y][x+1]!=0){
                    dir++;
                    y++;
                    continue;
                }
                x++;
            }else if(t==1){
                map[y][x]=num;
                num++;
                if(y==N-1||map[y+1][x]!=0){
                    dir++;
                    x--;
                    continue;
                }
                y++;
            }else if(t==2){
                map[y][x]=num;
                num++;
                if(x==0||map[y][x-1]!=0){
                    dir++;
                    y--;
                    continue;
                }
                x--;
            }else{
                map[y][x]=num;
                num++;
                if(y==0||map[y-1][x]!=0){
                    dir++;
                    x++;
                    continue;
                }
                y--;
            }
        }
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++){
                System.out.print(String.format("%03d",map[i][j]));
                if(j!=N-1){System.out.print(" ");}
            }System.out.println();
        }
    }
}
0