hdu 5374 Tetris(模拟俄罗斯方块)

题意:

俄罗斯方块想必大家都玩过,现在题目给出了3种俄罗斯方块。
O形,I形,L形
以及5种操作,w选择,a左移一位,d右移以为,w下移一位,p什么都不做。然后又给出了方块出现的序列,要求按所给出的操作能消除多少行。

解析:

DRAW数组表示已 (x,y) 为左下角,能在图上画出的形状。
然后模拟每个每个方块,直到当前方块不能动位置,在二维数组中标记方块,然后接下来选择下一个方块继续操作。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int SIZE[3] = {1, 2, 4};
int DRAW[3][4][4][2] = {
    {
        { {0, 0}, {0, 1}, {1, 0}, {1, 1} }, {}, {}, {}
    },
    {
        { {0, 0}, {0, 1}, {0, 2}, {0, 3} },
        { {0, 0}, {1, 0}, {2, 0}, {3, 0} },
        {}, {}
    },
    {
        { {0, 0}, {0, 1}, {1, 0}, {2, 0} },
        { {0, 0}, {0, 1}, {0, 2}, {1, 2} },
        { {0, 1}, {1, 1}, {2, 1}, {2, 0} },
        { {0, 0}, {1, 0}, {1, 1}, {1, 2} }
    }
};

int n;
char oper[1005];
int G[15][15];

void clearGrid() {
    memset(G, 0, sizeof(G));
    for(int i = 0; i < 15; i++) {
        G[0][i] = G[10][i] = -1;
        G[i][0] = -1;
    }
}

bool canMove(int x, int y, int dir[4][2]) {
    for(int i = 0; i < 4; i++) {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if(G[nx][ny])
            return false;
    }
    return true;
}

void drawGrid(int x, int y, int dir[4][2]) {
    for(int i = 0; i < 4; i++) {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        G[nx][ny] = 1;
    }
}

int removeGrid() {
    int ret = 0;
    for(int j = 1; j <= 9; j++) { //row
        bool full = true;
        for(int i = 1; i <= 9; i++) { //col
            if(!G[i][j]) {
                full = false;
                break;
            }
        }
        if(full) {
            for(int y = j; y < 12; y++) {
                for(int x = 1; x <= 9; x++) {
                    G[x][y] = G[x][y+1];
                }
            }
            ret++;
            j--;
        }
    }
    return ret;
}

int len, pos;
void play(int cur) {
    int x = 4, y = 9, state = 0;
    while(pos < len) {
        if(oper[pos] == 'w') {
            if(canMove(x, y, DRAW[cur][(state + 1) % SIZE[cur]]))
                state = (state + 1) % SIZE[cur];
        }else if(oper[pos] == 'a') {
            if(canMove(x-1, y, DRAW[cur][state]))
                x--;
        }else if(oper[pos] == 'd') {
            if(canMove(x+1, y, DRAW[cur][state]))
                x++;
        }else if(oper[pos] == 's') {
            if(canMove(x, y-1, DRAW[cur][state]))
                y--;
        }
        pos++;
        if(canMove(x, y-1, DRAW[cur][state]))
            y--;
        else break;
    }
    drawGrid(x, y, DRAW[cur][state]);
}

int main() {
    int T, cas = 1;
    scanf("%d", &T);
    while(T--) {
        clearGrid();
        scanf("%d%s", &n, oper);
        len = strlen(oper), pos = 0;
        int ans = 0, block;
        for(int i = 0; i < n; i++) {
            scanf("%d", &block);
            play(block);
            ans += removeGrid();
        }
        printf("Case %d: %d\n", cas++, ans);
    }
    return 0;
}

你可能感兴趣的:(HDU,5374)