結果

問題 No.401 数字の渦巻き
ユーザー uafr_csuafr_cs
提出日時 2016-07-22 22:52:10
言語 Java19
(openjdk 21)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,333 bytes
コンパイル時間 3,874 ms
コンパイル使用メモリ 74,104 KB
実行使用メモリ 63,352 KB
最終ジャッジ日時 2023-08-06 06:11:06
合計ジャッジ時間 8,535 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,680 KB
testcase_01 AC 124 ms
55,532 KB
testcase_02 AC 125 ms
55,944 KB
testcase_03 AC 128 ms
55,900 KB
testcase_04 AC 131 ms
55,888 KB
testcase_05 AC 131 ms
55,620 KB
testcase_06 AC 138 ms
56,052 KB
testcase_07 AC 143 ms
55,656 KB
testcase_08 AC 144 ms
56,340 KB
testcase_09 AC 147 ms
55,620 KB
testcase_10 AC 151 ms
55,564 KB
testcase_11 AC 159 ms
56,008 KB
testcase_12 AC 161 ms
55,844 KB
testcase_13 AC 166 ms
55,908 KB
testcase_14 AC 168 ms
55,708 KB
testcase_15 AC 182 ms
55,688 KB
testcase_16 AC 187 ms
55,944 KB
testcase_17 AC 184 ms
55,696 KB
testcase_18 AC 195 ms
56,588 KB
testcase_19 AC 206 ms
56,376 KB
testcase_20 AC 206 ms
56,036 KB
testcase_21 AC 209 ms
56,120 KB
testcase_22 AC 214 ms
56,368 KB
testcase_23 AC 218 ms
56,136 KB
testcase_24 AC 208 ms
56,048 KB
testcase_25 AC 222 ms
56,240 KB
testcase_26 AC 227 ms
58,544 KB
testcase_27 AC 228 ms
56,276 KB
testcase_28 AC 231 ms
55,864 KB
testcase_29 AC 270 ms
63,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static final int[][] move_dirs = {
		{ 1, 0},
		{ 0, 1},
		{-1, 0},
		{ 0,-1}
	};
	
	public static boolean in_range(int x, int y, int W, int H){
		return 0 <= x && x < W && 0 <= y && y < H;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		
		int[][] map = new int[N][N];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				map[i][j] = Integer.MAX_VALUE;
			}
		}
		int cx = 0, cy = 0, dir = 0;
		int cnt = 1;
		
		while(map[cy][cx] >= cnt){
			map[cy][cx] = cnt;
			cnt++;
			
			//System.out.println(cy + " " + cx);
			
			for(int next = 0; next < move_dirs.length; next++){
				final int next_dir = (dir + next) % move_dirs.length;
				
				final int nx = cx + move_dirs[next_dir][0];
				final int ny = cy + move_dirs[next_dir][1];
				
				if(!in_range(nx, ny, N, N)){
					continue;
				}else if(map[ny][nx] < cnt){
					continue;
				}else{
					cx = nx;
					cy = ny;
					dir = next_dir;
					break;
				}
			}
		}
		
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				System.out.printf("%s%03d", j == 0 ? "" : " ", map[i][j]);
			}
			System.out.println();
		}
	}

}
0