URAL - 1627:Join (生成树计数)

Join

题目链接https://vjudge.net/problem/URAL-1627

Description:

Businessman Petya recently bought a new house. This house has one floor with n × m square rooms, placed in rectangular lattice. Some rooms are pantries and the other ones are bedrooms. Now he wants to join all bedrooms with doors in such a way that there will be exactly one way between any pair of them. He can make doors only between neighbouring bedrooms (i.e. bedrooms having a common wall). Now he wants to count the number of different ways he can do it.

Input:

First line contains two integers n and m (1 ≤ nm ≤ 9)  — the number of lines and columns in the lattice. Next n lines contain exactly m characters representing house map, where "." means bedroom and "*" means pantry. It is guaranteed that there is at least one bedroom in the house.

Output:

Output the number of ways to join bedrooms modulo 10 9.

Sample Input:

2 2
.*
*.

Sample Output:

0

题意:

给出一个n*m的矩阵,然后"*"表示障碍物。现在可以在两个"."之间搭桥,问有多少种方式将所有的"."连通。

 

题解:

直接给所有能够搭桥的点建边,然后就是个生成树计数问题了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 105,MOD = 1e9;
char mp[N][N];
int g[N][N],num[N][N];
ll b[N][N];
int tot=0;
ll Det(int n){
    int i,j,k;
    ll ret = 1;
    if(n==0) return 0;
    for(i=2;i<=n;i++){
        for(j = i+1;j <= n;j++){
            while(b[j][i]){
                ll tmp=b[i][i]/b[j][i];//不存在除不尽的情况
                for(k = i;k <= n;k++)
                    b[i][k] = ((b[i][k] - tmp*b[j][k])%MOD+MOD)%MOD;
                for(k=i;k<=n;k++)
                    swap(b[i][k],b[j][k]);
                ret = -ret;
            }
        }
        if(!b[i][i]) return 0;
        ret = ret * b[i][i]%MOD;
        if(ret<0) ret+=MOD;
    }
    if(ret < 0) ret += MOD;
    return ret;
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",mp[i]+1);
        for(int j=1;j<=m;j++){
            if(mp[i][j]=='.') num[i][j]=++tot;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(mp[i][j]=='*') continue ;
            if(j>1&&mp[i][j-1]=='.') g[num[i][j-1]][num[i][j]]=1;
            if(i<n&&mp[i+1][j]=='.') g[num[i+1][j]][num[i][j]]=1;
        }
    }
    for(int i=1;i<=tot;i++){
        for(int j=1;j<=tot;j++){
            if(g[i][j]){
                b[i][i]++;b[j][j]++;
                b[i][j]=b[j][i]=-1;
            }
        }
    }
    cout<<Det(tot);
    return 0;
}

 

转载于:https://www.cnblogs.com/heyuhhh/p/10392763.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值