結果

問題 No.401 数字の渦巻き
ユーザー Grenache
提出日時 2016-07-22 22:30:58
言語 Java8
(openjdk 1.8.0.222)
結果
AC  
実行時間 172 ms
コード長 2,040 Byte
コンパイル時間 3,126 ms
使用メモリ 23,016 KB
最終ジャッジ日時 2019-11-26 09:49:29

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
01.txt AC 116 ms
21,396 KB
02.txt AC 120 ms
21,412 KB
03.txt AC 122 ms
21,404 KB
04.txt AC 122 ms
21,444 KB
05.txt AC 119 ms
21,448 KB
06.txt AC 120 ms
21,440 KB
07.txt AC 124 ms
21,476 KB
08.txt AC 125 ms
21,516 KB
09.txt AC 129 ms
21,532 KB
10.txt AC 129 ms
21,628 KB
11.txt AC 134 ms
21,644 KB
12.txt AC 130 ms
21,668 KB
13.txt AC 132 ms
21,688 KB
14.txt AC 133 ms
21,716 KB
15.txt AC 134 ms
21,740 KB
16.txt AC 137 ms
21,784 KB
17.txt AC 136 ms
21,844 KB
18.txt AC 137 ms
21,844 KB
19.txt AC 151 ms
22,208 KB
20.txt AC 161 ms
22,468 KB
21.txt AC 148 ms
22,368 KB
22.txt AC 154 ms
22,568 KB
23.txt AC 163 ms
22,624 KB
24.txt AC 155 ms
22,668 KB
25.txt AC 157 ms
22,712 KB
26.txt AC 157 ms
22,756 KB
27.txt AC 157 ms
22,816 KB
28.txt AC 157 ms
22,860 KB
29.txt AC 160 ms
22,920 KB
30.txt AC 172 ms
23,016 KB
テストケース一括ダウンロード

ソースコード

diff #
import java.io.*;
import java.util.*;


public class Main_yukicoder401 {

    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	Printer pr = new Printer(System.out);

    	int[] dx = {1, 0, -1, 0};
    	int[] dy = {0, 1, 0, -1};

    	int n = sc.nextInt();

    	int[][] ret = new int[n][n];
    	int d = 0;
    	int x = 0;
    	int y = 0;
    	for (int i = 0; i < n * n; i++) {
    		ret[y][x] = i + 1;

    		int nx = x + dx[d];
    		int ny = y + dy[d];
    		if (ny >= n || nx >= n || nx < 0 || ny < 0 || ret[ny][nx] > 0) {
    			d = (d + 1) % 4;
        		nx = x + dx[d];
        		ny = y + dy[d];
    		}
    		x = nx;
    		y = ny;
    	}

    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			if (j > 0) {
    				pr.print(" ");
    			}
    			pr.printf("%03d", ret[i][j]);
    		}
    		pr.println();
    	}

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
//					it = Arrays.asList(br.readLine().split(" ")).iterator();
					it = Arrays.asList(br.readLine().split("\\p{javaWhitespace}+")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0