CSP Tetris (201604-2), csp201604-2

Source: Internet
Author: User

CSP Tetris (201604-2), csp201604-2
Problem descriptionTetris is a casual game invented by Russian alexschey pakitov.
The game is carried out on a 15-row, 10-column grid. Each grid on the grid may have been placed in a square or not. In each round, there will be a new section composed of four small blocks falling from the top of the square chart. Players can operate the plate and move it to the right position, when the bottom edge of a square in a plate overlaps with the top edge of the square or reaches the bottom boundary, the plate is no longer moved. If a row of the square chart is filled with the square, the row is eliminated and scored.
In this case, you need to write a program to simulate the fall of a plate. You do not need to handle player operations, or cancel or score.
Specifically, given an initial square map, as well as the shape of a plate and its initial position, you need to give the final square map.Input FormatThe first 15 lines of the input contain the initial square chart. Each line contains 10 numbers. Adjacent numbers are separated by spaces. If a number is 0, it indicates that there is no square in the corresponding square. If the number is 1, it indicates that there is a square at the beginning. Make sure that the numbers in the first four rows are 0.
The input 16th to 19th rows contain the shape of the newly added plate. Each row contains four numbers to form a plate pattern. Similarly, 0 indicates no square, and 1 indicates that there is a square. The input ensures that the plate pattern contains 4 blocks, and the 4 blocks are connected together (to be precise, the 4 blocks are 4-connected, that is, the given plate is the standard plate of Tetris ).
The row 20th contains an integer between 1 and 7, indicating the column in the square chart at the leftmost of the plate pattern. Note that the plate pattern here refers to the plate pattern entered in lines 16 to 19. If the leftmost column of the plate pattern is 0, the left side of the plate is different from the left side of the actual section (see the example)Output FormatOutput 15 rows with 10 numbers in each row. Adjacent numbers are separated by a space, indicating the backward square chart under the plate. Note that you do not need to process the final elimination.Sample Input0 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 1 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 1 1 1
0 0 0 1
0 0 0 0
3Sample output0 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 1 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 0IdeasOne way is to simulate the gradual fall as in the game, and determine whether it overlaps with the original square each time. Note that the original blocks may be blank. Another method is to calculate the highest point of each column of the original Square and the lowest point of each column of the drop square to determine which row of the drop square will stop. Therefore, the length of each column is calculated directly (accurately speaking, it should be the relative length of the lowest point of the drop square, and the relative length of the highest point of the original square ). As shown in figure 1, the original length is 5 2 3. The length of the drop square is 1, 1, 2. The overlay length is 6 3 5, that is, the highest point of the drop box is 6; the original length is 5 2 as shown in figure 2. The length of the dropped square is 3. The stacked length is 8 5, that is, the highest point of the drop square is 8. The code for the second method is as follows:

1 # include <iostream> 2 using namespace std; 3 4 int main () {5 int a [15] [10]; 6 int in [4] [4]; 7 for (int I = 0; I <15; I ++) {8 for (int j = 0; j <10; j ++) {9 cin> a [I] [j]; 10} 11} 12 for (int I = 0; I <4; I ++) {13 for (int j = 0; j <4; j ++) {14 cin> in [I] [j]; 15} 16} 17 int n; 18 cin> n; 19 20 int m [4]; 21 for (int I = 0; I <4; I ++) {// calculates the relative length of each column in the drop box. 22 m [I] = 0; 23 for (int j = 0; j <4; j ++) {24 if (in [j] [I] = 1) m [I] = j + 1; 25} 26} 27 int f [4]; 28 (Int I = n-1; I <n + 3; I ++) {// calculate the relative length of each column in the original square. 29 f [I-n + 1] = 0; 30 for (int j = 0; j <15; j ++) {31 if (a [j] [I] = 1) {32 f [I-n + 1] = 15-j; 33 break; 34} 35} 36} 37 int max = 0; 38 for (int I = 0; I <4; I ++) {// calculate the maximum length, that is, 39 if (m [I]! = 0 & m [I] + f [I]> = max) {40 max = m [I] + f [I]; 41} 42} 43 for (int I = 0; I <4; I ++) {44 for (int j = 0; j <4; j ++) {45 if (in [I] [j]) a [15-max + I] [n-1 + j] = 1; 46} 47} 48 for (int I = 0; I <15; I ++) {49 for (int j = 0; j <10; j ++) {50 cout <a [I] [j] <"; 51} 52 cout <endl; 53} 54 return 0; 55}

Code for the first idea:

 1 #include<stdio.h> 2 int a[19][10],b[4][4]; 3 int compareBlock(int r,int c){ 4     for(int i=0;i<4;i++){ 5         for(int j=0;j<4;j++){ 6             if(b[i][j]==1&&a[i+r][j+c]==1) return 1; 7         } 8     } 9     return 0;10 } 11 int main()12 {13     for(int i=15;i<19;i++){14         for(int j=0;j<10;j++){15             a[i][j]=1;16         }17     } 18     int n,temp=0;19     for(int i=0;i<15;i++)20     {21         for(int j=0;j<10;j++)22         {23             scanf("%d",&a[i][j]);24         } 25     }26     for(int i=0;i<4;i++)27     {28         for(int j=0;j<4;j++)29         {30             scanf("%d",&b[i][j]);31         } 32     }33     scanf("%d",&n);34     int mark;35      36     for(int i=0;i<=14;i++)37     {38         if(compareBlock(i,n-1)) break;39         mark=i;40     }41     for(int i=0;i<4;i++){42         for(int j=0;j<4;j++){43             if(b[i][j]==1) a[mark+i][n-1+j]=1;44         }45     }46     for(int i=0;i<15;i++){47         for(int j=0;j<10;j++){48             printf("%d ",a[i][j]);49         }50         printf("\n");51     }52     return 0;53 } 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.