結果

問題 No.401 数字の渦巻き
ユーザー uafr_cs
提出日時 2016-07-22 22:52:10
言語 Java
(openjdk 23)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 1,333 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 79,004 KB
実行使用メモリ 48,592 KB
最終ジャッジ日時 2024-11-06 09:10:54
合計ジャッジ時間 8,664 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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