2019 Xidian Institute of Computer Test-3 Elimination Game

Problem description The
  elimination game is a popular game. The game is played on a game board with n rows and m columns. A colored chess piece is placed on a square in each row and column of the board. Or when there are three or more pieces of the same color in a row, these pieces are eliminated. When there are multiple places that can be eliminated, the pieces in these places will be eliminated at the same time.
  Now give you a chessboard with n rows and m columns. There is a chess piece on each square in the chessboard. Please give the chessboard after elimination once.
  Please note: A pawn may be eliminated in a certain row and a certain column at the same time.
Input format
  The first line of input contains two integers n, m, separated by spaces, which respectively represent the number of rows and columns of the chessboard.
  In the next n rows, m integers in each row, separated by spaces, respectively represent the color of the chess pieces in each square. The colors are numbered from 1 to 9.
Output format
  output n lines, each line of m integers, separated by a space between adjacent integers, representing the chessboard after one elimination. If a piece in a square is eliminated, the corresponding square will output 0, otherwise the color number of the piece will be output.
Sample input

4 5
2 2 3 1 2
3 4 5 1 4
2 3 2 1 3
2 2 2 4 4

Sample output

2 2 3 0 2
3 4 5 0 4
2 3 2 0 3
0 0 0 4 4

Sample explanation
  The 1 in the 4th column and the 2 in the 4th row in the chessboard can be eliminated, and the chess pieces in the other squares are kept.
Sample input

4 5
2 2 3 1 2
3 1 1 1 1
2 3 2 1 3
2 2 3 3 3

Sample output

2 2 3 0 2
3 0 0 0 0
2 3 2 0 3
2 2 0 0 0
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 30;

int a[N][N], f[N][N];//f标记用

int main()
{
    
    
    int n, m;
    cin>>n>>m;
    for(int i = 0; i < n; i++)
    {
    
    
        for(int j = 0; j < m; j++)
        {
    
    
            cin>>a[i][j];
        }
    }
    memset(f, 0, sizeof(f));
    for(int i = 0; i < n; i++)
    {
    
    
        for(int j = 0; j < m - 2; j++)
        {
    
    
            if(a[i][j] == a[i][j + 1] && a[i][j + 1] == a[i][j + 2])
                f[i][j] = f[i][j + 1] = f[i][j + 2] = 1;
        }
    }
    for(int j = 0; j < m; j++)//这里第一次出错啦,i.j写反了
    {
    
    
        for(int i = 0; i < n - 2; i++)
        {
    
    
            if(a[i][j] == a[i + 1][j] && a[i + 1][j] == a[i + 2][j])
                f[i][j] = f[i + 1][j] = f[i + 2][j] = 1;
        }
    }
    for(int i = 0; i < n; i++)
    {
    
    
        for(int j = 0; j < m; j++)
        {
    
    
            if(f[i][j])
                a[i][j] = 0;
        }
    }
    for(int i = 0; i < n; i++)
    {
    
    
        for(int j = 0; j < m; j++)
        {
    
    
            if(j != 0)
                cout<<" ";
            cout<<a[i][j];
        }
        cout<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/KO812605128/article/details/114419032