首页 > 搜索 > DFS搜索 > HDU 3647-Tetris-计算几何-[解题报告]HOJ
2014
11-30

HDU 3647-Tetris-计算几何-[解题报告]HOJ

Tetris

问题描述 :

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.

Fate Stay Night

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:

Fate Stay Night

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.

输入:

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

输出:

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

样例输入:

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

样例输出:

Yes
No

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3647

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

分析:这个题的方法跟 POJ 1020 Anniversary Cake 完全一致。具体分析参见 http://blog.csdn.net/lyy289065406/article/details/6683250

每个俄罗斯方块都是由更小的小方格拼成的, 可以用一个一维数组来记录每一列已经摞上了多少个小方格。DFS遵循底部放满原则,如果可以恰好和已存在的方块实现无缝拼接才往上放,否则回溯。

Tetris

细心一些,把情况考虑清楚即可。

 #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;
 }

参考:http://www.cnblogs.com/GBRgbr/archive/2012/08/09/2629787.html


  1. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }