HDU1045Fire Net(二分图匹配)

来源:互联网 发布:男内裤淘宝买家秀图 编辑:程序博客网 时间:2024/06/13 09:59

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11954    Accepted Submission(s): 7189


Problem Description
Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. 

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening. 

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets. 

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through. 

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways. 



Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration. 
 

Input
The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file. 
 

Output
For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
 

Sample Input
4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0
 

Sample Output
51524


题意:

给出一张图,图中'X'表示wall,'.'表示空地,可以放置blockhouse同一条直线上只能有一个blockhouse,除非有wall

隔开,问在给出的图中最多能放置多少个blockhous


分析:

把原始图分别按行和列缩点  
建图:横竖分区。先看每一列,同一列相连的空地同时看成一个点,显然这样的区域不能够同时放两个点。这些

点作为二分图的X部。同理在对所有的行用相同的方法缩点,作为Y部。  
  连边的条件是两个区域有相交部分(即'.'的地方)。最后求最大匹配就是答案。

#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;const int N = 8;int cnt_row, cnt_col;int row[N][N], col[N][N], r[N], c[N];char mp[N][N];bool path[N][N], vis[N];int dfs(int rr){    for(int i = 0; i < cnt_col; i++)    {        if(path[rr][i] && vis[i] == false)        {            vis[i] = true;            if(c[i] == -1 || dfs(c[i]))            {                c[i] = rr;                r[rr] = i;                return 1;            }        }    }    return 0;}int hungarian(){    int ans = 0;    memset(r, -1, sizeof(r));    memset(c, -1, sizeof(c));    for(int i = 0; i < cnt_row; i++)        if(r[i] == -1)        {            memset(vis, false, sizeof(vis));            ans += dfs(i);        }    return ans;}int main(){    int n;    while(scanf("%d", &n) && n)    {        for(int i = 0; i < n; i++)            for(int j = 0; j < n; j++)                scanf(" %c", &mp[i][j]);        memset(row, -1, sizeof(row));        memset(col, -1, sizeof(col));        cnt_row = cnt_col = 0;        for(int i = 0; i < n; i++)            for(int j = 0; j < n; j++)            {                if(mp[i][j] == '.' && row[i][j] == -1)                {                    for(int k = j; mp[i][k] == '.' && k < n; k++)                        row[i][k] = cnt_row;                    cnt_row++;                }                if(mp[i][j] == '.' && col[i][j] == -1)                {                    for(int k = i; mp[k][j] == '.' && k < n; k++)                        col[k][j] = cnt_col;                    cnt_col++;                }            }        memset(path, false, sizeof(path));        for(int i = 0; i < n; i++)            for(int j = 0; j < n; j++)                if(mp[i][j] == '.')                    path[row[i][j]][col[i][j]] = true;        printf("%d\n", hungarian());    }    return 0;}






原创粉丝点击