FZU2107:Hua Rong Dao(DFS)

 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.

因为n最大为4,暴力搜索一下吧

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int d[4][2] = { {0, 0}, {0, 1}, {1, 0}, {1, 1} };
int dir[3][3][2] = { { {0, 1}, {0, 0} }, { {1, 0}, {0, 0} }, { {0, 0} } };
int cnt[3] = {2, 2, 1};
int r, v[5][5], tmp;
const int c = 4;
bool ok(int k, int x, int y)
{
    for (int i = 0; i< cnt[k]; i++)
    {
        int p = x + dir[k][i][0], q = y + dir[k][i][1];
        if (p<= 0 || p >r) return false;
        if (q<= 0 || q >c) return false;
        if (v[p][q]) return false;
    }
    return true;
}
void clear(int k, int x, int y, int t)
{
    for (int i = 0; i< cnt[k]; i++)
    {
        int p = x + dir[k][i][0], q = y + dir[k][i][1];
        v[p][q] = t;
    }
}
void dfs(int x, int y)
{
    if (y >c)	x = x + 1, y = 1;
    if (x == r + 1)
    {
        tmp++;
        return;
    }
    if (v[x][y]) dfs(x, y + 1);
    for (int i = 0; i< 3; i++)
    {
        if (ok(i, x, y))
        {
            clear(i, x, y, 1);
            dfs(x, y + 1);
            clear(i, x, y, 0);
        }
    }
}
int find(int x, int y)
{
    memset(v, 0, sizeof(v));
    for (int i = 0; i< 4; i++)
        v[x + d[i][0]][y + d[i][1]] = 1;
    tmp = 0;
    dfs(1,  1);
    return tmp;
}
int solve()
{
    int ans = 0;
    for (int i = 1; i< r; i++)
    {
        for (int j = 1; j< c; j++)
        {
            ans += find(i, j);
        }
    }
    return ans;
}
int main ()
{
    int t[10];
    for (r = 1; r<= 4; r++)
    {
        t[r] = solve();
    }
    int cas, n;
    scanf("%d", &cas);
    while (cas--)
    {
        scanf("%d", &n);
        printf("%d\n", t[n]);
    }
    return 0;
}
时间: 2024-11-03 22:09:34

FZU2107:Hua Rong Dao(DFS)的相关文章

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

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

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 aisl

最后的日子-训练2

A - Solve equation Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u                                   Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D

&quot;高教社杯&quot;第三届福建省大学生程序设计竞赛

 A.Problem 2102 Solve equation Accept: 1032    Submit: 2471 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-

42-2 mysql备份与恢复

02 mysql备份与恢复 实战:percona-xtrabackup备份还原数据库 原服务器:source 192.168.1.133 CentOS7.2  备份服务器:restore 192.168.1.132 CentOS7.2 1.使用indobackupex进行完全备份 [[email protected] ~]# yum install percona-xtrabackup-2.3.2-1.el7.x86_64.rpm [[email protected] ~]# rpm -ql p