【CCF CSP】 201512-2 Elimination game (100 points)

Question number: 201512-2
Question name: elimination game
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:
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 piece is placed on the square of each row and each column of the board. 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, each square in the chessboard has a chess piece, please give the chessboard after one elimination.
  Please note: a pawn may be eliminated in a row and a column at the same time.
input format
  The first line of input contains two integers n , m , separated by spaces, representing the number of rows and columns of the chessboard, respectively.
  The next n lines, each line of m integers, separated by spaces, respectively represent the color of the pieces in each square. Colors are numbered from 1 to 9.
output format
  Output n lines, each line contains m integers, and adjacent integers are separated by a space, indicating the chessboard after one elimination. If a pawn in a square is eliminated, the corresponding square outputs 0, otherwise the color number of the pawn is 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 description
  The 1 in the 4th column and the 2 in the 4th row of the board can be eliminated, and the pieces in the other squares are retained.
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
Sample description
  All 1's on the board and 3's in the last row can be eliminated at the same time, and the pieces in the other squares are kept.
评测用例规模与约定
  所有的评测用例满足:1 ≤ n, m ≤ 30。

代码

C++

#include <iostream>
#include <cmath>
#define MAX_GARD 31
using namespace std;

int main(int argc, char** argv) {
    int n,m;
    int grad[MAX_GARD][MAX_GARD];
    cin>>n>>m;
    //输入 
    for(int i=1;i<=n;i++)   
    {
        for(int j=1;j<=m;j++)
        {
            cin>>grad[i][j];
        }
    }
    //计算需横向要消去的值
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m-2;j++)
        {
            //横轴三位相同 
            if((abs(grad[i][j])==abs(grad[i][j+1]))&&(abs(grad[i][j+1])==abs(grad[i][j+2])))
            {
                grad[i][j]=-abs(grad[i][j]);
                grad[i][j+1]=-abs(grad[i][j+1]);
                grad[i][j+2]=-abs(grad[i][j+2]);
            }        
        }
    } 

    //计算需纵向要消去的值
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n-2;j++)
        {
            //纵向轴三位相同 
            if((abs(grad[j][i])==abs(grad[j+1][i]))&&(abs(grad[j+1][i])==abs(grad[j+2][i])))
            {
                grad[j][i]=-abs(grad[j][i]);
                grad[j+1][i]=-abs(grad[j+1][i]);
                grad[j+2][i]=-abs(grad[j+2][i]);
            }        
        }
    }

    //消除标记位 
    for(int i=1;i<=n;i++)   
    {
        for(int j=1;j<=m;j++)
        {
            if(grad[i][j]<0)
                grad[i][j]=0;
        }
    }

    //输出结果 
    for(int i=1;i<=n;i++)   
    {
        for(int j=1;j<=m;j++)
        {
            if(j==m)
                cout<<grad[i][j]<<endl;
            else 
                cout<<grad[i][j]<<' ';
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325919434&siteId=291194637