UVa live6525Attacking rooks(二分最大匹配之最大匹配)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536

Chess inspired problems are a common source of exercises in algorithms classes. Starting with the well
known 8-queens problem, several generalizations and variations were made. One of them is the N -rooks
problem, which consists of placing N rooks in an N by N chessboard in such a way that they do not
attack each other.
Professor Anand presented the N -rooks problem to his students. Since rooks only attack each other
when they share a row or column, they soon discovered that the problem can be easily solved by placing
the rooks along a main diagonal of the board. So, the professor decided to complicate the problem by
adding some pawns to the board. In a board with pawns, two rooks attack each other if and only if
they share a row or column and there is no pawn placed between them. Besides, pawns occupy some
squares, which gives an additional restriction on which squares the rooks may be placed on.
Given the size of the board and the location of the pawns, tell Professor Anand the maximum
number of rooks that can be placed on empty squares such that no two of them attack each other.
Input
The input le contains several test cases, each of them as described below.
The rst line contains an integer N (1 N 100) representing the number of rows and columns
of the board. Each of the next N lines contains a string of N characters. In the i-th of these strings,
the j -th character represents the square in the i-th row and j-th column of the board. The character
is either `.' (dot) or the uppercase letter `X', indicating respectively an empty square or a square
containing a pawn.
Output
For each test case, output a line with an integer representing the maximum number of rooks that can
be placed on the empty squares of the board without attacking each other.
Sample Input
5
X....
X....
..X..
.X...
....X
4
....
.X..
....
....
1
X
Sample Output
7
5
0

这个题也是以前做过的,当时用的暴搜。。不用说。。结果自然是该超的都超了。。超内存超时限。现在用二分匹配做真心简单啊。。从一篇文章中有这题的建图方法。。看了好长时间才发现这个建图方法跟我刚做过的一道题的建图方法本质上是一样的。(详情见我上一篇博客。。)由于上一篇博客中有建图方法也有原因,这里就不多说了。。。

这题20s的时限,第一次因为忘记初始化而TLE了好几次。。。sad。。(为什么每个题都要如此手残一番。。)后来加上了之后以19.369s的时间AC了。。。后来发现定的数组过大,光在初始化上就浪费很长时间,改成5000*5000后,时间立即来到了11秒。。。据说用前向星更快,下次再来个前向星版本的,这博文有待更新。上代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
int n, mp1[101][101], mp2[101][101], mpp[101][101], mp[4001][4001], vis[4001], link[4001], k1, k2;
int dfs(int a)
{
    int i;
    for(i=1;i<=k2;i++)
    {
        if(!vis[i]&&mp[a][i])
        {
            vis[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=a;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    int i, ans=0;
    memset(link,-1,sizeof(link));
    for(i=1;i<=k1;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int main()
{
    int i, j, a;
    char s[200];
    while(scanf("%d",&n)!=EOF)
    {
        a=0;
        memset(mp,0,sizeof(mp));
        memset(mp1,0,sizeof(mp1));
        memset(mp2,0,sizeof(mp2));
        memset(mpp,0,sizeof(mpp));
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            for(j=0;j<n;j++)
            {
                if(s[j]=='.')
                    {
                        mpp[i][j]=1;
                        a++;
                    }
            }
        }
        k1=k2=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(mpp[i][j])
                {
                    if(j==0||(mpp[i][j-1]==0&&mpp[i][j]))
                    {
                        mp1[i][j]=++k1;
                    }
                    if(j!=0&&mpp[i][j-1]&&mpp[i][j])
                        mp1[i][j]=k1;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(mpp[j][i])
                {
                    if(j==0||(mpp[j-1][i]==0&&mpp[j][i]))
                    {
                        mp2[j][i]=++k2;
                    }
                    if(j!=0&&mpp[j-1][i]&&mpp[j][i])
                        mp2[j][i]=k2;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                mp[mp1[i][j]][mp2[i][j]]=1;
            }
        }
        printf("%d\n",hungary());
    }
    return 0;
}

更快的前向星写法新鲜出炉啦!!快了100倍!!从11秒马上变成了0.119秒。。。。太爽了。。。

前后对比:



简直炫酷。。。上代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int n, mp1[101][101], mp2[101][101], mpp[101][101], cnt,  vis[4001], link[4001], k1, k2, head[10000];
struct node
{
    int u, v;
    int next;
} edge[10001];
void add(int u, int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int dfs(int u)
{
    int i;
    for(i=head[u]; i!=-1; i=edge[i].next)
    {
        if(!vis[edge[i].v])
        {
            vis[edge[i].v]=1;
            if(link[edge[i].v]==-1||dfs(link[edge[i].v]))
            {
                link[edge[i].v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungary()
{
    int i, ans=0;
    memset(link,-1,sizeof(link));
    for(i=1; i<=k1; i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int main()
{
    int i, j;
    char s[200];
    while(scanf("%d",&n)!=EOF)
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        memset(mp1,0,sizeof(mp1));
        memset(mp2,0,sizeof(mp2));
        memset(mpp,0,sizeof(mpp));
        for(i=0; i<n; i++)
        {
            scanf("%s",s);
            for(j=0; j<n; j++)
            {
                if(s[j]=='.')
                {
                    mpp[i][j]=1;
                }
            }
        }
        k1=k2=0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(mpp[i][j])
                {
                    if(j==0||(mpp[i][j-1]==0&&mpp[i][j]))
                    {
                        mp1[i][j]=++k1;
                    }
                    if(j!=0&&mpp[i][j-1]&&mpp[i][j])
                        mp1[i][j]=k1;
                }
            }
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(mpp[j][i])
                {
                    if(j==0||(mpp[j-1][i]==0&&mpp[j][i]))
                    {
                        mp2[j][i]=++k2;
                    }
                    if(j!=0&&mpp[j-1][i]&&mpp[j][i])
                        mp2[j][i]=k2;
                }
            }
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(mpp[i][j])
                    add(mp1[i][j],mp2[i][j]);
            }
        }
        printf("%d\n",hungary());
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值