思路:
- 由题可知,棋子的颜色在1~9之间,所以按照1~9依次遍历。
- 遍历的方式是先遍历行,再遍历列。判断连续的三个数是否相等,若发现相等则将这三个数的位置添加到临时列表temp中。
注意:
- 直接将二维列表里的相应元素直接改为0,因为会影响接下来的遍历,所以记下位置暂存在临时列表中。
- 只需要连续三个数相等即可,因为三个以上连续相等的数也会被包含。
满分代码:
"""
测试用例1
4 5
2 2 3 1 2
3 4 5 1 4
2 3 2 1 3
2 2 2 4 4
测试用例2
4 5
2 2 3 1 2
3 1 1 1 1
2 3 2 1 3
2 2 3 3 3
测试用例3
2 3
1 1 1
1 2 3
测试用例4
2 2
1 1
2 2
"""
n, m = map(int, input().split())
a = [[] for i in range(n)]
for i in range(n):
line = list(map(int, input().split()))
for j in range(m):
a[i].append(line[j])
for color in range(1, 10):
temp = []
for i in range(0, n):
for j in range(0, m-2):
if a[i][j] == color and a[i][j+1] == color and a[i][j+2] == color:
temp += [[i, j], [i, j+1], [i, j+2]]
for i in range(0, m):
for j in range(0, n-2):
if a[j][i] == color and a[j+1][i] == color and a[j+2][i] == color:
temp += [[j, i], [j+1, i], [j+2, i]]
for t in temp:
a[t[0]][t[1]] = 0
for i in range(n):
print(" ".join(map(str, a[i])))