CCF CSP Brush Question Record 11-201512-2 Elimination Game (Java)

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.
  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 nm , separated by spaces, indicating the number of rows and columns of the chessboard, respectively.
  The next n lines, each line of m integers, 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, 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
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 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
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 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 ​​≤  nm  ≤ 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();
		}
	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108303411