Submission #61387994


Source Code Expand

h, w = map(int, input().split())
s = []
for i in range(h):
    si = input()
    s.append(si)
    if 'S' in si:
        st = (i, si.index('S'))
    if 'G' in si:
        gl = (i, si.index('G'))

vst = [[False] * w for i in range(h)]
vst[st[0]][st[1]] = True
prv = [(st, 0), (st, 1)]
nxt = []
trn = 0
while prv != []:
    for co in prv:
        if co[1] == 0:
            for dj in [-1, 1]:
                nx = (co[0][0], co[0][1] + dj)
                if 0 <= nx[1] < w and not vst[nx[0]][nx[1]]:
                    if s[nx[0]][nx[1]] == 'G':
                        print(trn + 1)
                        quit()
                    if s[nx[0]][nx[1]] == '.':
                        nxt.append((nx, 1))
        else:
            for di in [-1, 1]:
                nx = (co[0][0] + di, co[0][1])
                if 0 <= nx[0] < h and not vst[nx[0]][nx[1]]:
                    if s[nx[0]][nx[1]] == 'G':
                        print(trn + 1)
                        quit()
                    if s[nx[0]][nx[1]] == '.':
                        nxt.append((nx, 0))
    for nx in nxt:
        vst[nx[0][0]][nx[0][1]] = True
    prv = nxt
    nxt = []
    trn += 1

print(-1)

Submission Info

Submission Time
Task D - Snaky Walk
User permut
Language Python (CPython 3.11.4)
Score 0
Code Size 1217 Byte
Status WA
Exec Time 2220 ms
Memory 215588 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 28
WA × 8
TLE × 21
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, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.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_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 05_handmade_00.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt, 05_handmade_05.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 9 ms 8992 KiB
00_sample_01.txt AC 9 ms 8780 KiB
00_sample_02.txt AC 158 ms 10464 KiB
01_random_00.txt TLE 2217 ms 191448 KiB
01_random_01.txt TLE 2214 ms 97056 KiB
01_random_02.txt AC 19 ms 15972 KiB
01_random_03.txt AC 11 ms 10804 KiB
01_random_04.txt AC 16 ms 13972 KiB
01_random_05.txt AC 14 ms 9104 KiB
01_random_06.txt AC 10 ms 8832 KiB
01_random_07.txt AC 9 ms 8716 KiB
01_random_08.txt AC 21 ms 17996 KiB
01_random_09.txt TLE 2218 ms 188876 KiB
01_random_10.txt TLE 2217 ms 177592 KiB
01_random_11.txt TLE 2216 ms 141952 KiB
01_random_12.txt TLE 2220 ms 215588 KiB
01_random_13.txt TLE 2219 ms 204900 KiB
01_random_14.txt TLE 2218 ms 181096 KiB
01_random_15.txt AC 21 ms 17920 KiB
01_random_16.txt TLE 2214 ms 115564 KiB
01_random_17.txt AC 76 ms 18652 KiB
01_random_18.txt TLE 2217 ms 171212 KiB
01_random_19.txt TLE 2216 ms 144748 KiB
02_random2_00.txt AC 21 ms 17924 KiB
02_random2_01.txt WA 20 ms 17964 KiB
02_random2_02.txt AC 20 ms 18016 KiB
02_random2_03.txt WA 20 ms 17928 KiB
02_random2_04.txt AC 20 ms 18012 KiB
02_random2_05.txt AC 20 ms 18048 KiB
02_random2_06.txt WA 20 ms 17996 KiB
02_random2_07.txt AC 22 ms 18024 KiB
02_random2_08.txt WA 21 ms 17992 KiB
02_random2_09.txt AC 20 ms 18044 KiB
02_random2_10.txt AC 21 ms 17968 KiB
02_random2_11.txt WA 20 ms 18044 KiB
02_random2_12.txt AC 21 ms 17960 KiB
02_random2_13.txt AC 21 ms 18084 KiB
02_random2_14.txt AC 20 ms 18012 KiB
02_random2_15.txt WA 20 ms 18040 KiB
02_random2_16.txt AC 21 ms 17964 KiB
02_random2_17.txt WA 20 ms 17964 KiB
02_random2_18.txt AC 21 ms 17976 KiB
02_random2_19.txt WA 69 ms 19544 KiB
03_random3_00.txt TLE 2214 ms 108044 KiB
03_random3_01.txt TLE 2214 ms 120736 KiB
03_random3_02.txt TLE 2214 ms 125140 KiB
03_random3_03.txt TLE 2214 ms 111216 KiB
04_random4_00.txt TLE 2214 ms 132440 KiB
04_random4_01.txt TLE 2213 ms 138300 KiB
04_random4_02.txt TLE 2214 ms 132516 KiB
04_random4_03.txt TLE 2216 ms 150536 KiB
05_handmade_00.txt TLE 2215 ms 164764 KiB
05_handmade_01.txt TLE 2216 ms 164752 KiB
05_handmade_02.txt AC 10 ms 8896 KiB
05_handmade_03.txt AC 9 ms 8840 KiB
05_handmade_04.txt AC 11 ms 8956 KiB
05_handmade_05.txt AC 648 ms 17892 KiB