FOJ FZU 2017 Hua Rong Dao【DFS+打表】

本文探讨了在特定条件下华容道问题的解决方案数量。利用深度优先搜索算法(DFS),针对不同长度的华容道(N*4矩形)计算所有可能的布局方案。文章给出了详细的算法实现代码,并通过打表法预处理出了常见情况下的答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 2107 Hua Rong Dao
Accept: 326 Submit: 717
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.

Source

题目大意:在必须放置曹操的情况下,问没有空白地方的情况下,一共会有多少种放置方式。
思路:dfs
在此以用来纪念写的第一发这么挫的DFS代码、【因为最开始的思路就好挫好挫】
打表代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int n,ans;
int vis[1000000][5][5];
int check(int output[5][5],int x,int y)
{
    if(y+1>=0&&y+1<4&&x+1>=0&&x+1<n)
    {
        if(output[x][y]==0&&output[x][y+1]==0&&output[x][y]==0&&output[x+1][y+1]==0)
        return 1;
    }
    return 0;
}
int check2(int output[5][5],int x,int y)
{
    if(x+1>=0&&x+1<n)
    {
        if(output[x][y]==0&&output[x+1][y]==0)
        return 1;
    }
    return 0;
}
int check3(int output[5][5],int x,int y)
{
    if(y+1>=0&&y+1<4)
    {
        if(output[x][y]==0&&output[x][y+1]==0)
        return 1;
    }
    return 0;
}
void dfs(int output[5][5],int flag)
{
    int oj=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(output[i][j]==0)oj=1;
        }
    }
    if(oj==0)
    {
        if(ans==0)ans=1;
        int biaoji=1;
        for(int l=0;l<ans;l++)
        {
            int cont=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<4;j++)
                {
                    if(vis[l][i][j]==output[i][j])cont++;
                }
            }
            if(cont==n*4)biaoji=0;
            if(biaoji==0)break;
        }
        if(biaoji==1)
        {
            //printf("---------------\n");
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<4;j++)
                {
                    //printf("%d ",output[i][j]);
                    vis[ans][i][j]=output[i][j];
                }
                //printf("\n");
            }
            ans++;
        }
        return ;
    }
    if(flag==0)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(check(output,i,j))
                {
                    output[i][j]=4;output[i+1][j]=4;output[i][j+1]=4;output[i+1][j+1]=4;
                    dfs(output,1);
                    output[i][j]=0;output[i+1][j]=0;output[i][j+1]=0;output[i+1][j+1]=0;
                }
            }
        }
    }
    else
    {
        for(int l=0;l<3;l++)
        {
            if(l==0)
            {
                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<4;j++)
                    {
                        if(check2(output,i,j))
                        {
                            output[i][j]=3;output[i+1][j]=3;
                            dfs(output,1);
                            output[i][j]=0;output[i+1][j]=0;
                        }
                    }
                }
            }
            if(l==1)
            {
                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<4;j++)
                    {
                        if(check3(output,i,j))
                        {
                            output[i][j]=2;output[i][j+1]=2;
                            dfs(output,1);
                            output[i][j]=0;output[i][j+1]=0;
                        }
                    }
                }
            }
            if(l==2)
            {
                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<4;j++)
                    {
                        if(output[i][j]==0)
                        {
                            output[i][j]=1;
                            dfs(output,1);
                            output[i][j]=0;
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d",&n);
        int output[5][5];
        memset(output,0,sizeof(output));
        memset(vis,0,sizeof(vis));
        dfs(output,0);
        printf("%d\n",ans-1);
    }
}

AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int n,ans;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        if(n==1)printf("0\n");
        if(n==2)printf("18\n");
        if(n==3)printf("284\n");
        if(n==4)printf("4862\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值