Submission #1512985


Source Code Expand

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

	static int GOAL_Y;
	static int GOAL_X;


	public static void main(String[] args) {

		Scanner s = new Scanner(System.in);

		final int R = s.nextInt();
		final int C = s.nextInt();
		final int START_Y = s.nextInt();
		final int START_X = s.nextInt();
		GOAL_Y = s.nextInt();
		GOAL_X = s.nextInt();

		s.nextLine();	// 改行の読み込み

		char[][] status = new char[R][C];
		Zahyo[][] banmen = new Zahyo[R][C];
		for(int i=0; i<R; i++) {

			String line = s.next();
			for(int j=0; j<C; j++) {
				status[i][j] = line.charAt(j);
				banmen[i][j] = new Zahyo(i,j);
			}

		}

		int[][] dist = new int[R][C];
		for(int i=0; i<R; i++) {
			Arrays.fill(dist[i], -1);
		}

		dist[START_Y-1][START_X-1] = 0;

		Queue<Zahyo> queue = new LinkedList<>();
		queue.offer(new Zahyo(START_Y-1, START_X-1));

		while(!queue.isEmpty()) {
			checkDist(dist, queue, status);
		}

	}

	static void checkDist(int[][] dist, Queue<Zahyo> queue, char[][] status) {

		Zahyo nowZahyo = queue.poll();
		int y = nowZahyo.y;
		int x = nowZahyo.x;

		if(y == GOAL_Y-1 && x == GOAL_X-1) {
			System.out.println(dist[y][x]);
			return;
		}

		if(status[y-1][x] == '.'
				&& dist[y-1][x] == -1) {
			dist[y-1][x] = dist[y][x] + 1;
			queue.offer(new Zahyo(y-1, x));
		}

		if(status[y+1][x] == '.'
				&& dist[y+1][x] == -1) {
			dist[y+1][x] = dist[y][x] + 1;
			queue.offer(new Zahyo(y+1, x));
		}

		if(status[y][x-1] == '.'
				&& dist[y][x-1] == -1) {
			dist[y][x-1] = dist[y][x] + 1;
			queue.offer(new Zahyo(y, x-1));
		}

		if(status[y][x+1] == '.'
				&& dist[y][x+1] == -1) {
			dist[y][x+1] = dist[y][x] + 1;
			queue.offer(new Zahyo(y, x+1));
		}

	}

}

class Zahyo {
	int y;
	int x;

	public Zahyo(int y, int x) {
		super();
		this.y = y;
		this.x = x;
	}

}

Submission Info

Submission Time
Task C - 幅優先探索
User nasmarl
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 1979 Byte
Status AC
Exec Time 111 ms
Memory 23892 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 25
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
All 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
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 92 ms 18900 KB
subtask0_sample02.txt AC 90 ms 20692 KB
subtask0_sample03.txt AC 98 ms 19924 KB
subtask1_01.txt AC 98 ms 18900 KB
subtask1_02.txt AC 110 ms 21588 KB
subtask1_03.txt AC 109 ms 20564 KB
subtask1_04.txt AC 99 ms 23892 KB
subtask1_05.txt AC 102 ms 21844 KB
subtask1_06.txt AC 99 ms 19924 KB
subtask1_07.txt AC 93 ms 19028 KB
subtask1_08.txt AC 97 ms 19924 KB
subtask1_09.txt AC 109 ms 21716 KB
subtask1_10.txt AC 98 ms 17108 KB
subtask1_11.txt AC 100 ms 18772 KB
subtask1_12.txt AC 102 ms 21460 KB
subtask1_13.txt AC 100 ms 21716 KB
subtask1_14.txt AC 100 ms 19668 KB
subtask1_15.txt AC 106 ms 21076 KB
subtask1_16.txt AC 101 ms 19028 KB
subtask1_17.txt AC 99 ms 19924 KB
subtask1_18.txt AC 107 ms 18772 KB
subtask1_19.txt AC 108 ms 19796 KB
subtask1_20.txt AC 110 ms 21844 KB
subtask1_21.txt AC 104 ms 19924 KB
subtask1_22.txt AC 111 ms 20948 KB