Submission #43134529


Source Code Expand

import java.awt.*;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
	public static void main(String[] args) throws Exception {
		try (Scanner scanner = new Scanner(System.in)) {
			int h = scanner.nextInt();
			int w = scanner.nextInt();
			char[][] map = new char[h][w];
			for (int i = 0; i < h; i++) {
				String line = scanner.next();
				for (int j = 0; j < w; j++) {
					map[i][j] = line.charAt(j);
				}
			}

			char[] list = {'s', 'n', 'u', 'k', 'e'};
			Set<Point> path = new HashSet<>();
			if (check(list, 0, 0, 0, h, w, map, path)) {
				System.out.println("Yes");
			} else {
				System.out.println("No");
			}
		}
	}

	private static boolean check(char[] list, int listSeq, int x, int y, int h, int w, char[][] map, Set<Point> path) {
		if (x < 0 || x >= w) {
			return false;
		}
		if (y < 0 || y >= h) {
			return false;
		}

		Point point = new Point(x, y);
		if (path.contains(point)) {
			return false;
		}

		char target = list[listSeq];
		if (map[y][x] != target) {
			return false;
		}
		if (x == w - 1 && y == h - 1) {
			return true;
		}
		listSeq++;
		if (listSeq >= list.length) {
			listSeq = 0;
		}

		path.add(point);

		if (check(list, listSeq, x, y - 1, h, w, map, path)) {
			return true;
		}
		if (check(list, listSeq, x + 1, y, h, w, map, path)) {
			return true;
		}
		if (check(list, listSeq, x, y + 1, h, w, map, path)) {
			return true;
		}
		return check(list, listSeq, x - 1, y, h, w, map, path);
	}
}

Submission Info

Submission Time
Task D - Snuke Maze
User kmatsu
Language Java (OpenJDK 11.0.6)
Score 400
Code Size 1555 Byte
Status AC
Exec Time 1097 ms
Memory 120612 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 36
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 114 ms 35552 KiB
00_sample_01.txt AC 101 ms 35596 KiB
00_sample_02.txt AC 110 ms 35544 KiB
01_random_00.txt AC 141 ms 36052 KiB
01_random_01.txt AC 155 ms 38520 KiB
01_random_02.txt AC 172 ms 38680 KiB
01_random_03.txt AC 128 ms 37208 KiB
01_random_04.txt AC 154 ms 38416 KiB
01_random_05.txt AC 109 ms 35484 KiB
01_random_06.txt AC 143 ms 37052 KiB
01_random_07.txt AC 151 ms 37612 KiB
02_random2_00.txt AC 230 ms 43496 KiB
02_random2_01.txt AC 725 ms 88780 KiB
02_random2_02.txt AC 227 ms 47096 KiB
02_random2_03.txt AC 226 ms 47140 KiB
02_random2_04.txt AC 850 ms 104388 KiB
02_random2_05.txt AC 183 ms 39068 KiB
02_random2_06.txt AC 244 ms 49032 KiB
02_random2_07.txt AC 726 ms 88356 KiB
02_random2_08.txt AC 242 ms 48948 KiB
02_random2_09.txt AC 188 ms 39068 KiB
02_random2_10.txt AC 214 ms 44852 KiB
02_random2_11.txt AC 185 ms 39392 KiB
02_random2_12.txt AC 232 ms 44764 KiB
02_random2_13.txt AC 828 ms 88652 KiB
02_random2_14.txt AC 179 ms 39236 KiB
02_random2_15.txt AC 736 ms 86068 KiB
02_random2_16.txt AC 194 ms 39208 KiB
02_random2_17.txt AC 187 ms 38976 KiB
02_random2_18.txt AC 698 ms 84208 KiB
02_random2_19.txt AC 234 ms 43668 KiB
03_handmade_00.txt AC 124 ms 35528 KiB
03_handmade_01.txt AC 181 ms 39448 KiB
03_handmade_02.txt AC 810 ms 78644 KiB
03_handmade_03.txt AC 201 ms 40164 KiB
03_handmade_04.txt AC 1097 ms 120612 KiB