Submission #2558280


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
public class Main {
	private static final PrintStream ps = System.out;
	private static final InputStream IS = System.in;
	private static final byte[] BUFFER = new byte[1024];
	private static int ptr = 0;
	private static int buflen = 0;

	public static void main(String[] args) {
		int H = ni();
		int W = ni();
		char[][] g = new char[H][W];
		int[][] d = new int[H][W];
		int sh = -1, sw = -1, gh = -1, gw = -1;
		
		for (int i = 0; i < H; i++) {
			Arrays.fill(d[i], -1);
		}
		
		for (int i = 0; i < H; i++) {
			g[i] = n().toCharArray();
			for (int j = 0; j < W; j++) {
				if (g[i][j] == 's') {
					sh = i;
					sw = j;
				} else if (g[i][j] == 'g') {
					gh = i;
					gw = j;
				}
			}
		}
		
		int[] mh = {1, -1, 0, 0,};
		int[] mw = {0, 0, 1, -1,};

		Queue<Integer> qh = new LinkedList<Integer>();
		Queue<Integer> qw = new LinkedList<Integer>();
		
		qh.add(sh);
		qw.add(sw);
		
		while (!qh.isEmpty()) {
			int h = qh.poll();
			int w = qw.poll();
			
			for (int i = 0; i < mh.length; i++) {
				int nh = h + mh[i];
				int nw = w + mw[i];
				if (   nh >= 0 && nh < H
				    && nw >= 0 && nw < W
				    && g[nh][nw] != '#'
				    && d[nh][nw] == -1) {
					d[nh][nw] = d[h][w] + 1;
					qh.add(nh);
					qw.add(nw);
				}
			}
		}
		
		System.out.println(d[gh][gw] >= 0 ? "Yes" : "No");
	}

	private static boolean hasNextByte() {
		if (ptr < buflen)
			return true;
		else {
			ptr = 0;
			try {
				buflen = IS.read(BUFFER);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0)
				return false;
		}
		return true;
	}

	private static int readByte() {
		if (hasNextByte())
			return BUFFER[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	public static boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(BUFFER[ptr]))
			ptr++;
		return hasNextByte();
	}

	public static String n() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public static long nl() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b)
			throw new NumberFormatException();
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b))
				return minus ? -n : n;
			else
				throw new NumberFormatException();
			b = readByte();
		}
	}

	public static int ni() {
		long nl = nl();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}

	public static double nextDouble() {
		return Double.parseDouble(n());
	}

	private static int[] nia(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}
}

Submission Info

