結果

問題 No.401 数字の渦巻き
ユーザー Tsukasa_TypeTsukasa_Type
提出日時 2018-02-14 19:57:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,932 bytes
コンパイル時間 5,836 ms
コンパイル使用メモリ 73,996 KB
実行使用メモリ 57,704 KB
最終ジャッジ日時 2023-08-24 18:17:11
合計ジャッジ時間 8,180 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,436 KB
testcase_01 AC 120 ms
57,704 KB
testcase_02 AC 122 ms
55,676 KB
testcase_03 AC 124 ms
56,224 KB
testcase_04 AC 125 ms
55,992 KB
testcase_05 AC 127 ms
55,712 KB
testcase_06 AC 128 ms
55,736 KB
testcase_07 AC 130 ms
55,928 KB
testcase_08 AC 133 ms
55,676 KB
testcase_09 AC 133 ms
54,216 KB
testcase_10 AC 141 ms
55,928 KB
testcase_11 AC 149 ms
55,772 KB
testcase_12 AC 139 ms
56,132 KB
testcase_13 AC 153 ms
55,872 KB
testcase_14 AC 151 ms
56,188 KB
testcase_15 AC 145 ms
55,992 KB
testcase_16 AC 156 ms
55,860 KB
testcase_17 AC 160 ms
53,792 KB
testcase_18 AC 166 ms
55,892 KB
testcase_19 AC 165 ms
55,700 KB
testcase_20 AC 173 ms
56,064 KB
testcase_21 AC 168 ms
55,920 KB
testcase_22 AC 168 ms
55,868 KB
testcase_23 AC 171 ms
56,200 KB
testcase_24 AC 160 ms
55,896 KB
testcase_25 AC 177 ms
55,772 KB
testcase_26 AC 176 ms
56,588 KB
testcase_27 AC 167 ms
55,908 KB
testcase_28 AC 176 ms
56,020 KB
testcase_29 AC 171 ms
56,012 KB
権限があれば一括ダウンロードができます

ソースコード

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