In the ccf question Library, the question of the Russian square in December April 2, 2016, ccf in December 2016

Source: Internet
Author: User

In the ccf question Library, the question of the Russian square in December April 2, 2016, ccf in December 2016

The questions are as follows:

Problem description: Tetris is a casual game invented by Russian alexsey 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. The first 15 rows of input in the input format contain the initial square chart, each line contains 10 numbers, and 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, then, the left side of it is inconsistent with the left side of the actual section (see the example). The output format is 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 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 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 1 0 0 0 1 1 1 10 0 0 0 1 0 0 0 0 0 00 0 0 00 1 1 1 10 0 0 0 03 sample output 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 01 1 1 1 1 1 1 1 1 1 1 1 10 0 0 0 1 1 0 0 0 0

This method is simple and easy to understand. The Code is as follows:

#include<stdio.h>#include<stdlib.h>#include<iostream>using namespace std;struct Node{    int x;    int y;};int main(){    int s[15][10];    Node t[4];    for(int i=0;i<15;i++)    {        for(int j=0;j<10;j++)        {            cin>>s[i][j];        }    }    int count=0;    for(int i=0;i<4;i++)    {        for(int j=0;j<4;j++)        {            int temp;            cin>>temp;            if(temp==1)            {                t[count].x=i;                t[count].y=j;                count++;            }        }    }    int col;    cin>>col;    if(col+4>=15)    {        return 0;    }else{        for(int i=0;i<4;i++)        {            t[i].y+=col-1;        }    }    int flag=0;    while(1)    {        for(int i=0;i<4;i++)        {            if(s[t[i].x][t[i].y])            {                flag=1;                break;                }        }        if(flag)        {            break;        }else{            for(int i=0;i<4;i++)            {                t[i].x++;            }        }    }    if(flag==1)    {        for(int i=0;i<4;i++)        {            s[t[i].x-1][t[i].y]=1;        }    }    for(int i=0;i<15;i++)    {        for(int j=0;j<10;j++)        {            cout<<s[i][j]<<" ";        }        cout<<endl;    }    return 0;}

My idea at the beginning was, why isn't that faster without starting from the last line? Later, I found that this idea was not correct. from the bottom up, I had to traverse up to 0th rows, which was a huge computing workload.

Next, let me talk about my idea: first find the bottom row of the corresponding square in the large matrix through traversal, and then fill the graph into the large matrix based on the number of rows. For details, refer to the code.

# Include <stdio. h> # include <stdlib. h >#include <iostream >#include <algorithm> using namespace std; struct Node {int x; int y; int row ;}; int cmp (Node x, Node y) {return x. x> y. x;} int main () {int s [15] [10]; Node t [4]; Node temp [4]; // here t is used as an array of time periods, temp is mainly used to conveniently assign values for (int I = 0; I <15; I ++) {for (int j = 0; j <10; j ++) {cin> s [I] [j] ;}} int count = 0; for (int I = 0; I <4; I ++) {for (int j = 0; j <4; j ++) {int tem; cin> tem; if (tem = 1) {T [count]. x = I; t [count]. y = j; t [count]. row = 0; temp [count]. x = I; temp [count]. y = j; temp [count]. row = 0; count ++ ;}}int col; cin >>col; if (col + 4 >=15) {return 0 ;} else {for (int I = 0; I <4; I ++) {t [I]. y + = col-1; temp [I]. y + = col-1 ;}} sort (t, t + 4, cmp); sort (temp, temp + 4, cmp ); // ----------------- find the bottom row ----------------------- int row, real_row; for (real_row = 14; real_row> = 0; real_row --) {// traverse a row, start from the bottom layer. Row = real_row; int flag; for (; row> = 0; row --) // traverses all rows from the lowest row to the first row {int temp_row = row; flag = 0; for (int j = 0; j <4; j ++) {if (j = 0) {if (temp_row <0) {break ;} if (s [temp_row] [t [j]. y] = 1) {flag = 1; // tag failed break;} else {temp_row = temp_row-t [j]. x + t [j + 1]. x ;}} else {if (temp_row <0) {break;} if (s [temp_row] [t [j]. y] = 1) {flag = 1; // tag failed break;} else {if (j <= 2) {temp_row = temp_row-t [j]. x + t [j + 1]. x ;}}}if (flag = 1) {// No required To traverse, jump out of the loop and traverse break from real_row to 0 on the next real_row;} if (flag = 0) {// indicates that the number of rows meeting the condition exists, stop execution. Break ;}/// -------------- re-assigned -------------------- // cout <"the final number of rows is:" <real_row <endl; for (int I = 0; I <4; I ++) {if (I = 0) {temp [I]. x = real_row; real_row = real_row-t [0]. x + t [1]. x;} else {temp [I]. x = real_row; if (I <= 2) {real_row = real_row-t [I]. x + t [I + 1]. x; // use the function difference in the small matrix to determine the number of rows in the corresponding large matrix. For example, t [0] is the second row in the small matrix, t [1] is the first row in the small matrix, and t [0] is the first row in the large matrix, then t [1] should be 13-2 + 1 = 12 rows} s [temp [I]. x] [temp [I]. y] = 1;} // -------------- output result -------------------- for (int I = 0; I <15; I ++) {for (int j = 0; j <10; j ++) {cout <s [I] [j] <"" ;}cout <endl ;}return 0 ;}

 

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.