俄罗斯方块问题

问题:Tetris (Russian: Тeтрис) is a puzzle video game originally designed and programmed by Alexey Pajitnov in the Soviet Union. The Tetris game is a popular use of tetrominoes, the four element special case of polyominoes.The seven kinds of tetrominoes are listed below.



We use ‘I’, ‘J’, ‘L’, ‘O’ to stand for the tetrominoes in the top row, and ‘S’, ‘T’, ‘Z’ for the ones in the bottom row.
I enjoy it a lot. But unfortunately, I am not so good at it. So I want a computer program to help me in this game. Given the shapes and falling order of some tetrominoes, and the width and height of a rectangle, the program should check out whether those tetrominoes can fully fill in that rectangle.
There are rules. First of all, you can rotate or move a tetromino, but you can’t flip it. Secondly, let’s assume the height of the screen is unlimited, so there is always enough space to rotate or move a tetromino. Thirdly, to simplify the problem, any tetromino should not be placed under other tetrominos which fell earlier than it. For example:



In the graph above, T is placed under S. If T fell earlier than S, that’s OK. But if T came latter than S, that’s not allowed.
To make it easy, you get only ten tetrominoes in the game. So the area of the rectangle to be filled in is always 40.
 

Input
The input contains no more than 1000 test cases.Each test case contains two lines which are formatted as follows.
n m
t1 t2 ...... t10
n, m are integers indicating the width and height of the rectangle. It is promised that n*m=40. The next line contains 10 characters, each indicating a tetromino. t1 is the first falling tetromino and t10 is the last.
The input is ended by n=0 and m=0
 

Output
For each test case, if those tetrominoes can fill the rectangle, print Yes in a single line, else print No.
 

Sample Input
10 4
I I L Z O J O T T J
4 10
O Z J S T O I J I S
0 0

Sample Output
Yes
No


Hint
Here is a picture illustrated the first case. It may be helpful for you to understand the problem.

 

回答:题意大概是给你十个俄罗斯方块,问你能否拼成指定长宽的矩形,方块下落的顺序是严格确定的,后下落的方块不能落在先下落的方块之下写出算法。

#include <cstdio>
#include <cstring>
#include <cstdlib>

char tet[20];   //记录方块的下降次序
int box[45];    //把方块分成小格子,记录每列有多个小格子
int m, n;

