分析:
从题目限制我们可以得到这种图中有哪些边可以存在
因为任意两个房间之间都只有一条通路,那么得到的一定是一棵树
因此题目转化成:图的生成树个数
矩阵树定理即可
tip
注意柱子是不能算结点的
注意取模,矩阵中不要有负数
一开始我把矩阵消成了对角线形,结果WA了,消成上三角性就A了
看来保险起见,求解行列式的时候还是消成上三角
#include<cstdio>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std;
const ll p=1e9;
const int N=101;
int n,m,mp[N][N],tot=0;
char s[N];
ll a[N][N];
ll gauss(int n) {
int now=1,to,opt=0;
for (int i=1;i<=n;i++) {
for (to=now;to<=n;to++)
if (a[to][i]) break;
if (to>n) return 0;
if (to!=now) {
for (int j=1;j<=n;j++)
swap(a[to][j],a[now][j]);
opt^=1;
}
for (int j=i+1;j<=n;j++) //上三角
while (a[j][i]) {
int x=a[j][i]/a[now][i];
if (x) {
for (int k=1;k<=n;k++) {
a[j][k]-=x*a[now][k];
a[j][k]=(a[j][k]%p+p)%p;
}
}
else {
for (int k=1;k<=n;k++)
swap(a[now][k],a[j][k]);
opt^=1;
}
}
now++;
}
ll ans=1;
for (int i=1;i<=n;i++) ans=((ans*a[i][i])%p+p)%p;
if (opt) ans=p-ans;
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) {
scanf("%s",s);
for (int j=0;j<m;j++)
if (s[j]=='.') mp[i][j+1]=++tot;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (mp[i][j]) {
int num=mp[i][j];
if (j<m&&mp[i][j+1]) {
int num0=mp[i][j+1];
a[num][num0]=a[num0][num]=p-1;
a[num][num]++; a[num][num]%=p;
a[num0][num0]++; a[num0][num0]%=p;
}
if (i<n&&mp[i+1][j]) {
int num0=mp[i+1][j];
a[num][num0]=a[num0][num]=p-1;
a[num][num]++; a[num][num]%=p;
a[num0][num0]++; a[num0][num0]%=p;
}
}
printf("%lld\n",gauss(tot-1));
return 0;
}