FZOJ Problem 2107 Hua Rong Dao

                                                                                                                                 Problem 2107 Hua Rong Dao

Accept: 401    Submit: 853
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

For each test case, print the number of ways all the people can stand in a single line.

Sample Input

2 1 2

Sample Output

0 18

Hint

Here are 2 possible ways for the Hua Rong Dao 2*4.

 
 
题意:在由方格组成的大小为N*4的矩阵内排行军队伍,曹操占四个方格且必须存在,竖向的将军占2*1的长方形矩阵,横向将军占1*2的长方形矩阵,其余小兵占一单位方格,现在问总共能找到多少种行军队伍排列的方案,每种方案都要求将N*4的矩阵填充满。
思路:深度优先搜索,直至所有的小方块空间都被队伍填充。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
int N;
int vis[10][10];
bool flag = 0;
int num ;
bool judge(int x,int y) {
    if (x >= 0 && x < N&&y >= 0 && y < 4&&!vis[x][y]) {//!!!
        return true;
    }
    return false;
}

void dfs(int count) {
    if (count == N * 4 && flag) { num++; return; }
    if (count >= N * 4)return;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 4; j++) {
            if (judge(i, j) && judge(i + 1, j) && judge(i, j + 1) && judge(i + 1, j + 1) && !flag) {
                flag = 1;
                vis[i][j] = vis[i + 1][j] = vis[i][j + 1] = vis[i + 1][j + 1] = 1;
                dfs(count + 4);
                flag = 0;
                vis[i][j] = vis[i + 1][j] = vis[i][j + 1] = vis[i + 1][j + 1] = 0;
            }
            if (judge(i, j) && judge(i + 1, j)) {
                vis[i][j] = vis[i + 1][j] = 1;
                dfs(count + 2);
                vis[i][j] = vis[i + 1][j] = 0;
            }
            if (judge(i, j) && judge(i, j + 1)) {
                vis[i][j] = vis[i][j + 1] = 1;
                dfs(count + 2);
                vis[i][j] = vis[i][j + 1] = 0;
            }
            if (judge(i, j)) {
                vis[i][j] = 1;
                dfs(count + 1);
                vis[i][j] = 0;
                return;//!!!!
            }
        }
    }
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        scanf("%d",&N);
        num =flag= 0;
        memset(vis,0,sizeof(vis));
        dfs(0);
        printf("%d\n",num);
    }
    return 0;
}

 

 

posted on 2017-05-02 14:14  ZefengYao  阅读(186)  评论(0编辑  收藏  举报

导航