bool DFS( int cur )
{
    if ( cur == 10 ) return true;

    switch( tet[cur] )
    {
        case 'I' :
        for ( int i = 0; i < n; i++ )
        {
            if ( box[i] + 4 <= m )   //判断能不能竖着放
            {
                box[i] += 4;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 4;
            }
            if ( i + 3 < n && box[i] == box[i + 1] && box[i + 1] == box[i + 2] && box[i + 2] == box[i + 3] && box[i] + 1 <= m )    //能不能横着放
            {
                for ( int j = i; j < i + 4; j++ ) ++box[j];
                if ( DFS( cur + 1 ) ) return true;
                for ( int j = i; j < i + 4; j++ ) --box[j];
            }
        }
        break;

        case 'O':
        for ( int i = 0; i < n; i++ )
        {
            if ( i + 1 < n && box[i] == box[i + 1] && box[i] + 2 <= m )
            {
                box[i] += 2;
                box[i + 1] += 2;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 2;
                box[i + 1] -= 2;
            }
        }
        break;

        case 'L' :
        for ( int i = 0; i < n; i++ )
        {
            if ( i + 1 < n && box[i] + 3 <= m && box[i] == box[i + 1] )    //正着放L
            {
                box[i] += 3;
                box[i + 1] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 3;
                box[i + 1] -= 1;
            }

            if (i + 2 < n && box[i] + 1 == box[i + 1] && box[i + 1] == box[i + 2] && box[i] + 2 <= m && box[i + 1] + 1 <= m )    //顺时针旋转90°
            {
                box[i] += 2;
                box[i + 1] += 1;
                box[i + 2] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 2;
                box[i + 1] -= 1;
                box[i + 2] -= 1;
            }

            if (i + 1 < n && box[i] + 1 <= m && box[i + 1] + 3 <= m && box[i + 1] + 2 == box[i] )    //顺时针旋转180°
            {
                box[i] += 1;
                box[i + 1] += 3;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 1;
                box[i + 1] -= 3;
            }

            if (i + 2 < n && box[i] == box[i + 1] && box[i + 1] == box[i + 2] && box[i + 2] + 2 <= m )    //顺时针旋转270°
            {
                box[i] += 1;
                box[i + 1] += 1;
                box[i + 2] += 2;
                if ( DFS(cur + 1) ) return true;
                box[i] -= 1;
                box[i + 1] -= 1;
                box[i + 2] -= 2;
            }

        }
        break;

        case 'J' :
        for ( int i = 0; i < n; i++ )
        {
            if (i + 1 < n && box[i] == box[i + 1] && box[i + 1] + 3 <= m )         //0
            {
                box[i] += 1;
                box[i + 1] += 3;
                if ( DFS(cur + 1) ) return true;
                box[i] -= 1;
                box[i + 1] -= 3;
            }
            if (i + 2 < n && box[i] == box[i + 1] && box[i + 1] == box[i + 2] && box[i] + 2 <= m)         //90
            {
                box[i] += 2;
                box[i + 1] += 1;
                box[i + 2] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 2;
                box[i + 1] -= 1;
                box[i + 2] -= 1;
            }
            if (i + 1 < n && box[i] + 2 == box[i + 1] && box[i] + 3 <= m && box[i + 1] + 1 <= m )         //180
            {
                box[i] += 3;
                box[i + 1] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 3;
                box[i + 1] -= 1;
            }
            if (i + 2 < n && box[i] == box[i + 1] && box[i + 2] + 1 == box[i + 1] && box[i] + 1 <= m && box[i + 2] + 2 <= m)         //270
            {
                box[i] += 1;
                box[i + 1] += 1;
                box[i + 2] += 2;
                if ( DFS(cur + 1) ) return true;
                box[i] -= 1;
                box[i + 1] -= 1;
                box[i + 2] -= 2;
            }
        }
        break;

        case 'Z' :
        for ( int i = 0; i < n; i++ )
        {
            if (i + 2 < n && box[i + 2] == box[i + 1] && box[i + 1] + 1 == box[i] && box[i] + 1 <= m && box[i + 1] + 2 <= m )  //0
            {
                box[i] += 1;
                box[i + 1] += 2;
                box[i + 2] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 1;
                box[i + 1] -= 2;
                box[i + 2] -= 1;
            }
            if (i + 1 < n && box[i] + 1 == box[i + 1] && box[i] + 2 <= m && box[i + 1] + 2 <= m)   //90
            {
                box[i] += 2;
                box[i + 1] += 2;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 2;
                box[i + 1] -= 2;
            }
        }
        break;

        case 'S' :
        for ( int i = 0; i < n; i++ )
        {
            if (i + 2 < n && box[i] == box[i + 1] && box[i + 1] + 1 == box[i + 2] && box[i + 1] + 2 <= m && box[i + 2] + 1 <= m )    //0
            {
                box[i] += 1;
                box[i + 1] += 2;
                box[i + 2] += 1;
                if ( DFS(cur + 1) ) return true;
                box[i] -= 1;
                box[i + 1] -= 2;
                box[i + 2] -= 1;
            }
            if (i + 1 < n && box[i + 1] + 1 == box[i] && box[i] + 2 <= m && box[i + 1] + 2 <= m )    //90
            {
                box[i] += 2;
                box[i + 1] += 2;
                if ( DFS(cur + 1) ) return true;
                box[i] -= 2;
                box[i + 1] -= 2;
            }
        }
        break;

        case 'T' :
        for ( int i = 0; i < n; i++ )
        {
            if ( i + 2 < n && box[i] == box[i + 1] && box[i + 1] == box[i + 2] && box[i + 1] + 2 <= m ) //0
            {
                box[i] += 1;
                box[i + 1] += 2;
                box[i + 2] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 1;
                box[i + 1] -= 2;
                box[i + 2] -= 1;
            }

            if ( i + 1 < n && box[i] + 1 == box[i + 1] && box[i] + 3 <= m ) //90
            {
                box[i] += 3;
                box[i + 1] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 3;
                box[i + 1] -= 1;
            }
            if ( i + 2 < n && box[i] == box[i + 2] && box[i + 1] + 1 == box[i] && box[i + 1] + 2 <= m ) //180
            {
                box[i] += 1;
                box[i + 1] += 2;
                box[i + 2] += 1;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 1;
                box[i + 1] -= 2;
                box[i + 2] -= 1;
            }

            if ( i + 1 < n && box[i + 1] + 1 == box[i] && box[i + 1] + 3 <= m ) //270
            {
                box[i] += 1;
                box[i + 1] += 3;
                if ( DFS( cur + 1 ) ) return true;
                box[i] -= 1;
                box[i + 1] -= 3;
            }
        }
        break;
    }
    return false;
}

int main()
{
    while ( scanf( "%d%d", &n, &m ), n || m )
    {
        for ( int i = 0; i < 10; i++ )
        {
            getchar();
            tet[i] = getchar();
        }

        memset( box, 0, sizeof(box) );

        if ( DFS(0) ) puts("Yes");
        else puts("No");
    }
    return 0;
}

转载于:https://www.cnblogs.com/benchao/p/4527011.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值