小美的好矩阵 python
题目链接:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2023/11/3 19:34 # @Author : lanlin # 小美的好矩阵 def judge33(matrix, y, x): result = True flag_x = [-1, 0, 1, -1, 0, 1, -1, 0, 1] flag_y = [-1, -1, -1, 0, 0, 0, 1, 1, 1] flag_ABC = set() for i in range(len(flag_y)): xx = x+flag_x[i] yy = y+flag_y[i] temp = matrix[yy][xx] # 判断三种字符是否都出现过,且没有其他字符 if temp in ['A','B','C']: flag_ABC.add(temp) else: return False # 判断字符重复,每个元素只需判断其下、右两元素即可 if (yy+1)<(y+2) and temp == matrix[yy+1][xx]: return False if (xx+1)<(x+2) and temp == matrix[yy][xx+1]: return False if len(flag_ABC) != 3: result = False return result def judge(data, m, n): result = 0 for y in range(m-2): for x in range(n-2): if judge33(data, y+1, x+1): result += 1 return result if __name__=='__main__': array1 = input().split(" ") m = int(array1[0]) n = int(array1[1]) data_array = [] for i in range(m): temp_list = list(input()) data_array.append(temp_list) print(judge(data_array, m, n)) ''' 4 4 DABC ABAB BABA BBAB '''