-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16724.py
42 lines (33 loc) · 914 Bytes
/
16724.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 피리 부는 사나이
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
board = [input().rstrip() for _ in range(N)]
visit = [[0] * M for _ in range(N)]
safe = 0
for i in range(N):
for j in range(M):
if visit[i][j] == 1:
continue
cur = []
nexti, nextj = i, j
while True:
next = board[nexti][nextj]
visit[nexti][nextj] = -1
cur.append([nexti, nextj])
if next == "U":
nexti -= 1
elif next == "D":
nexti += 1
elif next == "L":
nextj -= 1
elif next == "R":
nextj += 1
if visit[nexti][nextj] == 1:
break
elif visit[nexti][nextj] == -1:
safe += 1
break
for a, b in cur:
visit[a][b] = 1
print(safe)