AtCoder Beginner Contest 007

Submission #1511852

Source codeソースコード

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {
	static int r;
	static int c;
	static int sy;
	static int sx;
	static int gy;
	static int gx;
	static int[][] m;
	static int[] x = { 0, 1, 0, -1 };
	static int[] y = { -1, 0, 1, 0 };

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		r = scan.nextInt();
		c = scan.nextInt();
		sy = scan.nextInt();
		sx = scan.nextInt();
		gy = scan.nextInt();
		gx = scan.nextInt();

		m = new int[r + 2][c + 2];

		for (int i = 1; i <= r; i++) {
			String temp = scan.next();
			for (int j = 1; j <= c; j++) {
				if (temp.charAt(j - 1) == '.') {
					m[i][j] = -1;
				} else {
					m[i][j] = -2;
				}

			}
		}
		m[sy][sx] = 0;
		bfs(sy, sx);
		int ans = Integer.MIN_VALUE;

		ans = m[gy][gx];
		System.out.println(ans);

	}

	static void bfs(int ny, int nx) {
		Deque<int[]> q = new ArrayDeque<>();
		int[] c = { ny, nx };
		q.offerLast(c);
		while (!q.isEmpty()) {
			int[] b = q.pollFirst();

			for (int i = 0; i < 4; i++) {
				if (m[b[0] + y[i]][b[1] + x[i]] == -1) {
					m[b[0] + y[i]][b[1] + x[i]] = m[b[0]][b[1]] + 1;
					int[] a = { b[0] + y[i], b[1] + x[i] };
					q.offerLast(a);
				}
			}

		}

	}
}

Submission

Task問題 C - 幅優先探索
User nameユーザ名 edna
Created time投稿日時
Language言語 Java8 (OpenJDK 1.8.0)
Status状態 AC
Score得点 100
Source lengthソースコード長 1303 Byte
File nameファイル名
Exec time実行時間 108 ms
Memory usageメモリ使用量 23764 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt
All 100 / 100 subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask1_13.txt,subtask1_14.txt,subtask1_15.txt,subtask1_16.txt,subtask1_17.txt,subtask1_18.txt,subtask1_19.txt,subtask1_20.txt,subtask1_21.txt,subtask1_22.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_sample01.txt AC 99 ms 21076 KB
subtask0_sample02.txt AC 90 ms 20692 KB
subtask0_sample03.txt AC 98 ms 19796 KB
subtask1_01.txt AC 107 ms 20564 KB
subtask1_02.txt AC 98 ms 21712 KB
subtask1_03.txt AC 96 ms 21972 KB
subtask1_04.txt AC 96 ms 19028 KB
subtask1_05.txt AC 98 ms 23764 KB
subtask1_06.txt AC 98 ms 19284 KB
subtask1_07.txt AC 92 ms 19412 KB
subtask1_08.txt AC 97 ms 19412 KB
subtask1_09.txt AC 102 ms 21588 KB
subtask1_10.txt AC 106 ms 21844 KB
subtask1_11.txt AC 97 ms 18644 KB
subtask1_12.txt AC 108 ms 18900 KB
subtask1_13.txt AC 103 ms 21588 KB
subtask1_14.txt AC 102 ms 20820 KB
subtask1_15.txt AC 108 ms 19796 KB
subtask1_16.txt AC 96 ms 19540 KB
subtask1_17.txt AC 98 ms 18644 KB
subtask1_18.txt AC 97 ms 18900 KB
subtask1_19.txt AC 98 ms 18388 KB
subtask1_20.txt AC 96 ms 19668 KB
subtask1_21.txt AC 97 ms 18772 KB
subtask1_22.txt AC 97 ms 22868 KB