結果

問題 No.401 数字の渦巻き
ユーザー uafr_csuafr_cs
提出日時 2016-07-22 22:52:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,333 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 79,440 KB
実行使用メモリ 49,364 KB
最終ジャッジ日時 2024-04-24 02:27:30
合計ジャッジ時間 7,606 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
41,364 KB
testcase_01 AC 117 ms
41,612 KB
testcase_02 AC 102 ms
40,448 KB
testcase_03 AC 113 ms
41,128 KB
testcase_04 AC 117 ms
41,276 KB
testcase_05 AC 128 ms
41,560 KB
testcase_06 AC 123 ms
41,452 KB
testcase_07 AC 126 ms
41,676 KB
testcase_08 AC 140 ms
41,984 KB
testcase_09 AC 144 ms
41,656 KB
testcase_10 AC 149 ms
41,832 KB
testcase_11 AC 155 ms
42,488 KB
testcase_12 AC 148 ms
42,384 KB
testcase_13 AC 143 ms
41,600 KB
testcase_14 AC 152 ms
41,848 KB
testcase_15 AC 156 ms
41,908 KB
testcase_16 AC 154 ms
41,836 KB
testcase_17 AC 174 ms
41,840 KB
testcase_18 AC 181 ms
41,956 KB
testcase_19 AC 177 ms
41,884 KB
testcase_20 AC 185 ms
42,772 KB
testcase_21 AC 189 ms
42,144 KB
testcase_22 AC 187 ms
42,132 KB
testcase_23 AC 198 ms
42,244 KB
testcase_24 AC 213 ms
42,692 KB
testcase_25 AC 212 ms
43,408 KB
testcase_26 AC 199 ms
43,108 KB
testcase_27 AC 195 ms
42,256 KB
testcase_28 AC 232 ms
42,560 KB
testcase_29 AC 253 ms
49,364 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