The ccf Question Bank eliminated games in December 2, 2015.

Source: Internet
Author: User

The ccf Question Bank eliminated games in December 2, 2015.

The questions are as follows:

Problem description: elimination games are popular games that are played on a game board containing n rows and m columns, there is a colored chess piece on the square of each column in each row of the board. When one row or column has three or more chess pieces of the same color in a row or column, these pawns are eliminated. When multiple pieces can be eliminated, the pieces in these places will be eliminated at the same time. Now we will give you a chessboard with n rows and m columns. Each square in the checker has a pawnboard. Please give it an eliminated checker. Note: A piece may be eliminated at the same time in a row and column. The first line in the input format contains two integers, n and m, separated by spaces, indicating the number of rows and columns on the board. In the next n rows, each row has m integers separated by spaces to represent the color of the pawns in each square. The colors are numbered from 1 to 9. The output format outputs n rows. Each row has m integers. Adjacent integers are separated by a space, indicating the Board after an elimination. If the chess piece in a square is eliminated, the corresponding square is 0; otherwise, the color number of the chess piece is output. Sample input 4 52 2 3 1 23 4 5 1 42 3 2 1 32 2 2 4 4 4 sample output 2 2 2 3 0 23 4 5 0 42 3 2 0 30 0 4 4. Example 2 of rows 1 and 4th of the 4th columns in the checker can be eliminated, the pawns in other squares are retained. Sample input 4 52 2 3 1 23 1 1 12 3 2 1 32 2 3 3 3 3 sample output 2 2 3 0 23 0 0 0 02 3 2 0 32 32 2 0 0 0 0 example 0 indicates that all 1 in the Board and 3 in the last line can be eliminated at the same time, the pawns in other squares are retained. The scale and conventions of the evaluation cases meet the following requirements: 1 ≤ n, m ≤ 30.

This idea is relatively simple. we can write a detection function func to test the case where the repeated elements in an array are greater than or equal to 3. Then, perform the func operation on each column and each row in the main function.

The Code is as follows:

#include<stdlib.h>#include<iostream>using namespace std;void func(int * s,int len){    int count=0;    int i;    for(i=0;i<len-1;i++){        if(s[i]==s[i+1]||(s[i]-10)==s[i+1]||s[i]==(s[i+1]-10)){            count++;        }else{            if(count>=2){                for(int j=0;j<=count;j++)                {                    s[i-j]+=10;                }                count=0;            }else{                count=0;            }        }    }    if(i==len-1&&count>=2){        for(int j=0;j<=count;j++)        {            s[i-j]+=10;        }    }}int main(){    int m,n;    cin>>n>>m;    int s[n][m];    for(int i=0;i<n;i++){        for(int j=0;j<m;j++){            cin>>s[i][j];        }    }    //-------------func--------------------    for(int i=0;i<n;i++)    {        func(s[i],m);    }    for(int i=0;i<m;i++)    {        int temp[n];        for(int j=0;j<n;j++){            temp[j]=s[j][i];        }        func(temp,n);        for(int j=0;j<n;j++){            if(temp[j]>10){                s[j][i]=temp[j];            }        }    }    //------------------show result------------------    for(int i=0;i<n;i++){        for(int j=0;j<m;j++){                if(s[i][j]>10)                {                    s[i][j]=0;                }                cout<<s[i][j]<<" ";        }        cout<<endl;    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.