Submission Time
Task A - 深さ優先探索
User central_field
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 3385 Byte
Status AC
Exec Time 193 ms
Memory 45072 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 5
AC × 83
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt, 00_sample_05.txt
All 00_min_01.txt, 00_min_02.txt, 00_min_03.txt, 00_min_04.txt, 00_min_05.txt, 00_min_06.txt, 00_min_07.txt, 00_min_08.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt, 00_sample_05.txt, 01_rnd_00.txt, 01_rnd_01.txt, 01_rnd_02.txt, 01_rnd_03.txt, 01_rnd_04.txt, 01_rnd_05.txt, 01_rnd_06.txt, 01_rnd_07.txt, 01_rnd_08.txt, 01_rnd_09.txt, 01_rnd_10.txt, 01_rnd_11.txt, 01_rnd_12.txt, 01_rnd_13.txt, 01_rnd_14.txt, 01_rnd_15.txt, 01_rnd_16.txt, 01_rnd_17.txt, 01_rnd_18.txt, 01_rnd_19.txt, 02_rndhard_00.txt, 02_rndhard_01.txt, 02_rndhard_02.txt, 02_rndhard_03.txt, 02_rndhard_04.txt, 02_rndhard_05.txt, 02_rndhard_06.txt, 02_rndhard_07.txt, 02_rndhard_08.txt, 02_rndhard_09.txt, 02_rndhard_10.txt, 02_rndhard_11.txt, 02_rndhard_12.txt, 02_rndhard_13.txt, 02_rndhard_14.txt, 02_rndhard_15.txt, 02_rndhard_16.txt, 02_rndhard_17.txt, 02_rndhard_18.txt, 02_rndhard_19.txt, 02_rndhard_20.txt, 02_rndhard_21.txt, 02_rndhard_22.txt, 02_rndhard_23.txt, 02_rndhard_24.txt, 02_rndhard_25.txt, 02_rndhard_26.txt, 02_rndhard_27.txt, 02_rndhard_28.txt, 02_rndhard_29.txt, 02_rndhard_30.txt, 02_rndhard_31.txt, 02_rndhard_32.txt, 02_rndhard_33.txt, 02_rndhard_34.txt, 02_rndhard_35.txt, 02_rndhard_36.txt, 02_rndhard_37.txt, 02_rndhard_38.txt, 02_rndhard_39.txt, 03_rndhardsmall_00.txt, 03_rndhardsmall_01.txt, 03_rndhardsmall_02.txt, 03_rndhardsmall_03.txt, 03_rndhardsmall_04.txt, 03_rndhardsmall_05.txt, 03_rndhardsmall_06.txt, 03_rndhardsmall_07.txt, 03_rndhardsmall_08.txt, 03_rndhardsmall_09.txt
Case Name Status Exec Time Memory
00_min_01.txt AC 71 ms 20692 KB
00_min_02.txt AC 67 ms 20820 KB
00_min_03.txt AC 67 ms 21204 KB
00_min_04.txt AC 66 ms 17620 KB
00_min_05.txt AC 68 ms 21204 KB
00_min_06.txt AC 67 ms 21204 KB
00_min_07.txt AC 67 ms 16340 KB
00_min_08.txt AC 66 ms 21332 KB
00_sample_01.txt AC 67 ms 17364 KB
00_sample_02.txt AC 68 ms 21204 KB
00_sample_03.txt AC 67 ms 19284 KB
00_sample_04.txt AC 66 ms 17876 KB
00_sample_05.txt AC 68 ms 19156 KB
01_rnd_00.txt AC 112 ms 25428 KB
01_rnd_01.txt AC 190 ms 41220 KB
01_rnd_02.txt AC 174 ms 41180 KB
01_rnd_03.txt AC 192 ms 42228 KB
01_rnd_04.txt AC 187 ms 43288 KB
01_rnd_05.txt AC 109 ms 28244 KB
01_rnd_06.txt AC 179 ms 44832 KB
01_rnd_07.txt AC 179 ms 42896 KB
01_rnd_08.txt AC 112 ms 24404 KB
01_rnd_09.txt AC 110 ms 24148 KB
01_rnd_10.txt AC 156 ms 32856 KB
01_rnd_11.txt AC 105 ms 23508 KB
01_rnd_12.txt AC 190 ms 43812 KB
01_rnd_13.txt AC 184 ms 42948 KB
01_rnd_14.txt AC 112 ms 25684 KB
01_rnd_15.txt AC 155 ms 35688 KB
01_rnd_16.txt AC 113 ms 23764 KB
01_rnd_17.txt AC 168 ms 36816 KB
01_rnd_18.txt AC 115 ms 26580 KB
01_rnd_19.txt AC 193 ms 45072 KB
02_rndhard_00.txt AC 113 ms 24020 KB
02_rndhard_01.txt AC 102 ms 26324 KB
02_rndhard_02.txt AC 120 ms 29140 KB
02_rndhard_03.txt AC 122 ms 26324 KB
02_rndhard_04.txt AC 109 ms 23380 KB
02_rndhard_05.txt AC 101 ms 24020 KB
02_rndhard_06.txt AC 110 ms 25300 KB
02_rndhard_07.txt AC 112 ms 24404 KB
02_rndhard_08.txt AC 105 ms 26068 KB
02_rndhard_09.txt AC 108 ms 23380 KB
02_rndhard_10.txt AC 104 ms 26324 KB
02_rndhard_11.txt AC 110 ms 26580 KB
02_rndhard_12.txt AC 108 ms 22612 KB
02_rndhard_13.txt AC 103 ms 25556 KB
02_rndhard_14.txt AC 103 ms 22868 KB
02_rndhard_15.txt AC 105 ms 26196 KB
02_rndhard_16.txt AC 115 ms 24404 KB
02_rndhard_17.txt AC 101 ms 26068 KB
02_rndhard_18.txt AC 118 ms 25556 KB
02_rndhard_19.txt AC 117 ms 26196 KB
02_rndhard_20.txt AC 115 ms 22612 KB
02_rndhard_21.txt AC 124 ms 25024 KB
02_rndhard_22.txt AC 119 ms 23892 KB
02_rndhard_23.txt AC 115 ms 23636 KB
02_rndhard_24.txt AC 106 ms 24404 KB
02_rndhard_25.txt AC 103 ms 26436 KB
02_rndhard_26.txt AC 114 ms 24532 KB
02_rndhard_27.txt AC 112 ms 24404 KB
02_rndhard_28.txt AC 114 ms 24404 KB
02_rndhard_29.txt AC 112 ms 27604 KB
02_rndhard_30.txt AC 109 ms 23892 KB
02_rndhard_31.txt AC 109 ms 26324 KB
02_rndhard_32.txt AC 115 ms 22868 KB
02_rndhard_33.txt AC 115 ms 23508 KB
02_rndhard_34.txt AC 115 ms 24148 KB
02_rndhard_35.txt AC 109 ms 24276 KB
02_rndhard_36.txt AC 110 ms 24360 KB
02_rndhard_37.txt AC 105 ms 26452 KB
02_rndhard_38.txt AC 102 ms 24148 KB
02_rndhard_39.txt AC 112 ms 22868 KB
03_rndhardsmall_00.txt AC 67 ms 17620 KB
03_rndhardsmall_01.txt AC 69 ms 18516 KB
03_rndhardsmall_02.txt AC 67 ms 18900 KB
03_rndhardsmall_03.txt AC 70 ms 18772 KB
03_rndhardsmall_04.txt AC 67 ms 19412 KB
03_rndhardsmall_05.txt AC 69 ms 18772 KB
03_rndhardsmall_06.txt AC 68 ms 16980 KB
03_rndhardsmall_07.txt AC 67 ms 19028 KB
03_rndhardsmall_08.txt AC 67 ms 17492 KB
03_rndhardsmall_09.txt AC 67 ms 20564 KB