-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14940.py
More file actions
70 lines (55 loc) · 1.36 KB
/
14940.py
File metadata and controls
70 lines (55 loc) · 1.36 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#author madaniel47
n, m = map(int, input().split())
arr = [0]*n
for i in range(n):
arr[i] = list(map(int, input().split()))
visited = [[0 for i in range(m)] for j in range(n)]
pointed = [[0 for i in range(m)] for j in range(n)]
target_point_x = 0
target_point_y = 0
for i in range(n):
for j in range(m):
if arr[i][j] == 2:
target_point_x = j
target_point_y = i
break
point= 0
stack_x = []
stack_y = []
stack_x.append(target_point_x)
stack_y.append(target_point_y)
stack_x.append('s')
stack_y.append('s')
while len(stack_x) != 0:
x = stack_x.pop(0)
y = stack_y.pop(0)
if x == 's':
point = point + 1
if len(stack_x) == 0:
break
stack_x.append('s')
stack_y.append('s')
continue
if x < 0 or x >= m or y < 0 or y >= n:
continue
if visited[y][x] == 1 or arr[y][x] == 0:
continue
# 방문기록
visited[y][x] = 1
pointed[y][x] = point
stack_x.append(x+1)
stack_y.append(y)
stack_x.append(x-1)
stack_y.append(y)
stack_x.append(x)
stack_y.append(y+1)
stack_x.append(x)
stack_y.append(y-1)
for i in range(n):
for j in range(m):
if visited[i][j] == 0 and arr[i][j] == 1:
pointed[i][j] = -1
for i in range(n):
for j in range(m):
print(pointed[i][j], end=" ")
print()