HDU5374 Tetris (2015-year School competition 7th game) Big simulation

Source: Internet
Author: User



Ideas:

Several functions are written first. Rotation, four operations to determine if merge elimination is possible

There are several details to consider in the question, such as

When nature falls to the bottom, it is not judged by the merger, but when the natural fall is illegal.

If you eliminate a row, the block above the line will drop only one line, and there is no direct drop to the bottom of the case

The game test instructions not understand well, wrong these two places.


There are some mistakes in writing, such as the right and left to move the topic coordinates to start from 1, convenient many

Move left and right, and if it is illegal to recover, the undo operation will be the exact opposite of the move operation, and the range will start at 0 because it may move to an illegal position when moving.

After the game to see the standard process, a variety of beautiful and concise code. The simulation capability has yet to be strengthened.


#include <iostream> #include <cmath> #include <algorithm> #include <cstring> #include < string> #include <cstdio> #include <vector> #define PB push_back#define Debug puts ("==================== = ");//#pragma comment (linker,"/stack:1024000000,1024000000 ") using namespace Std;typedef long long ll;int t;int map[20]     [20];  Record whole int updata[20][20];    Record the current drop box char ch[1111];int a[1111];int n;int Chong ()//Determine if there is an illegal operation {int sum = 0;    for (int i=1, i<=12; i++) for (int j=1; j<=9; J + +) if (updata[i][j]) sum++;    if (sum!=4) return 0; for (int i=1, i<=12; i++) for (int j=1; j<=9; J + +) if (updata[i][j]+map[i][j]>1) re    Turn 0; return 1;}        int zhuan (int type,int x,int y)//rotation {if (type==1) {swap (updata[x-1][y],updata[x][y+1]);        Swap (updata[x-2][y],updata[x][y+2]);        Swap (updata[x-3][y],updata[x][y+3]);  if (!chong ())//if illegal, then turn back {          Swap (updata[x-1][y],updata[x][y+1]);            Swap (updata[x-2][y],updata[x][y+2]);        Swap (updata[x-3][y],updata[x][y+3]); }} else if (type==2) {if (updata[x][y]==0)///3->4 {swap (updata[x-1][y],updata[x-2            ][y+1]);            Swap (updata[x-1][y+2],updata[x][y+1]);            Swap (updata[x][y+2],updata[x][y]);                if (!chong ()) {swap (updata[x-1][y],updata[x-2][y+1]);                Swap (updata[x-1][y+2],updata[x][y+1]);            Swap (updata[x][y+2],updata[x][y]);                }} else {if (updata[x-1][y]&&updata[x][y+1])///1->2 {                Swap (updata[x][y+1],updata[x-2][y]);                Swap (updata[x][y+2],updata[x-2][y+1]);                    if (!chong ()) {swap (updata[x][y+1],updata[x-2][y]);                Swap (updata[x][y+2],updata[x-2][y+1]); }} ELSE if (Updata[x-1][y]&&!updata[x][y+1])///2->3 {swap (updata[x][y],updata[x-1][y+1])                ;                Swap (updata[x-2][y],updata[x-1][y+2]);                Swap (updata[x-2][y+1],updata[x][y+2]);                    if (!chong ()) {swap (updata[x][y],updata[x-1][y+1]);                    Swap (updata[x-2][y],updata[x-1][y+2]);                Swap (updata[x-2][y+1],updata[x][y+2]); }} else if (!updata[x-1][y]&&updata[x][y+1])///4->1 {swap (upd                Ata[x-1][y+1],updata[x-1][y]);                Swap (updata[x-2][y+1],updata[x][y+2]);                    if (!chong ()) {swap (updata[x-1][y+1],updata[x-1][y]);                Swap (updata[x-2][y+1],updata[x][y+2]);            }}}}}int Ctrl (char lei,int type)//control up or down {if (lei== ' W ') {if (type==0)        return 0;    int xx,yy;    xx =-9999;        yy = 9999; for (int i=1, i<=12; i++) for (int j=1; j<=9; J + +) if (updata[i][j]) xx =   Max (xx,i), yy = min (yy,j);    Find the green point, with this point as the benchmark Zhuan (TYPE,XX,YY); } else if (lei== ' a ') {for (Int. I=1; i<=12; i++) for (int j=0; j<=10; J + +) if (up        DATA[I][J]) swap (updata[i][j],updata[i][j-1]); if (!chong ())//if illegal, then move back {for (int i=1; i<=12; i++) for        (int j=10; j>=0; j--) if (Updata[i][j]) swap (updata[i][j],updata[i][j+1]);                }} else if (lei== ' d ') {for (Int. I=1; i<=12; i++) for (int j=10; j>=0; j--)        if (Updata[i][j]) swap (updata[i][j],updata[i][j+1]);                    if (!chong ()) {for (Int. I=1; i<=12; i++) for (int j=0; j<=10; j + +)     if (Updata[i][j]) swap (updata[i][j],updata[i][j-1]);   }} else if (lei== ' s ') {for (Int. i=13; i>=1; i--) for (int j=1; j<=9; j + +)        if (Updata[i][j]) swap (updata[i][j],updata[i+1][j]);                    if (!chong ()) {for (Int. I=1; i<=13; i++) for (int j=1; j<=9; j + +)        if (Updata[i][j]) swap (updata[i][j],updata[i-1][j]); }} return 1;}    int Score,tmp,jj,len;int di ()//determines whether the {int go = 1 can be merged and eliminated; for (int i=1, i<=11; i++) for (int j=1; j<=9; J + +) {if (updata[i][j]&&updata[i+1][j]=            =0) {if (map[i+1][j]) go = 0;    }} for (int j=1; j<=9; J + +) {if (updata[12][j]) go = 0;    } if (go) return 0; for (int i=1, i<=12; i++) for (int j=1; j<=9; J + +) {if (Updata[i][j]) map[i][j        ] = Updata[i][j];        } while (1) {int flag = 0; int hang = 12;        for (int i=1; i<=12; i++) {int sum = 0;            if (Map[i][1]) {for (int j=1; j<=9; j + +) Sum+=map[i][j];                } if (sum==9) {//cout<< "--------\ n";                Hang = i;                flag = 1;                score++;                for (int j=1; j<=9; j + +) Map[i][j] = 0;            Break                    }} if (flag) {for (Int. i=hang-1; i>=1; i--) for (int j=1; j<=9; j + +)        if (map[i][j]&&map[i+1][j]==0) swap (map[i][j],map[i+1][j]);    } else break; } return 1;}    int main () {//Freopen ("3.out", "w", stdout);    cin>>t;    int cs = 1;        while (t--) {score = 0;        scanf ("%d%s", &n,ch);        for (int i=0; i<n; i++) scanf ("%d", &a[i]);    memset (map,0,sizeof (MAP));    memset (updata,0,sizeof (Updata));        JJ = 0;        Len=strlen (CH);            for (int i=0; i<n; i++)//falling block {tmp = A[i];            memset (updata,0,sizeof (Updata));                if (a[i]==0) {updata[4][4] = 1;                UPDATA[3][4] = 1;                UPDATA[4][5] = 1;            UPDATA[3][5] = 1;                } else if (a[i]==1) {updata[4][4] = 1;                UPDATA[3][4] = 1;                UPDATA[2][4] = 1;            UPDATA[1][4] = 1;                } else if (a[i]==2) {updata[4][4] = 1;                UPDATA[3][4] = 1;                UPDATA[4][5] = 1;            UPDATA[4][6] = 1; } for (; jj<len; jj++)//action number {if (ch[jj]!= ' P ') Ctrl (CH[JJ],                 TMP);                    if (Di ())//To determine whether to merge, eliminate {jj++;           Break     } CTRL (' s ', TMP);    Every natural Drop} printf ("Case%d:%d\n", Cs++,score); } return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU5374 Tetris (2015-year School competition 7th game) Big simulation

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.