Submission #8954320


Source Code Expand

Copy
import sys
sys.setrecursionlimit(1000000)


class GoalFound(Exception):
    pass

class Solver:
    def __init__(self, H, W, maze) -> None:
        self.H = H
        self.W = W
        self.maze = maze

    def find_point(self, obj):
        for h in range(self.H):
            for w in range(self.W):
                if self.maze[h][w] == obj:
                    return (h, w)
        raise ValueError('Not found:{}'.format(obj))

    def depth_first_search(self, h, w):
        if not (0 <= h < self.H):
            return False
        if not (0 <= w < self.W):
            return False
        if self.maze[h][w] == "g":
            raise GoalFound()
        if self.maze[h][w] == "#":
            return False
        if self.maze[h][w] in [".", "s"]:
            self.maze[h][w] = "#"
            return any([
                self.depth_first_search(h - 1, w),
                self.depth_first_search(h, w - 1),
                self.depth_first_search(h, w + 1),
                self.depth_first_search(h + 1, w)
            ])
        return False

    def solve(self) -> str:
        start = self.find_point('s')
        try:
            self.depth_first_search(*start)
            return 'No'
        except GoalFound:
            return 'Yes'


if __name__ == "__main__":
    H, W = list(map(int, input().split()))
    maze = []
    for _ in range(H):
        maze.append(list(input()))

    s = Solver(H, W, maze)
    print(s.solve())

Submission Info

Submission Time
Task A - 深さ優先探索
User hagino3000
Language Python (3.4.3)
Score 100
Code Size 1504 Byte
Status
Exec Time 466 ms
Memory 158816 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
× 5
× 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 17 ms 3064 KB
00_min_02.txt 17 ms 3064 KB
00_min_03.txt 17 ms 3064 KB
00_min_04.txt 17 ms 3064 KB
00_min_05.txt 17 ms 3064 KB
00_min_06.txt 17 ms 3064 KB
00_min_07.txt 17 ms 3064 KB
00_min_08.txt 17 ms 3064 KB
00_sample_01.txt 17 ms 3064 KB
00_sample_02.txt 17 ms 3064 KB
00_sample_03.txt 17 ms 3064 KB
00_sample_04.txt 17 ms 3064 KB
00_sample_05.txt 17 ms 3064 KB
01_rnd_00.txt 45 ms 5364 KB
01_rnd_01.txt 466 ms 158816 KB
01_rnd_02.txt 156 ms 33632 KB
01_rnd_03.txt 336 ms 119392 KB
01_rnd_04.txt 73 ms 22516 KB
01_rnd_05.txt 28 ms 5364 KB
01_rnd_06.txt 312 ms 59444 KB
01_rnd_07.txt 358 ms 85412 KB
01_rnd_08.txt 30 ms 5364 KB
01_rnd_09.txt 40 ms 5364 KB
01_rnd_10.txt 275 ms 14068 KB
01_rnd_11.txt 44 ms 5364 KB
01_rnd_12.txt 417 ms 129380 KB
01_rnd_13.txt 132 ms 43124 KB
01_rnd_14.txt 38 ms 5364 KB
01_rnd_15.txt 81 ms 12020 KB
01_rnd_16.txt 47 ms 5364 KB
01_rnd_17.txt 362 ms 23656 KB
01_rnd_18.txt 41 ms 7284 KB
01_rnd_19.txt 97 ms 28276 KB
02_rndhard_00.txt 49 ms 5492 KB
02_rndhard_01.txt 48 ms 5492 KB
02_rndhard_02.txt 88 ms 6772 KB
02_rndhard_03.txt 84 ms 6772 KB
02_rndhard_04.txt 43 ms 5364 KB
02_rndhard_05.txt 43 ms 5364 KB
02_rndhard_06.txt 42 ms 5492 KB
02_rndhard_07.txt 40 ms 5364 KB
02_rndhard_08.txt 51 ms 5748 KB
02_rndhard_09.txt 52 ms 5748 KB
02_rndhard_10.txt 41 ms 5748 KB
02_rndhard_11.txt 41 ms 5748 KB
02_rndhard_12.txt 43 ms 5620 KB
02_rndhard_13.txt 43 ms 5620 KB
02_rndhard_14.txt 53 ms 5876 KB
02_rndhard_15.txt 54 ms 5876 KB
02_rndhard_16.txt 29 ms 5364 KB
02_rndhard_17.txt 29 ms 5364 KB
02_rndhard_18.txt 38 ms 5492 KB
02_rndhard_19.txt 38 ms 5492 KB
02_rndhard_20.txt 31 ms 5620 KB
02_rndhard_21.txt 31 ms 5492 KB
02_rndhard_22.txt 48 ms 6004 KB
02_rndhard_23.txt 42 ms 5876 KB
02_rndhard_24.txt 48 ms 5492 KB
02_rndhard_25.txt 48 ms 5492 KB
02_rndhard_26.txt 34 ms 5492 KB
02_rndhard_27.txt 31 ms 5364 KB
02_rndhard_28.txt 43 ms 5492 KB
02_rndhard_29.txt 43 ms 5492 KB
02_rndhard_30.txt 36 ms 5364 KB
02_rndhard_31.txt 35 ms 5364 KB
02_rndhard_32.txt 45 ms 5748 KB
02_rndhard_33.txt 45 ms 5748 KB
02_rndhard_34.txt 44 ms 5492 KB
02_rndhard_35.txt 44 ms 5492 KB
02_rndhard_36.txt 43 ms 5492 KB
02_rndhard_37.txt 43 ms 5364 KB
02_rndhard_38.txt 38 ms 5492 KB
02_rndhard_39.txt 37 ms 5492 KB
03_rndhardsmall_00.txt 17 ms 3064 KB
03_rndhardsmall_01.txt 17 ms 3064 KB
03_rndhardsmall_02.txt 17 ms 3064 KB
03_rndhardsmall_03.txt 17 ms 3064 KB
03_rndhardsmall_04.txt 17 ms 3064 KB
03_rndhardsmall_05.txt 18 ms 3064 KB
03_rndhardsmall_06.txt 17 ms 3064 KB
03_rndhardsmall_07.txt 17 ms 3064 KB
03_rndhardsmall_08.txt 17 ms 3064 KB
03_rndhardsmall_09.txt 17 ms 3064 KB