-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyutil.py
50 lines (47 loc) · 1.25 KB
/
myutil.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
43
44
45
46
47
48
49
50
from item import ITEM_INPUT
import numpy as np
def dist(s, d):
return ((s[0]-d[0]) ** 2 + (s[1]-d[1]) ** 2) ** 0.5
def is_in_set(b: int, b_set: int):
return (b_set & (1<<b)) != 0
def bfs(i, j, cnt, w, h, vis, mmp, mp_ret):
q = []
s = 0
r = 0
q.append((i, j))
while(r <= s):
x = (int)(q[r][0])
y = (int)(q[r][1])
r += 1
if vis[x][y] == 1 or mmp[x][y] == 1:
continue
vis[x][y] = 1
mp_ret[x][y] = cnt
if(x-1 >= 0):
s+=1
q.append((x-1, y))
if (x + 1 < w):
s += 1
q.append((x+1, y))
if (y - 1 >= 0):
s += 1
q.append((x, y-1))
if (y + 1 < h):
s += 1
q.append((x, y+1))
def check_block(mp):
w, h = mp.shape
mmp = np.zeros(mp.shape)
vis = np.zeros((w, h), dtype=int)
for i in range(0, w):
for j in range(0, h):
vis[i][j] = 0
mmp[i][j] = 1 if mp[i][j] >= 1 else 0
cnt = 1
mp_ret = np.zeros((w, h), dtype=int)
for i in range(0, w):
for j in range(0, h):
if vis[i][j] == 0 and mmp[i][j] != 1:
cnt+=1
bfs(i, j, cnt, w, h, vis, mmp, mp_ret)
return mp_ret