吉利矩阵(DFS)

所有元素为非负整数,且各行各列的元素和都等于 7 的 3×3 方阵称为 “吉利矩阵”,因为这样的矩阵一共有 666 种。

本题就请你统计一下,各行各列的元素和都等于 5 的 3×3 方阵一共有多少种?

思路:统计方法数,且数据不大,所以可以考虑DFS

细节:if(x==n-1&&sy[y]+i

其中x表示某一行,y表示某列

#include
using namespace std;
#define int long long
const int N=9;
int ans=0;
int s,n;
int sx[N];
int sy[N];
void dfs(int j)
{
	if(n*n==j)
	{
		ans+=1;
		return;
	}
	for(int i=0;i<=s;i++)
	{
		int x,y;
		x=j/n;
		y=j%n;
		if(sx[x]+i>s||sy[y]+i>s) continue;
		if(x==n-1&&sy[y]+i>s>>n;
	dfs(0);
	cout<

 

你可能感兴趣的:(矩阵,算法,线性代数)