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. 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. 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 Sample output 0 0 0 0 0 0 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();
}
}
}
感觉讲的非常透彻,特别好,值得我学习