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

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

题意:

给出n(1≤n≤4)然后在一个n * 4的格子上放矩形,必须放一个2*2的(为题目背景下的曹操),然后剩余的位置要用三种矩形拼接

PS:

先枚举曹操的位置,然后用回溯的方式枚举出所有可能,计算总数。

打表代码:(系转自:http://www.shangxueba.com/jingyan/1818854.html

#include<stdio.h>
#include<string.h>
const int N = 10;
const int d[4][2] = { {0, 0}, {0, 1}, {1, 0}, {1, 1} };
const int dir[3][3][2] = { { {0, 1}, {0, 0} }, { {1, 0}, {0, 0} }, { {0, 0} } };
const int cnt[3] = {2, 2, 1};
int r, v[N][N], tmp;
const int c = 4;
bool isInsert(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 (isInsert(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;
}

代码如下:

#include <cstdio>
int main()
{
    int t;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n==1)
            puts("0");
        else if(n==2)
            puts("18");
        else if(n==3)
            puts("284");
        else if(n==4)
            puts("4862");
    }
    return 0;
}
时间: 2024-08-26 18:06:36

FZU Problem 2107 Hua Rong Dao (打表 dfs啊)的相关文章

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

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

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

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 a

FZU Problem 2171 防守阵地 II (线段树,区间更新)

 Problem 2171 防守阵地 II Accept: 143    Submit: 565Time Limit: 3000 mSec    Memory Limit : 32768 KB  Problem Description 部队中总共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M个需要防守的地点,指挥部将选择M个士兵依次进入指定地点进行防守任务,获得的参考指数即为M个士兵的能力之和.随着时间的推移,指挥部将下达Q个指令来替换M个进行防守的士兵们,每个参加完防守

FZU Problem 2034 Password table (简单模拟题)

这种简单题做了好长时间,我是不是有点逗? 地址:http://acm.fzu.edu.cn/problem.php?pid=2034 不解释了,自己看吧,练手的好题 上个代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio.h> #include <string.h> #include <stdlib.h>

FZU Problem 2238 Daxia &amp; Wzc&#39;s problem

Daxia在2016年5月期间去瑞士度蜜月,顺便拜访了Wzc,Wzc给他出了一个问题: Wzc给Daxia等差数列A(0),告诉Daxia首项a和公差d; 首先让Daxia求出数列A(0)前n项和,得到新数列A(1); 然后让Daxia求出数列A(1)前n项和,得到新数列A(2); 接着让Daxia求出数列A(2)前n项和,得到新数列A(3); 规律题,首先写出 a.a+d.a+2d.a+3d...这个容易写出 下面一行也容易写出:a.2a+d.3a+3d.... 再下一行,确实难写,但是通过上

FZU Problem 2168 防守阵地 I

http://acm.fzu.edu.cn/problem.php?pid=2168 题目大意: 给定n个数和m,要求从n个数中选择连续的m个,使得a[i]*1+a[i+1]*2+--a[i+m]*m最大 思路: 常规思路是以每个数开始,枚举m个,但是这样会TLE. 可以有O(n)的算法. 例如样例的 n=5 m=3 五个数分别为 2 1 3 1 4 有三种连续的三个数 2 * 1 + 1 * 2 + 3* 3 = 13 1 * 1 + 3 * 2 + 1 * 3= 10 3 * 1 + 1 *