結果

問題 No.401 数字の渦巻き
ユーザー uafr_csuafr_cs
提出日時 2016-07-22 22:50:06
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,312 bytes
コンパイル時間 2,319 ms
コンパイル使用メモリ 79,088 KB
実行使用メモリ 60,072 KB
最終ジャッジ日時 2024-04-24 02:26:30
合計ジャッジ時間 8,020 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
41,140 KB
testcase_01 AC 118 ms
41,240 KB
testcase_02 AC 113 ms
41,216 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
					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