Submission #6954218


Source Code Expand

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	try {
    		final int H = sc.nextInt();
    		final int W = sc.nextInt();

    		final int startY = sc.nextInt();
    		final int startX = sc.nextInt();
    		final int goalY = sc.nextInt();
    		final int goalX = sc.nextInt();

    		Maze maze = new Maze(H, W);
    		for (int i=0; i<H; i++) {
    			maze.init(i, sc.next());
    		}

    		YX start = new YX(startY - 1, startX - 1);
    		Queue<YX> queue = new ArrayDeque<>();
    		maze.set(start.y, start.x, 0);
    		queue.add(start);

    		while(!queue.isEmpty()) {
    			YX here = queue.remove();
    			if (maze.get(here.y-1, here.x) == -1) {
    				maze.set(here.y-1, here.x, maze.get(here.y,  here.x) + 1);
    				queue.add(new YX(here.y-1, here.x));
    			}
    			if (maze.get(here.y, here.x+1) == -1) {
    				maze.set(here.y, here.x+1, maze.get(here.y,  here.x) + 1);
    				queue.add(new YX(here.y, here.x+1));
    			}
    			if (maze.get(here.y+1, here.x) == -1) {
    				maze.set(here.y+1, here.x, maze.get(here.y,  here.x) + 1);
    				queue.add(new YX(here.y+1, here.x));
    			}
    			if (maze.get(here.y, here.x-1) == -1) {
    				maze.set(here.y, here.x-1, maze.get(here.y,  here.x) + 1);
    				queue.add(new YX(here.y, here.x-1));
    			}
    			// System.out.println(maze.toString());
    		}

    		System.out.println(maze.get(goalY - 1,  goalX - 1));

    	} finally {
    		sc.close();
    	}
    }
}


class Maze {
	final int H;
	final int W;
	int[][] maze;
	public Maze(int h, int w) {
		this.H = h;
		this.W = w;
		maze = new int[H][W];
	}

	public void init(int y, String mazelet) {
		for (int i=0; i<mazelet.length(); i++) {
			char c = mazelet.charAt(i);
			if (c == '#') {
				maze[y][i] = Integer.MAX_VALUE;
			} else {
				maze[y][i] = -1;
			}
		}
	}

	public void set(int y, int x, int i) {
		maze[y][x] = i;
	}

	public int get(int y, int x) {
		return maze[y][x];
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (int y=0; y<maze.length; y++) {
			for (int x=0; x<maze[y].length; x++) {
				if (maze[y][x] == -1) {
					sb.append('.');
				} else if (maze[y][x] == Integer.MAX_VALUE) {
					sb.append('#');
				} else {
					// sb.append('o');
					sb.append(maze[y][x] % 10);
				}
			}
			sb.append("\n");
		}
		return sb.toString();
	}
}

class YX {
	public final int y;
	public final int x;
	public YX(int y, int x) {
		this.y = y;
		this.x = x;
	}

	public String toString() {
		return "[" + y + "," + x + "]";
	}
}

Submission Info

Submission Time
Task A - 幅優先探索
User satob
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 2767 Byte
Status AC
Exec Time 166 ms
Memory 23764 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 115 ms 19284 KB
subtask0_sample02.txt AC 93 ms 18644 KB
subtask0_sample03.txt AC 102 ms 19024 KB
subtask1_01.txt AC 100 ms 18644 KB
subtask1_02.txt AC 99 ms 19156 KB
subtask1_03.txt AC 99 ms 21716 KB
subtask1_04.txt AC 101 ms 19540 KB
subtask1_05.txt AC 105 ms 19668 KB
subtask1_06.txt AC 100 ms 19924 KB
subtask1_07.txt AC 166 ms 19284 KB
subtask1_08.txt AC 99 ms 21844 KB
subtask1_09.txt AC 100 ms 18772 KB
subtask1_10.txt AC 101 ms 20180 KB
subtask1_11.txt AC 101 ms 21588 KB
subtask1_12.txt AC 102 ms 18640 KB
subtask1_13.txt AC 99 ms 23764 KB
subtask1_14.txt AC 101 ms 19668 KB
subtask1_15.txt AC 102 ms 17748 KB
subtask1_16.txt AC 101 ms 19412 KB
subtask1_17.txt AC 102 ms 20564 KB
subtask1_18.txt AC 101 ms 20948 KB
subtask1_19.txt AC 103 ms 19156 KB
subtask1_20.txt AC 99 ms 19284 KB
subtask1_21.txt AC 100 ms 19924 KB
subtask1_22.txt AC 100 ms 21332 KB