AtCoder Beginner Contest 007

Submission #1511850

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<Integer> qy = new ArrayDeque<Integer>();
		Deque<Integer> qx = new ArrayDeque<Integer>();
		qy.offer(ny);
		qx.offer(nx);
		while (!qy.isEmpty() && !qx.isEmpty()) {
			int tempX = qx.poll();
			int tempY = qy.poll();

			for (int i = 0; i < 4; i++) {
				if (m[tempY + y[i]][tempX + x[i]] == -1) {
					m[tempY + y[i]][tempX + x[i]] = m[tempY][tempX] + 1;
					qy.offer(tempY + y[i]);
					qx.offer(tempX + x[i]);
				}
			}

		}

	}
}

Submission

Task問題 C - 幅優先探索
User nameユーザ名 edna
Created time投稿日時
Language言語 Java8 (OpenJDK 1.8.0)
Status状態 AC
Score得点 100
Source lengthソースコード長 1394 Byte
File nameファイル名
Exec time実行時間 117 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 92 ms 20052 KB
subtask0_sample02.txt AC 91 ms 23764 KB
subtask0_sample03.txt AC 99 ms 21844 KB
subtask1_01.txt AC 98 ms 18644 KB
subtask1_02.txt AC 104 ms 19540 KB
subtask1_03.txt AC 99 ms 19796 KB
subtask1_04.txt AC 100 ms 20436 KB
subtask1_05.txt AC 99 ms 19540 KB
subtask1_06.txt AC 98 ms 19924 KB
subtask1_07.txt AC 92 ms 21588 KB
subtask1_08.txt AC 98 ms 16980 KB
subtask1_09.txt AC 99 ms 18644 KB
subtask1_10.txt AC 98 ms 21844 KB
subtask1_11.txt AC 117 ms 19540 KB
subtask1_12.txt AC 103 ms 16976 KB
subtask1_13.txt AC 99 ms 19924 KB
subtask1_14.txt AC 98 ms 19284 KB
subtask1_15.txt AC 100 ms 20564 KB
subtask1_16.txt AC 99 ms 21204 KB
subtask1_17.txt AC 101 ms 21972 KB
subtask1_18.txt AC 99 ms 20820 KB
subtask1_19.txt AC 100 ms 21712 KB
subtask1_20.txt AC 100 ms 19796 KB
subtask1_21.txt AC 98 ms 19924 KB
subtask1_22.txt AC 99 ms 21204 KB