結果

問題 No.401 数字の渦巻き
ユーザー ieneko18
提出日時 2016-11-17 15:24:02
言語 Java
(openjdk 23)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 709 bytes
コンパイル時間 2,224 ms
コンパイル使用メモリ 77,780 KB
実行使用メモリ 46,760 KB
最終ジャッジ日時 2024-11-26 02:49:43
合計ジャッジ時間 9,029 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int board[][]=new int[n][n];
		board[0][0]=1;
		int move[][]={{1,0},{0,1},{-1,0},{0,-1}};
		int nx=0;
		int ny=0;
		int direction=0;
		for(int i=2;i<=n*n;i++){
			int nnx=nx+move[direction%4][0];
			int nny=ny+move[direction%4][1];
			if(nnx<0||nnx>=n||nny<0||nny>=n){
				direction++;
			}else if(board[nnx][nny]!=0){
				direction++;
			}
			nx+=move[direction%4][0];
			ny+=move[direction%4][1];
			board[nx][ny]=i;
		}
		
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				System.out.printf("%03d ",board[j][i]);
			}
			System.out.println();
		}
	}
}
0