2014
11-30

# 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.

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.

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.



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


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