201512-2 python CCF 更简单的思路和算法 100分

n, m = map(int, input().split())
chessboard = [list(map(int, input().split())) for i in range(n)]#输入数据
flagboard = [[1] * m for i in range(n)]# 1表示未被消去
for i in range(n):#行遍历
    for j in range(m - 2):
        if chessboard[i][j] == chessboard[i][j + 1] == chessboard[i][j + 2]:
            flagboard[i][j] = 0
            flagboard[i][j + 1] = 0
            flagboard[i][j + 2] = 0
for j in range(m):#列遍历
    for i in range(n - 2):
        if chessboard[i][j] == chessboard[i + 1][j] == chessboard[i + 2][j]:
            flagboard[i][j] = 0
            flagboard[i + 1][j] = 0
            flagboard[i + 2][j] = 0
for i in range(n):#按照标记消去元素
    for j in range(m):
        chessboard[i][j] *= flagboard[i][j]
    print(" ".join(map(str, chessboard[i])))

心得:消去元素的题,先标记,再消去

注意如下代码:

flagboard = [[1] * m for i in range(n)]

如果改为

flagboard = [[1] * m] * n

就会出问题

原因点我

你可能感兴趣的:(python,算法,csp)