码迷,mamicode.com
首页 > 其他好文 > 详细

HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)

时间:2014-10-02 19:08:23      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:map   二分匹配   

Attacking rooks
Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB
Total submit users: 12, Accepted users: 7
Problem 13028 : No special judgement
Problem description
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 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 character is either "." (dot) or the uppercase letter "X", indicating respectively an empty square or a square containing a pawn.
Output
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
Sample input 1
5
X....
X....
..X..
.X...
....X

Sample input 2
4
....
.X..
....
....

Sample input 3
1
X
Sample Output
Sample output 1
7

Sample output 2
5

Sample output 3
0
Problem Source
ICPC Latin American Regional 2013
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
vector<int>map[10005];
int match[10005],vist[10005];
int find(int x)
{
    int len=map[x].size();
    for(int i=0;i<len;i++)
    if(vist[map[x][i]]==0)
    {
        vist[map[x][i]]=1;
        if(match[map[x][i]]==0||find(match[map[x][i]]))
        {
            match[map[x][i]]=x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int n,rn,ln,t=0,mpr[105][105],mpl[105][105];
    char c;
    while(scanf("%d",&n)>0)
    {
        for(int i=1;i<=n;i++)
        {
            getchar();
            for(int j=1;j<=n;j++)
            {
                mpr[i][j]=mpl[i][j]=0;
                scanf("%c",&c);
                if(c=='X')
                mpr[i][j]=mpl[i][j]=-1;
            }
        }
        rn=0;
        for(int i=1;i<=n;i++)//一行变多行
        for(int j=1;j<=n;j++)
        {
            while(mpr[i][j]==-1&&j<=n)j++;
            rn++;
            while(mpr[i][j]!=-1&&j<=n)
            {
                mpr[i][j]=rn; j++;
            }
        }
        ln=0;
        for(int j=1;j<=n;j++)//一列变多列
        for(int i=1;i<=n;i++)
        {
            while(mpl[i][j]==-1&&i<=n)i++;
            ln++;
            while(mpl[i][j]!=-1&&i<=n)
            {
                mpl[i][j]=ln; i++;
            }
        }
        for(int i=1;i<=rn;i++)
        map[i].clear();
         for(int i=1;i<=n;i++)//行列建图
        for(int j=1;j<=n;j++)
        if(mpr[i][j]!=-1)
        map[mpr[i][j]].push_back(mpl[i][j]);

        memset(match,0,sizeof(match));
        int ans=0;
        for(int i=1;i<=rn;i++)
        {
            for(int j=0;j<=ln;j++)
            vist[j]=0;
            ans+=find(i);
        }

        printf("%d\n",ans);
    }
}

HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)

标签:map   二分匹配   

原文地址:http://blog.csdn.net/u010372095/article/details/39737291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!