CCF201512-2 elimination games (C language)

topic

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 .

Ideas analysis

1. A flag to flag array flag, set, set to 0 at a position other positions may be eliminated, and the final array output by the flag.
2. whether judges can eliminate the function I set two functions, respectively judgment passed in a position (row, col) whether to eliminate rows and columns can be eliminated. Can be eliminated if, in the corresponding set flag array location.

C language source code (100)

#include <stdio.h>
#define N 30
#define M 30
int chessboard[N][M];
int flag[N][M];
int eliminateRow(int row,int col)
{
	if(chessboard[row][col] == chessboard[row][col+1] && chessboard[row][col+1] == chessboard[row][col+2])
	{
		flag[row][col] = 1;
		flag[row][col+1] = 1;
		flag[row][col+2] = 1;	
		return 1;
	} 
	return 0;
}

int eliminateCol(int row,int col)
{
	if(chessboard[row][col] == chessboard[row+1][col] && chessboard[row+1][col] == chessboard[row+2][col])
	{
		flag[row][col] = 1;
		flag[row+1][col] = 1;
		flag[row+2][col] = 1;
		return 1;
	}
	return 0;
}

void setFlag(int n,int m)
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(j+2 < m)
				eliminateRow(i,j);
			if(i+2 < n)
				eliminateCol(i,j);
		}
	}
}


int main()
{
	int n,m,i,j;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			scanf("%d",&chessboard[i][j]);
			flag[i][j] = 0;
		}
	setFlag(n,m);
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			if(flag[i][j])
				printf("0 ");
			else
				printf("%d ",chessboard[i][j]);
		}
		printf("\n");
	}
	return 0;
} 
Published 92 original articles · won praise 110 · views 110 000 +

Guess you like

Origin blog.csdn.net/weixin_44589540/article/details/104227032