HDU 5374 模拟俄罗斯方块

模拟俄罗斯方块游戏

完全按照俄罗斯方块的规则来做

注意规则即可:

1:每种图形开始出现时绿点均在(4,9)位置

2:先做变换,再下降一格

3:若碰到操作无法被执行的则不执行,依次进行下个操作

#include "stdio.h"
#include "string.h"

struct Type
{
    int a,b,x,y;
}type;
char str[1010];
int n,a[1010],ans,flag,mark,now;
int mp[20][20];

void tianchong(int op) // 将方块所在占的位置消除或者填充
{
    if (type.a==0)
    {
        mp[type.x][type.y]=op;
        mp[type.x+1][type.y]=op;
        mp[type.x][type.y+1]=op;
        mp[type.x+1][type.y+1]=op;
    }

    if (type.a==1)
    {
        if (type.b==1)
        {
        mp[type.x][type.y]=op;
        mp[type.x][type.y+1]=op;
        mp[type.x][type.y+2]=op;
        mp[type.x][type.y+3]=op;
        }
        else
        {
        mp[type.x][type.y]=op;
        mp[type.x+1][type.y]=op;
        mp[type.x+2][type.y]=op;
        mp[type.x+3][type.y]=op;
        }
    }

    if (type.a==2)
    {
        if (type.b==1)
            mp[type.x][type.y]=mp[type.x][type.y+1]=mp[type.x+1][type.y]=mp[type.x+2][type.y]=op;
        if (type.b==2)
            mp[type.x][type.y]=mp[type.x][type.y+1]=mp[type.x][type.y+2]=mp[type.x+1][type.y+2]=op;
        if (type.b==3)
            mp[type.x][type.y+1]=mp[type.x+1][type.y+1]=mp[type.x+2][type.y+1]=mp[type.x+2][type.y]=op;
        if (type.b==4)
            mp[type.x][type.y]=mp[type.x+1][type.y]=mp[type.x+1][type.y+1]=mp[type.x+1][type.y+2]=op;
    }

}

void change()
{
    if (type.a==0) return ;

    if (type.a==1)
    {
        if (type.b==1)
        {
            if (mp[type.x+1][type.y]==0 && mp[type.x+2][type.y]==0 && mp[type.x+3][type.y]==0)
            {
                tianchong(0);
                type.b=2;
                tianchong(1);
            }
        }
        else
        {
            if (mp[type.x][type.y+1]==0 && mp[type.x][type.y+2]==0 && mp[type.x][type.y+3]==0)
            {
                tianchong(0);
                type.b=1;
                tianchong(1);
            }
        }
    }

    if (type.a==2)
    {
        if (type.b==1)
        {
            if (mp[type.x][type.y+2]==0 && mp[type.x+1][type.y+2]==0)
            {
                tianchong(0);
                type.b=2;
                tianchong(1);
            }
        }
        else
        if (type.b==2)
        {
            if (mp[type.x+1][type.y+1]==0 && mp[type.x+2][type.y+1]==0 && mp[type.x+2][type.y]==0)
            {
                tianchong(0);
                type.b=3;
                tianchong(1);
            }
        }
        else
        if (type.b==3)
        {
            if (mp[type.x][type.y]==0 && mp[type.x+1][type.y]==0 && mp[type.x+1][type.y+2]==0)
            {
                tianchong(0);
                type.b=4;
                tianchong(1);
            }
        }
        else
        if (type.b==4)
        {
            if (mp[type.x][type.y+1]==0 && mp[type.x+2][type.y]==0)
            {
                tianchong(0);
                type.b=1;
                tianchong(1);

            }
        }
    }
}

void left()
{
    if (type.a==0)
    {
        if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1 ) return ;
        tianchong(0);
        type.x--;
        tianchong(1);
    }
    if (type.a==1)
    {
        if (type.b==1)
        {
            if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1 || mp[type.x-1][type.y+2]==1 || mp[type.x-1][type.y+3]==1) return ;
            tianchong(0);
            type.x--;
            tianchong(1);
        }
        else
        {
            if (type.x==1 || mp[type.x-1][type.y]==1) return ;
            tianchong(0);
            type.x--;
            tianchong(1);
        }
    }
    if (type.a==2)
    {
        if (type.b==1)
        {
            if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1) return ;
            tianchong(0);
            type.x--;
            tianchong(1);
        }
        if (type.b==2)
        {
            if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x-1][type.y+1]==1 || mp[type.x-1][type.y+2]==1) return ;
            tianchong(0);
            type.x--;
            tianchong(1);
        }
        if (type.b==3)
        {
            if (type.x==1 || mp[type.x-1][type.y+1]==1 || mp[type.x+1][type.y]==1) return ;
            tianchong(0);
            type.x--;
            tianchong(1);
        }
        if (type.b==4)
        {
            if (type.x==1 || mp[type.x-1][type.y]==1 || mp[type.x][type.y+1]==1 || mp[type.x][type.y+2]==1) return ;
            tianchong (0);
            type.x--;
            tianchong(1);
        }
    }
}

