Submission #24975784


Source Code Expand

import java.util.*;

public class Main {

	static String[][] a;
	static int n;

	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		n = Integer.parseInt(sc.next());
		a = new String[n][n];

		// コーナーケース、n=1だったら答えは"X"
		if (n == 1) {
			System.out.println("X");
			return;
		}

		// 行ごとのスプレー開始位置
		int[] begin = new int[5];
		begin[0] = 0;
		begin[1] = 2;
		begin[2] = 4;
		begin[3] = 1;
		begin[4] = 3;

		// 生成
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				a[i][j] = (j % 5 == begin[i % 5]) ? "X" : ".";
			}
		}

		// 4辺は追加スプレーが必要
		for (int i = 0; i < n; i++) {
			a[0][i] = safe(0, i) ? "Y" : a[0][i];
			a[n - 1][i] = safe(n - 1, i) ? "Y" : a[n - 1][i];
			a[i][0] = safe(i, 0) ? "Y" : a[i][0];
			a[i][n - 1] = safe(i, n - 1) ? "Y" : a[i][n - 1];
		}

		// スプレー箇所確認用
//		for (int i = 0; i < n; i++) {
//			System.out.println(Arrays.toString(a[i]).replace(", ", "").replace("[", "").replace("]", ""));
//		}

		// スプレー回数確認用
//		int count = 0;
//		for (int i = 0; i < n; i++) {
//			for (int j = 0; j < n; j++) {
//				count += (a[i][j].equals("X") || a[i][j].equals("Y")) ? 1 : 0;
//			}
//		}
//		System.out.println(count);

		// こたえ
		for (int i = 0; i < n; i++) {
			System.out.println(
					Arrays.toString(a[i]).replace("Y", "X").replace(", ", "").replace("[", "").replace("]", ""));
		}

	}

	// マス(i, j)がまだ食べられる状態かどうか判定
	static boolean safe(int i, int j) {

		if (a[i][j].equals("X")) {
			return false;
		}

		if (i + 1 != n) {
			if (a[i + 1][j].equals("X")) {
				return false;
			}
		}

		if (i - 1 != -1) {
			if (a[i - 1][j].equals("X")) {
				return false;
			}
		}

		if (j + 1 != n) {
			if (a[i][j + 1].equals("X")) {
				return false;
			}
		}

		if (j - 1 != -1) {
			if (a[i][j - 1].equals("X")) {
				return false;
			}
		}

		return true;
	}

}

Submission Info

Submission Time
Task C - Special Cake for CODE FESTIVAL
User tobi00604
Language Java (OpenJDK 11.0.6)
Score 500
Code Size 2097 Byte
Status AC
Exec Time 387 ms
Memory 62672 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 15
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
in01.txt AC 387 ms 62516 KiB
in02.txt AC 384 ms 61792 KiB
in03.txt AC 381 ms 61584 KiB
in04.txt AC 383 ms 61380 KiB
in05.txt AC 383 ms 62672 KiB
in06.txt AC 198 ms 41760 KiB
in07.txt AC 152 ms 38760 KiB
in08.txt AC 135 ms 37396 KiB
in09.txt AC 103 ms 35476 KiB
in10.txt AC 105 ms 35688 KiB
in11.txt AC 102 ms 35468 KiB
in12.txt AC 99 ms 35512 KiB
sample_01.txt AC 96 ms 35624 KiB
sample_02.txt AC 100 ms 35564 KiB
sample_03.txt AC 100 ms 35652 KiB