Problem 2107 Hua Rong Dao
Accept: 365 Submit: 796
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Cao Cao was hunted down by thousands of enemy soldiers when he escaped from Hua Rong Dao. Assuming Hua Rong Dao is a narrow aisle (one N*4 rectangle), while Cao Cao can be regarded as one 2*2 grid. Cross general can be regarded as one 1*2 grid.Vertical general can be regarded as one 2*1 grid. Soldiers can be regarded as one 1*1 grid. Now Hua Rong Dao is full of people, no grid is empty.
There is only one Cao Cao. The number of Cross general, vertical general, and soldier is not fixed. How many ways can all the people stand?
Input
There is a single integer T (T≤4) in the first line of the test data indicating that there are T test cases.
Then for each case, only one integer N (1≤N≤4) in a single line indicates the length of Hua Rong Dao.
Output
Sample Input
Sample Output
Hint
Here are 2 possible ways for the Hua Rong Dao 2*4.
Source
“高教社杯”第三届福建省大学生程序设计竞赛
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define LEFT(x) (x<<1)
#define ysk(x) (1<<x)
#define CLEAR(s,x) (s&~(1<<x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxS= 32 ;
const int maxn= 4 ;
int ed,n,m=4;
ll dp[maxn+3][maxS+3][2];
int now;
void update(int a,int b,int num,int num2)
{
if(num2>1) return;
dp[now][CLEAR(b,m+1)][num2]+=dp[now^1][a][num];
}
void work()
{
ed=ysk(m+1)-1;
now=0;
mes(dp[now],0,ed+1);
dp[now][ed][0]=1;
for0(i,n)
{
for0(j,m)
{
now^=1;
mes(dp[now],0 ,ed+1);
for(int s=0;s<=ed;s++)
{
for(int num=0;num<=1;num++) if(dp[1^now][s][num])
{
//不铺
if( s&ysk(m) ) update(s,LEFT(s) ,num,num);
//1*1;
if( ( s&ysk(m-1) )&& (s&ysk(m) ) )update( s, LEFT(s)|1 ,num ,num );
//横铺
if(j&& !(s&1) &&( s&ysk(m-1) )&& (s&ysk(m) ) ) update(s,LEFT(s)|3,num,num );
//竖铺
if(i&& !(s&ysk(m-1)) && (s&ysk(m) ) ) update(s,LEFT(s)|1|ysk(m),num,num );
//2*2 曹操
if(i&&j&&!(s&1)&&!(s&ysk(m-1)) &&!(s&ysk(m)) )
update(s,LEFT(s)|3|ysk(m)|ysk(m+1),num,num+1 );
}
}
}
}
cout<<dp[now][ed][1]<<endl;
}
int main()
{
std::ios::sync_with_stdio(false);
int T;cin>>T;
while(T--)
{
cin>>n;
work();
}
return 0;
}