結果

問題 No.401 数字の渦巻き
ユーザー Tsukasa_Type
提出日時 2018-02-14 19:57:19
言語 Java
(openjdk 23)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,932 bytes
コンパイル時間 4,840 ms
コンパイル使用メモリ 77,892 KB
実行使用メモリ 42,228 KB
最終ジャッジ日時 2024-12-23 02:48:42
合計ジャッジ時間 9,311 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		String[][] ar = new String[n+2][n+2];
		for (int i=0; i<n+2; i++) {
			for (int j=0; j<n+2; j++) {
				if (i==0 || i==n+1 || j==0 || j==n+1) {
					ar[i][j] = "xxx";
				}
				else {
					ar[i][j] = "000";
				}
			}
		}
		ar[1][1] = "001";
		String direction = "right";
		int value = 1;
		int x = 1;
		int y = 1;
		
		for (int i=0; i<n*n-1; i++) {
			if (direction.equals("right")) {
				if (y>=n+1) {direction = "down"; i--;}
				else {
					if (ar[x][y+1].equals("000")) {
						y++;
						value++;
						ar[x][y] = zero(value);
					}
					else {direction = "down"; i--;}
				}
			}
			else if (direction.equals("down")) {
				if (x>=n+1) {direction = "left"; i--;}
				else {
					if (ar[x+1][y].equals("000")) {
						x++;
						value++;
						ar[x][y] = zero(value);
					}
					else {direction = "left"; i--;}
				}
			}
			else if (direction=="left") {
				if (y<=1) {direction = "up"; i--;}
				else {
					if (ar[x][y-1].equals("000")) {
						y--;
						value++;
						ar[x][y] = zero(value);
					}
					else {direction = "up"; i--;}
				}
			}
			else if (direction=="up") {
				if (x<=1) {direction = "right"; i--;}
				else {
					if (ar[x-1][y].equals("000")) {
						x--;
						value++;
						ar[x][y] = zero(value);
					}
					else {direction = "right"; i--;}
				}
			}
		}
		
		
		
		
		print(n+2,ar);
		
		
		
	}
	
	static String zero (int a) {
		StringBuilder sb = new StringBuilder();
		if (a<=9) {
			sb.append("00");
		}
		if (10<=a && a<=99) {
			sb.append("0");
		}
		sb.append(String.valueOf(a));
		return sb.toString();
	}
	
	static void print (int n, String strs[][]) {
		for (int i=1; i<n-1; i++) {
			for (int j=1; j<n-1; j++) {
				System.out.print(strs[i][j]);
				if (j!=n-1) {System.out.print(" ");}
			}
			System.out.println();
		}
	}
}

0