void right()
{
    if (type.a==0)
    {
        if (type.x==8 || mp[type.x+2][type.y]==1 || mp[type.x+2][type.y+1]==1) return ;
        tianchong(0);
        type.x++;
        tianchong(1);
    }

    if (type.a==1)
    {
        if (type.b==1)
        {
            if (type.x==9 || mp[type.x+1][type.y]==1 || mp[type.x+1][type.y+1]==1 || mp[type.x+1][type.y+2]==1 || mp[type.x+1][type.y+3]==1) return ;
            tianchong(0);
            type.x++;
            tianchong(1);
        }
        else
        {
            if (type.x==6 || mp[type.x+4][type.y]==1) return ;
            tianchong(0);
            type.x++;
            tianchong(1);
        }
    }

    if (type.a==2)
    {
        if (type.b==1)
        {
            if (type.x==7 || mp[type.x+1][type.y+1]==1 || mp[type.x+3][type.y]==1) return ;
            tianchong(0);
            type.x++;
            tianchong(1);
        }
        if (type.b==2)
        {
            if (type.x==8 || mp[type.x+1][type.y]==1 || mp[type.x+1][type.y+1]==1 || mp[type.x+2][type.y+2]==1) return ;
            tianchong(0);
            type.x++;
            tianchong(1);
        }
        if (type.b==3)
        {
            if (type.x==7 || mp[type.x+3][type.y]==1 || mp[type.x+3][type.y+1]==1) return ;
            tianchong(0);
            type.x++;
            tianchong(1);
        }
        if (type.b==4)
        {
            if (type.x==8 || mp[type.x+2][type.y]==1 || mp[type.x+2][type.y+1]==1 || mp[type.x+2][type.y+2]==1) return ;
            tianchong(0);
            type.x++;
            tianchong(1);
        }
    }
}

void down()
{
    if (type.y==1) {flag=1; return ;}

    if (type.a==0 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1) ) { flag=1; return ;}

    if (type.a==1)
    {
        if (type.b==1 && mp[type.x][type.y-1]==1) {flag=1; return ;}
        if (type.b==2 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1 || mp[type.x+2][type.y-1]==1 || mp[type.x+3][type.y-1]==1)  )
        {
            flag=1;
            return ;
        }
    }

    if (type.a==2)
    {
        if (type.b==1 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1 || mp[type.x+2][type.y-1]==1 ) )
        {
            flag=1;
            return ;
        }
        if (type.b==2 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y+1]==1)  )
        {
            flag=1;
            return ;
        }
        if (type.b==3 && (mp[type.x][type.y]==1 || mp[type.x+1][type.y]==1 || mp[type.x+2][type.y-1]) )
        {
            flag=1;
            return ;
        }
        if (type.b==4 && (mp[type.x][type.y-1]==1 || mp[type.x+1][type.y-1]==1))
        {
            flag=1;
            return ;
        }
    }
    tianchong(0);
    type.y--;
    tianchong(1);
}

void check_xiao()
{

    int i,j,xiao,k;
    i=0;
    while (i<12)
    {
        i++;
        xiao=1;
        for (j=1;j<=9;j++)
            if (mp[j][i]==0) {xiao=0; break;}
        if (xiao==1)
        {
            ans++;
            for (j=i+1;j<=12;j++)
                for (k=1;k<=9;k++)
                mp[k][j-1]=mp[k][j];
            i--;
        }
    }
}

