OPPO CV笔试一题
binaryImage = [[0, 1, 0], [1, 0, 1], [1, 0, 1], [1, 0, 1], [0, 0, 0]] # binaryImage = [[0, 1, 0, 0], [1, 0, 1, 0], [1, 0, 1, 1], [1, 0, 0, 1], [0, 1, 1, 1]] row = len(binaryImage) col = len(binaryImage[0]) zuobiao = set() # 1像素坐标 zeros = set() # 环内0 ring = 1 for i in range(row): for j in range(col): if binaryImage[i][j] == 0: need = [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]] ans = 0 for k in need: if 0 <= k[0] < row and 0 <= k[1] < col and binaryImage[k[0]][k[1]] == 1: ans += 1 if 0 <= k[0] < row and 0 <= k[1] < col and (k[0], k[1]) in zeros: ans += 1 if ans >= 3: zeros.add((i, j)) if len(zeros) == 0: print(0) else: for zero in zeros: i = zero[0] j = zero[1] need = [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]] for k in need: if 0 <= k[0] < row and 0 <= k[1] < col and binaryImage[k[0]][k[1]] == 1: zuobiao.add((k[0], k[1])) if 0 <= k[0] < row and 0 <= k[1] < col and binaryImage[k[0]][k[1]] == 0 and (k[0], k[1]) not in zeros: ring = 0 break if ring == 0: print(0) else: print(len(zuobiao))
#OPPO##笔经#