CCF 201512-2 Elimination game C language implementation, full score code

CCF 201512-2 questions

topic

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 each grid 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 piece 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, 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, the adjacent integers are separated by a space, which means the chessboard after elimination once. 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 on the board can be eliminated, and the 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 The
sample shows
  all the 1s in the chessboard and the last row 3 can be eliminated at the same time, and the pieces in the other squares are kept.
Evaluation use case scale and conventions
  All evaluation use cases satisfy: 1 ​​≤ n, m ≤ 30.

Code

The overall idea is: Calculate the same numbers in the horizontal and vertical directions. If the number of the same numbers exceeds 3 or more, set it to 0.
The code is shown below, with a full score of 100.
Insert picture description here

#include<stdio.h>
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	int a[n][m] = {0};
	int b[n][m] = {0};
	int c[m][n] = {0}; 
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			scanf("%d",&a[i][j]);
	// 计算横向的重复三次及以上的数字 
	for(int i=0;i<n;i++)
	{
		int p=1;
		int k = a[i][0];
		for(int j=1;j<m;j++)
		{
			if(a[i][j] == k)
			{
				p++;
			}
			else if(p >= 3)
			{
				int q = j;
				for(int l=0;l<p;l++)
				{
					b[i][q-1] = 1;
					q--;
				}
				k = a[i][j];
				p = 1;
			}
			else
			{
				k = a[i][j];
				p = 1;
			}
			if(j == m-1 && p>=3)
			{
				int q = j;
				for(int l=0;l<p;l++)
				{
					b[i][q] = 1;
					q--;
				}
			}
		}
	}
	// 计算纵向的重复三次及以上的数字,因其计算方式一样,所以先进行转置,再用之前方法进行计算,
	// 最后再转置回来,相当于对纵向计算重复三次以上的数字。 
	//对矩阵进行转置 
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			c[j][i] = a[i][j];
		}
	}
	
	for(int i=0;i<m;i++)
	{
		int p=1;
		int k = c[i][0];
		for(int j=1;j<n;j++)
		{
			if(c[i][j] == k)
			{
				p++;
			}
			else if(p >= 3)
			{
				int q = j;
				for(int l=0;l<p;l++)
				{
					c[i][q-1] = 0;
					q--;
				}
				k = c[i][j];
				p = 1;
			}
			else
			{
				k = c[i][j];
				p = 1;
			}
			if(j == n-1 && p>=3)
			{
				int q = j;
				for(int l=0;l<p;l++)
				{
					c[i][q] = 0;
					q--;
				}
			}
		}
	}
	// 再进行转置 
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			a[j][i] = c[i][j];
		}
	}
	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(b[i][j] == 1)
				a[i][j] = 0;
			printf("%d ",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

The writing is rough and the algorithm needs to be improved.

Guess you like

Origin blog.csdn.net/qq_41018465/article/details/109265783