FZU 2107 Hua Rong Dao_暴力dfs

本文探讨了一个经典的计数问题,在一个n*4的矩阵中,如何通过使用特定尺寸的砖块(包括一个2*2的特殊砖块)来铺满整个区域的不同方式的数量。文章采用深度优先搜索的方法来解决这一挑战。

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

原题:

Accept: 147    Submit: 416
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

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

题意+题解:

在一个n*4的矩阵中(n<=4),用一些砖块铺满,其中必须铺一个2*2的方砖,且只能铺一个,剩下的由三种 1*2 2*1 1*1的砖铺完,求一共有多少种铺设方案。

可以用状压做,不过最多就4*4,比赛的时候想直接数出来但是有数重的导致没做出来。但只状压要得用三进制表示砖块铺设确实有点过于繁琐了,我估计我也下不出来。但是后来看题解发现可以直接用搜索暴力数出来,也比较直观好想。

由于曹操的2*2砖比较特殊,就先枚举每一种曹操的位置。然后在剩下的图中一块一块的遍历,并尝试在这块中放那三种砖都试一次,如果能放,就dfs搜下去,搜完后再及时把砖块的铺设清空,进行下一种的搜索。



代码:

#include<cstdio>
#include<cstring> 
#include<vector>
#include<algorithm>
using namespace std;
int vis[6][6];
int n;
int ans;
void dfs(int x,int y)//x,y表示当前搜索到的点
{
	if(y>4)
	{
		x++;y=1;
		if(x>n)
		{	
			ans++;
			return ;
		}
	}

	if(vis[x][y])dfs(x,y+1);

	if(!(vis[x][y]))
	{
		vis[x][y]=1;
		dfs(x,y+1);
		vis[x][y]=0;
	}
	if(!vis[x][y] && y+1<=4 && !vis[x][y+1])
	{
		vis[x][y]=vis[x][y+1]=1;
		dfs(x,y+2);
		vis[x][y]=vis[x][y+1]=0;
	}
	if(!vis[x][y] && x+1<=n && !vis[x+1][y])
	{
		vis[x][y]=vis[x+1][y]=1;
		dfs(x,y+1);
		vis[x][y]=vis[x+1][y]=0;
	}
	return ;
}
int main()
{
	int t,i,j;
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d",&n);
		ans=0;
		//暴力枚举曹操在的位置
		for ( i = 1; i <= n-1 ; i++)
		{
			for ( j = 1; j <= 3 ; j++)
			{
				memset(vis,0,sizeof(vis));
				vis[i][j]=vis[i+1][j]=vis[i][j+1]=vis[i+1][j+1]=1;
				dfs(1,1);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值