CCF CSP Brush Question Record 13-201604-2 Tetris (java)

Reprinted from: https://blog.csdn.net/wd2014610/article/details/98204878

Because I got 90 points, it shows that my code is not all right, and there are some places I haven't noticed, so I just look at the idea of ​​the great god, and it is very clear.

Question number: 201604-2
Question name: Tetris
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Tetris is a casual game invented by Russian Alexei Pakitnov.
  The game is played on a grid with 15 rows and 10 columns. Each grid on the grid may have squares or no squares. In each round, a new plate composed of 4 small squares will fall from the top of the grid. Players can operate the plate to move left and right and place it in a suitable position. When the upper edge of the upper square overlaps or reaches the lower boundary, the plate will no longer move. If a row of the grid graph is filled with squares at this time, the row will be eliminated and scored.
  In this problem, you need to write a program to simulate the falling of the plates. You don't need to deal with player actions, and you don't need to deal with elimination and scoring.
  Specifically, given an initial grid, as well as the shape of a plate and the initial position where it falls, you have to give the final grid.

Input format

  The first 15 lines of input contain the initial grid graph, each line contains 10 numbers, and adjacent numbers are separated by spaces. If a number is 0, it means there are no squares in the corresponding square, if the number is 1, it means there are squares at the beginning. Enter to ensure that the numbers in the first 4 lines are all 0.
  The 16th to 19th lines of the input contain the shape of the newly added plate. Each line contains 4 numbers to form the pattern of the plate. Similarly, 0 means no square, and 1 means there is a square. The input guarantee plate pattern contains exactly 4 squares, and the 4 squares are connected together (to be precise, the 4 squares are four-connected, that is, the given plate is the standard Tetris plate).
  The 20th line contains an integer between 1 and 7, which indicates which column of the grid is in the leftmost column of the pattern. Note that the plate pattern here refers to the plate pattern entered in rows 16 to 19. If the leftmost column of the plate pattern is all 0, its left side and the actual left side of the plate are inconsistent (see example)

Output format

  Output 15 lines with 10 numbers in each line, separated by a space between adjacent numbers, which represents the grid graph behind the plate. Note that you do not need to deal with the final cancellation.

Sample input

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
1 1 1 0 0 0 1 1 1 1
0 0 0 0 1 0 0 0 0 0
0 0 0 0
0 1 1 1
0 0 0 1
0 0 0 0
3

Sample output

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 1 1 0 0 0 0

 

一下是大神的分析过程:

import java.util.Scanner;

/**
 * 遇见这样的复杂图像题目
 * 不要慌,先想办法将图片以合适的方式,完整的录入进来再说
 * @author wangdong
 *
 */
public class Main {
	public static void main(String[] args) {
		Main.run();
	}
	
	public static void run() {
		Scanner scanner = new Scanner(System.in);
		int[][] arr = new int[15][10];
		int[][] arrSqu = new int[4][4];
		
		//将输入的方格图存入数组
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 10; j++) {
				arr[i][j] = scanner.nextInt();
			}
		}
		
		//将输入的方块存入数组
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				arrSqu[i][j] = scanner.nextInt();
			}
		}
		//获取方块将要下沉的位置,偏移
		int column = scanner.nextInt();
		scanner.close();
		//获取这个方块值为1的每个节点在4*4矩阵中的位置
		int[] x = new int[4];
		int[] y = new int[4];
		int z = 0;
		
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (arrSqu[i][j] == 1) {
					x[z] = i;
					y[z] = j+column-1;
					z++;
				}
			}
		}
		boolean flag = true;
		int count = 0;
		int offset = 0;
		outer:
			while (flag) {
				for (int i = 0; i < 4; i++) {
					//判断最下面的边界,如果有一个方块到了最下面,直接结束下沉
					if ((x[i] + offset) == 14) {
						for (int j = 0; j < 4; j++) {
							arr[x[j] + offset][y[j]] =1;
						}
						break outer;
					}
					if (arr[x[i] + offset][y[i]]==0) {//不断下沉
						count++;
					}
					
				}
				
				if (count == 4) {
					offset++;
					count=0;
				}else {
					for (int i = 0; i < 4; i++) {
						arr[x[i] + offset -1][y[i]] = 1;
					}
					flag = false;
				}
				
			}
		
		//遍历下沉后的矩阵
        for(int i=0;i<15;i++){
            for(int j=0;j<10;j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
		
	}
}

 

感觉讲的非常透彻,特别好,值得我学习

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108310908
Recommended