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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值