def connect_to_X(data):     temp = data     temp_2 = copy.deepcopy(temp)     # 解决横向连通     for i in range(len(temp)):         for j in range(len(temp[i])):             if j >= len(temp[i])-2:                 break             else:                 if temp[i][j] == temp[i][j+1] == temp[i][j+2]:                     temp_2[i][j], temp_2[i][j+1], temp_2[i][j+2] = "X", "X", "X"     # 解决竖向连通     for i in range(len(temp)):         for j in range(len(temp[i])):             if i >= len(temp)-2:                 break             else:                 if temp[i][j] == temp[i+1][j] == temp[i+2][j]:                     temp_2[i][j], temp_2[i+1][j], temp_2[i+2][j] = "X", "X", "X"     return down(temp_2) def down(data):     temp = data     temp_2 = copy.deepcopy(temp)     for i in range(len(temp)):         for j in range(len(temp[i])):             if temp[i][j] == "X":                 temp_2[0][j] = "X"                 for k in range(1, i+1):                     temp_2[k][j] = temp[k-1][j]     if temp_2 == temp:         print(temp_2)     else:         return connect_to_X(temp_2) with open(r"D:/Downloads/test.txt") as f:     import copy     lines = f.readlines()     n = len(lines)     temp = []     for i in lines:         temp.append(i.strip().split(" "))     connect_to_X(temp)
点赞 评论

相关推荐

昨天 16:31
已编辑
桂林电子科技大学 Python
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务