传送门
题意
寻找从起点1开始,经过所有点1次,再次回到起点1,权值总和为k次数。
思路
由于N(N<8)很小,枚举全排列即可。
AC代码
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const int maxn =10;
int a[maxn][maxn];
int b[maxn];
int n,k;
bool vis[maxn];
int ans = 0;
void dfs(int cnt){
if(cnt>n){
if(b[1]!=1) return ;
int sum = 0;
for(int i = 1; i <= n-1; i++){
sum += a[b[i]][b[i+1]];
}
sum += a[b[n]][b[1]];
if(sum==k) ans++;
return ;
}
for(int i = 1; i <= n; i++){
if(vis[i]==0){
vis[i] = 1;
b[cnt] = i;
dfs(cnt+1);
vis[i] = 0;
}
}
return ;
}
int main(void){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
cin >> n >> k;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
}
}
dfs(1);
cout<<ans<<endl;
return 0;
}