uvalive 6525——Attacking rooks(二分图匹配,好题!)

22 篇文章 0 订阅

Chess inspired problems are a common source of exercises in algorithms classes. Starting with the wellknown 8-queens problem, several generalizations and variations were made. One of them is the N-rooksproblem, which consists of placing N rooks in an N by N chessboard in such a way that they do notattack each other.

Professor Anand presented the N-rooks problem to his students. Since rooks only attack each otherwhen they share a row or column, they soon discovered that the problem can be easily solved by placingthe rooks along a main diagonal of the board. So, the professor decided to complicate the problem byadding some pawns to the board. In a board with pawns, two rooks attack each other if and only ifthey share a row or column and there is no pawn placed between them. Besides, pawns occupy somesquares, 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 maximumnumber of rooks that can be placed on empty squares such that no two of them attack each other.

Input

The input file contains several test cases, each of them as described below.
The first 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 characteris either ‘.’ (dot) or the uppercase letter ‘X’, indicating respectively an empty square or a squarecontaining a pawn.

Output

For each test case, output a line with an integer representing the maximum number of rooks that canbe 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

750 



插个好的题解好了,建图非常奇妙http://blog.csdn.net/hitwhacmer1/article/details/46731219


代码如下,我用网络流写得。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int MAXV=101000;
const int INF=1000000007;
struct edge{int to,cap,rev,is_reverse;};
vector <edge> G[MAXV];
int level[MAXV],iter[MAXV];
void add_edge(int from,int to,int cap){
    G[from].push_back((edge){to,cap,(int)G[to].size(),0});
    G[to].push_back((edge){from,0,(int)G[from].size()-1,1});
}

void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=0;i<G[v].size();i++){
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f){
    if(v==t)
        return f;
    for(int &i=iter[v];i<G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){
    int flow=0;
    for(;;){
        bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}
int n;
char a[110][110];
int cnt=1;
int x[110][110],y[110][110];
const int s=0,t=100000;
void solve(){
    for(int i=0;i<MAXV;i++){
        G[i].clear();
    }
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    cnt=1;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            x[i][j]=cnt;
            if(a[i][j]=='X'||j==n-1){
                cnt++;
            }
        }
    }
    int temp=cnt;
    for(int i=1;i<cnt;i++){
        add_edge(s,i,1);
    }
    for(int j=0;j<n;j++){
        for(int i=0;i<n;i++){
            y[i][j]=cnt;
            if(a[i][j]=='X'||i==n-1){
                cnt++;
            }
        }
    }
    for(int i=temp;i<cnt;i++){
        add_edge(i,t,1);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(a[i][j]=='.'){
                add_edge(x[i][j], y[i][j], 1);
            }
        }
    }
    printf("%d\n",max_flow(s, t));
}

int main(int argc, char *argv[]) {
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%s",a[i]);
        }
        solve();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值