Elimination games
I. Description of the problem
Problem Description
elimination game is a game welcomed by the public, in a game conducted on the game board contains a column of n row m, stood a colored checkerboard pieces on each row of each column, when a row when three consecutive or more of the same color or a piece, these pieces are eliminated. When there are multiple can be eliminated, where the pieces will also be eliminated.
You are given a chessboard n rows and m columns, there is a piece on the board in each box, give the board after the first elimination.
Note: a piece may also be eliminated in a row and a column.
Input format
of the first line of input contains two integers n, m, separated by spaces, respectively, number of rows and columns of the board.
Next n lines of m integers, separated by spaces, respectively, each of the color pieces of a square. Color number using 1-9.
Output format
output n lines of m integers, using a space separation between adjacent integers, represents the elapsed time after the elimination of the board. If a piece of the box is eliminated, the output of the corresponding box 0, otherwise the output color number of pieces.
Sample input
. 4. 5
2. 3. 1 2 2
. 3. 4. 5. 1. 4
2 2. 3. 1. 3
2 2 2. 4. 4
sample output
2 0 2. 3 2
. 3. 4. 5. 4 0
2. 3. 3 2 0
0 0 0. 4. 4
Sample DESCRIPTION
board 1 and the second line 42 may be eliminated in the fourth column, the other pieces are retained in the grid.
Sample input
. 4. 5
2 2 2. 3. 1
1 1 1 1. 3
2. 3. 3 2 1
2 2. 3. 3. 3
sample output
2 0 2. 3 2
. 3 0 0 0 0
2 0 2. 3. 3
2 2 0 0 0
Example Description
All board 1 and last row 3 can be simultaneously eliminated, other squares in the piece are reserved.
Evaluation scale use cases and conventions
in all use cases satisfy Reviews: 1 ≤ n, m ≤ 30 .
#消除类游戏
n,m = map(int,input().split())
a = []
d = {}
for i in range(n):
a.append(list(map(int,input().split()))) #二维数组储存数据
#利用字典d来储存每个下标的状态,当值为1的时候说明被消除
for i in range(n):
for j in range(m):
d[(i,j)] = 0
#横向遍历
for i in range(n):
for j in range(m-2):
if a[i][j] == a[i][j+1] and a[i][j+1] == a[i][j+2]:
d[(i,j)] = 1
d[(i,j+1)] = 1
d[(i,j+2)] = 1
#纵向遍历
for i in range(m):
for j in range(n-2):
if a[j][i] == a[j+1][i] and a[j+1][i] == a[j+2][i]:
d[(j,i)] = 1
d[(j+1,i)] = 1
d[(j+2,i)] = 1
#将下标为1的数组数据变为0
for i in d:
if d[i] == 1:
x = i[0]
y = i[1]
a[x][y] = 0
for i in range(n):
for j in range(m):
print(a[i][j],end=' ')
print('')