w = int(input())
h = int(input())
cluster = [0] * (w*h) # 存储当前星群的每个点坐标
size = 0 # 存储当前星群的星群数量
hash_table = []
move = [[0, 1], [1, 0], [-1, 0], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1]]
label = ord('a')
grids = []
for _ in range(h):
grids.append(list(input()))
def dfs(x, y):
global w, h, size
cluster[size] = [x, y]
size += 1
grids[x][y] = '0'
for mi, mj in move:
nx, ny = x + mi, y + mj
if 0 <= nx < h and 0 <= ny < w and grids[nx][ny] == '1':
dfs(nx, ny)
return
def cal_hash():
global size
ret = 0
for i in range(size):
for j in range(i+1, size):
x1, y1 = cluster[i]
x2, y2 = cluster[j]
ret += ((x1-x2)**2 + (y1-y2)**2)**0.5
return ret
def get_label(hash_code):
global label
for h2, l2 in hash_table:
if abs(h2 - hash_code) < 1e-6:
return l2
ret = chr(label)
hash_table.append([hash_code, chr(label)])
label += 1
return ret
for x in range(h):
for y in range(w):
if grids[x][y] == '1':
size = 0
dfs(x, y)
hash_code = cal_hash()
tmp_label = get_label(hash_code)
for i in range(size):
x1, y1 = cluster[i]
grids[x1][y1] = tmp_label
for i in range(h):
print("".join(grids[i]))
感谢Py版~