-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ16234.py
More file actions
97 lines (81 loc) · 2.52 KB
/
BJ16234.py
File metadata and controls
97 lines (81 loc) · 2.52 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from sys import stdin
from collections import deque
NLR = stdin.readline()
NLR = NLR.split(' ')
N = int(NLR[0])
L = int(NLR[1])
R = int(NLR[2])
arr = []
visited = []
tempArr = []
tempVisit = []
for i in range(N):
user = input().split(' ')
for j in range(N):
tempArr.append(int(user[j]))
tempVisit.append(0)
arr.append(tempArr)
visited.append(tempVisit)
tempArr = []
tempVisit = []
q = deque([])
count = 0
answer = 0
turn = True
breaker = False
unite = []
uniteIndex = []
i = 0
j = 0
while turn:
print(arr)
while i < N:
if breaker:
breaker = False
break
while j < N:
if visited[i][j] == 0:
q.append([i, j, arr[i][j]])
visited[i][j] = 1
while q:
x = q[0][0]
y = q[0][1]
val = q[0][2]
pop = q.popleft()
uniteIndex.append(pop)
unite.append(val)
if x < N - 1:
if L <= abs(val - arr[x + 1][y]) <= R and visited[x + 1][y] == 0:
q.append([x + 1, y, arr[x + 1][y]])
visited[x + 1][y] = 1
if x > 0:
if L <= abs(val - arr[x - 1][y]) <= R and visited[x - 1][y] == 0:
q.append([x - 1, y, arr[x - 1][y]])
visited[x - 1][y] = 1
if y < N - 1:
if L <= abs(val - arr[x][y + 1]) <= R and visited[x][y + 1] == 0:
q.append([x, y + 1, arr[x][y + 1]])
visited[x][y + 1] = 1
if y > 0:
if L <= abs(val - arr[x][y - 1]) <= R and visited[x][y - 1] == 0:
q.append([x, y - 1, arr[x][y - 1]])
visited[x][y - 1] = 1
if len(unite) > 1:
answer += 1
population = sum(unite) / len(unite)
for k in uniteIndex:
x = k[0]
y = k[1]
arr[x][y] = int(population)
visited[x][y] = 0
unite = []
uniteIndex = []
breaker = True
break
else:
breaker = True
turn = False
break
j += 1
i += 1
print(answer)