void bfs()
{
    int now,i,j,len;
    memset(mp,0,sizeof(mp));
    ans=0;
    now=0; // 记录当前处理到哪个动作
    len=strlen(str);
    mark=1; // 记录当前处理到哪个方块
    type.a=a[1]; type.b=1; type.x=4; type.y=9; // a记录形状,b记录角度,x,y记录绿点坐标;
    tianchong(1);

    while (now<len)
    {
        flag=0;
        if (str[now]=='w') change(); // 变换
        if (str[now]=='a') left(); // 左移
        if (str[now]=='d') right(); // 右移
        if (str[now]=='s') down(); // 下降
       /* for (i=12;i>=1;i--)
        {
            for (j=1;j<=9;j++)
            printf("%d",mp[j][i]);
            printf("\n");
        }
        printf("\n");*/

        now++;

        if (flag==1)
        {
            check_xiao(); // 检查是否有可以消除的行
            mark++;
            type.a=a[mark]; type.b=1; type.x=4; type.y=9;
        }
        else
        {
            down();
            if (flag==1)
            {
                check_xiao();
                mark++;
                type.a=a[mark]; type.b=1; type.x=4; type.y=9;
            }
        }
    }
}
int main()
{
    int t,ii,i;
    scanf("%d",&t);
    for (ii=1;ii<=t;ii++)
    {
        scanf("%d",&n);
        scanf("%s",str);
        for (i=1;i<=n;i++)
            scanf("%d",&a[i]);

        bfs();

        printf("Case %d: %d\n",ii,ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 12:15:37

HDU 5374 模拟俄罗斯方块的相关文章

hdu 5374 Tetris(模拟)

题目链接:hdu 5374 Tetris 模拟,每次进行操作时判断操作是否合法,合法才执行,否则跳过.每次一个token落地,判断一下是否有消除整行. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; /******* Token **********/ const int C[3] = {1, 2, 4}; const int T[3][4][4][2] = {

HDU 4930 模拟

Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 266    Accepted Submission(s): 87 Problem Description Fighting the Landlords is a card game which has been a heat for yea

HDU 5374 Tetris (2015年多校比赛第7场)

1.题目描写叙述:点击打开链接 2.解题思路:本题要求模拟俄罗斯方块游戏.然而比赛时候写了好久还是没过. 后来补题发现原来是第四步的逻辑实现写错了... 题目中要求假设一整行能够消除,那么仍然运行该步.否则才回到第一步.可是我的代码却是不论能否够消除,都回到第一步.. .补题时候还发现一个地方我的理解出错了.. (可能是我脑洞真的有点大).题目中说假设一整行能够消除,那么它上面的方格要下落.我的理解是下落的方格要一直降落到第一行或者某个支撑物上,最后发现依照这样写,例子都算不正确==.调试了一下

hdu 5374 Tetris 模拟俄罗斯方块

题目链接: hdu5374 题意: 俄罗斯方块游戏, 给出一个玩家的操作序列(w,a,s,d,p), 和依次出现的n个方块的形状, 问最终玩家消除了几行. 解题思路: 大模拟,想清楚就好写了, 用一个数组保存 所有形状 所有状态下 四个点的相对位置 每进行一次操作时,判断是否合法(越界,重叠) 下落时判断是否重叠 ,如果重叠则不动 将方块加入地图,消行,换下一个方块继续 代码: #include<iostream> #include<cstdio> #include<cstr

hdu 1022 模拟栈

其实就是模拟一下栈啦. 1 #include <iostream> 2 using namespace std; 3 4 const int N = 10; 5 char o1[N]; 6 char o2[N]; 7 char s[N]; 8 int ans[N * 2]; 9 10 int main () 11 { 12 int n; 13 while ( cin >> n ) 14 { 15 cin >> o1 >> o2; 16 int top = 0

hdu 4054 模拟 练习十六进制输出

http://acm.hdu.edu.cn/showproblem.php?pid=4054 貌似一般区域赛都会有一道水题 这道题PE了一次  因为输出每个数其实是两个位 如果用空格补齐的话  应该用两个空格 我用了一个空格,,, 学到: 1.%x  十六进制输出  可以输出整型,字符型等等 2.%02x  保证两位 而且会输出先导0的两位 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstd

hdu 5246(模拟)

题解:直接模拟 #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 10005; long long n, m, k; long long s[N]; int main() { int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%lld%l

hdu 5386 模拟

想明白以后会发现其实就是模拟... 因为每次只能给一整行或者一整列赋值,所以目标矩阵一开始一定有一行或者一列上数字都是相同的,然后找到对应的操作,把那一行或者那一列标记为访问过.然后新的矩阵也一定能找到一行或者一列数字都相同,再找到相应的操作,标记那一行或者那一列,依次类推,然后没有用到的操作随便找个顺序就好了.其实初始矩阵并没有什么用...... 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio>

HDU 5071 模拟

考察英语的题 - -# 按条件模拟,一遍就行了,每个聊天对象有其价值U,数组模拟队列过程即可,若存在Top标记,则和Top标记的人聊天,否则和队列的第一个人聊天 mark记录队尾,top记录Top操作,data[i].p记录U,data[i].x记录chat数,data[i].y记录该人是否被删除 Add U:在 队尾插入价值为U的人,需要特判U人已经存在 Close U::在整个队列中查找价值为U的人,将其删除,需要特判该人不存在 Chat x:当前聊天页面的人chat+=x,特判当前队列没有