Question number: | 201512-2 |
Question name: | Elimination games |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description 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 the square of each row and column of the board. It is a row or column. 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. Input format The first line of input contains two integers n , m , separated by spaces, indicating the number of rows and columns of the chessboard, respectively. Output format Output n lines, each line of m integers, separated by a space between adjacent integers, 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 Sample output 2 2 3 0 2 Sample description The 1 in the 4th column and the 2 in the 4th row in the chessboard can be eliminated, and the pieces in the other squares are retained. Sample input 4 5 Sample output 2 2 3 0 2 Sample description All 1s and 3s in the last row of the board can be eliminated at the same time, and the pieces in the other squares are retained. Evaluation use case scale and conventions All evaluation use cases satisfy: 1 ≤ n , m ≤ 30. |
For this question, you can save the index number and assign the value to 0, otherwise it is not easy to operate
The code for 100 points is as follows:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class 消除类游戏 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int[][] a=new int[n][m];
List<Integer> list1 = new ArrayList();
List<Integer> list2 = new ArrayList();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m-2;j++){
if(a[i][j]==a[i][j+1]&&a[i][j+1]==a[i][j+2]){
list1.add(i);
list2.add(j);
list1.add(i);
list2.add(j+1);
list1.add(i);
list2.add(j+2);
}
}
}
for(int j=0;j<m;j++){
for(int i=0;i<n-2;i++){
if(a[i][j]==a[i+1][j]&&a[i+1][j]==a[i+2][j]){
list1.add(i);
list2.add(j);
list1.add(i+1);
list2.add(j);
list1.add(i+2);
list2.add(j);
}
}
}
for(int i=0;i<list1.size();i++){
a[(int) list1.get(i)][(int) list2.get(i)]=0;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}