Problem description
Elimination game is a popular game. The game is played on a chessboard with n rows and m rows. A colored chessboard is placed on the square of each row and column of the chessboard. When there are three or more chessboards of the same color in a row or column, these chessboards are eliminated. When there are many places that can be eliminated, the chess pieces in these places will be eliminated at the same time.
Now give you a chessboard with n rows and m rows. Each square in the chessboard has a chessboard. Please give a chessboard that has been eliminated once.
Note: A chess piece may be eliminated at the same time in a row and a column.
Input format
The first line of input contains two integers n, m, separated by spaces, representing the number of rows and columns on the chessboard, respectively.
Next, n rows, m integers per row, separated by spaces, represent the color of the pieces in each square. Colors are numbered 1 to 9.
Output format
Output n rows, m integers per row, adjacent integers are separated by a space, indicating the chessboard after one elimination. If the chess pieces in a square are eliminated, the corresponding square outputs 0, otherwise the color number of the chess pieces 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 explanation
The first and second rows of the fourth column and the fourth row in the chessboard can be eliminated, while 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 explanation
All 1 in the board and 3 in the last line can be eliminated at the same time, while the pieces in other squares are retained.
Assessment of use case size and conventions
All evaluation cases satisfy: 1 < n, m < 30.
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[][] a = new int[n][m]; int[][] b = new int[n][m];//b Special for operation for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = sc.nextInt(); b[i][j] = a[i][j]; } } // First check whether there are more than three consecutive transverse for (int i = 0; i < n; i++) { for (int j = 1; j < m-1; j++) { if(a[i][j-1]==a[i][j] && a[i][j]==a[i][j+1])//There are three consecutive identical { int point = j+1; //Point to three identical third if(j+2==m) //j+1 is the last { b[i][j-1] = 0;b[i][j] = 0;b[i][j+1] = 0; break; } else { for (int j2 = j+2; j2 < m; j2++) { //Are there three identical ones behind them? if(a[i][j2]==a[i][j+1] && j2==point+1) b[i][j2] = 0; else point = -30; point++; } b[i][j-1] = 0;b[i][j] = 0;b[i][j+1] = 0; } } } } // Re-test whether there are more than 3 consecutive vertical tests for (int i = 0; i < m; i++) { for (int j = 1; j < n-1; j++) { if(a[j-1][i]==a[j][i] && a[j][i]==a[j+1][i])//There are three consecutive identical { int point = j+1; //Point to three identical third if(j+2==n) //j+1 is the last { b[j-1][i] = 0;b[j][i] = 0;b[j+1][i] = 0; break; } else { for (int j2 = j+2; j2 < n; j2++) { //Are there three identical ones behind them? if(a[j2][i]==a[j+1][i] && j2==point+1) b[j2][i] = 0; else point = -30; point++; } b[j-1][i] = 0;b[j][i] = 0;b[j+1][i] = 0; } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.print(b[i][j]+" "); } System.out.println(); } } }