DFS FZU 2107

Problem 2107 Hua Rong Dao

Accept: 247    Submit: 555

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

212

Sample Output

018

Hint

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

Source

“高教社杯”第三届福建省大学生程序设计竞赛

//用搜索来记录来统计有多少种情况
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 6
int vis[MAXN][MAXN];
int n,m;
int flag;
int ans;

bool f(int x,int y){
    if(x>=n || x<0 || y>=4 || y<0){
        return false;
    }
    return true;
}

void DFS(int con){
    int i,j;
    if(flag==1 && con==n*m){
        ans++;
        //flag = 0;
        return ;
    }
    if(con > n*m){
        return ;
    }
    for(int i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(vis[i][j]==0&&vis[i][j+1]==0&&vis[i+1][j]==0&&vis[i+1][j+1]==0&&f(i,j+1)==true&&f(i+1,j)==true&&f(i+1,j+1)==true && flag==0){
                vis[i][j] = 1;
                flag = 1;
                vis[i][j+1] = 1;
                vis[i+1][j] = 1;
                vis[i+1][j+1] = 1;
                DFS(con+4);
                flag = 0;
                vis[i][j] = 0;
                vis[i+1][j] = 0;
                vis[i][j+1] = 0;
                vis[i+1][j+1] = 0;
            }
            if(vis[i][j]==0&&vis[i][j+1]==0&&f(i,j+1)==true){
                vis[i][j+1] = 1;
                vis[i][j] = 1;
                DFS(con+2);
                vis[i][j+1] = 0;
                vis[i][j] = 0;
            }
            if(vis[i][j]==0&&vis[i+1][j]==0&&f(i+1,j)==true){
                vis[i][j] = 1;
                vis[i+1][j] = 1;
                DFS(con+2);
                vis[i][j] = 0;
                vis[i+1][j] = 0;
            }
            if(vis[i][j] == 0){
                vis[i][j] = 1;
                DFS(con+1);
                vis[i][j] = 0;
                return ;
            }
        }
    }

    //return ;
}

int main(){
    int T;

    while(~scanf("%d",&T)){
        while(T--){
            scanf("%d",&n);
            m = 4;
            memset(vis,0,sizeof(vis));
            ans = 0;
            if(n<2){
                printf("0\n");
                continue;
            }
            flag = 0;
            DFS(0);
            printf("%d\n",ans);
        }
    }

    return 0;
}

时间: 2024-08-10 00:31:53

DFS FZU 2107的相关文章

ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Practice 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

FZU 2107 Hua Rong Dao DFS

Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u FZU 2107 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

FZU 2107 Hua Rong Dao 递归回溯

点击打开链接 Problem 2107 Hua Rong Dao Accept: 254    Submit: 591 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 nar

FZU 2107 Hua Rong Dao

dfs暴力回溯,这个代码是我修改以后的,里面的go相当简洁,以前的暴力手打太麻烦,我也来点技术含量.. #include<iostream> #include<cstring> #include<cstdio> using namespace std; #define m 4 int caocao[4][2] = {0,0,0,1,1,0,1,1}; int go[3][3][2] = {{{0,1},{0,0}},{{1,0},{0,0}},{{0,0}}}; int

FZU Problem 2107 Hua Rong Dao (打表 dfs啊)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2107 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 a

ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this boar

FZU Problem 2112 Tickets (dfs 欧拉图啊)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2112 Problem Description You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction between the 2 different cities printed on the ticket.

FZU 2108(dfs模拟,大数取余)

K Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2108 Description Given one non-negative integer A and one positive integer B, it’s very easy for us to calculate A Mod B. Here A Mod B means the

FZU 2176 easy problem (DFS序+树状数组)

对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开始结束两个点标记一下就可以,用后缀数组求前缀和就可知道每个点的值. 这道题虽然很麻烦(dep[y]-dep[x])%k .但是注意到K很小(1<=k<=5),可以维护k个树状数组. 提交时编译器选GUN C++迷之RE...换Visual C++ #include <cstdio> #