結果

問題 No.401 数字の渦巻き
ユーザー Grenache
提出日時 2016-07-22 22:30:58
言語 Java8
(openjdk 1.8.0.191)
結果
AC  
実行時間 152 ms
コード長 2,040 Byte
コンパイル時間 2,741 ms
使用メモリ 33,252 KB
最終ジャッジ日時 2019-07-10 22:02:56

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
01.txt AC 105 ms
32,688 KB
02.txt AC 104 ms
32,228 KB
03.txt AC 107 ms
32,684 KB
04.txt AC 107 ms
32,708 KB
05.txt AC 108 ms
32,280 KB
06.txt AC 107 ms
31,792 KB
07.txt AC 111 ms
31,788 KB
08.txt AC 112 ms
31,820 KB
09.txt AC 109 ms
32,684 KB
10.txt AC 111 ms
31,792 KB
11.txt AC 115 ms
32,724 KB
12.txt AC 116 ms
31,836 KB
13.txt AC 116 ms
32,724 KB
14.txt AC 116 ms
32,616 KB
15.txt AC 120 ms
32,560 KB
16.txt AC 125 ms
32,672 KB
17.txt AC 142 ms
32,280 KB
18.txt AC 121 ms
30,076 KB
19.txt AC 144 ms
32,272 KB
20.txt AC 134 ms
31,852 KB
21.txt AC 128 ms
32,724 KB
22.txt AC 132 ms
31,856 KB
23.txt AC 144 ms
33,248 KB
24.txt AC 144 ms
32,684 KB
25.txt AC 142 ms
32,156 KB
26.txt AC 144 ms
32,632 KB
27.txt AC 147 ms
32,420 KB
28.txt AC 152 ms
32,748 KB
29.txt AC 146 ms
32,156 KB
30.txt AC 151 ms
33,252 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