国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU1045-行列聯通快最大匹配

2019-11-08 19:25:15
字體:
來源:轉載
供稿:網友

Fire Net

Time Limit: 2000/1000 MS (java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11241    Accepted Submission(s): 6709PRoblem DescriptionSuppose 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.  InputThe 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.  OutputFor 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

題目大意:

                  給你一個n*n的矩陣,其中有一些點放置了障礙,然后問你最多放置多少的子使得每行每列有且只有一個除非中間有障礙

題目思路:

                  首先我們考慮最大匹配,但是中間有障礙也可以放,所以我們想到分成聯通快,即某一行或某一列里最大相連通的快,然后進行編號,最后其實我們可以發現如果某一行出現了一個障礙物使得改行增加了一個聯通快,實際上就相當于增加了一行,然后我們枚舉所有點如果改點是空白點,就以改點的行列聯通編號連邊,最后進行最大匹配就是我們要求的答案

AC代碼:

#include<cstring>#include<cstdio>const int maxn = 20;int link[maxn],a[maxn],b[maxn];bool vis[maxn],mp[maxn][maxn];int n,cnt,cnt1,flag,flag1;bool dfs(int u){    for(int i=1;i<=cnt1;i++){        if(!vis[i]&&mp[u][i]){            vis[i]=true;            if(link[i]==-1||dfs(link[i])){                link[i]=u;                return true;            }        }    }    return false;}int main(){    while(scanf("%d",&n),n){        memset(mp,false,sizeof(mp));        memset(link,-1,sizeof(link));        char str[5][5];        for(int i=0;i<n;i++)scanf("%s",str[i]);        cnt=cnt1 = 1,flag=flag1=0;        for(int i=0;i<n*n;i++){            int h = i/n,l=i%n;            if(str[h][l]=='.')a[i]=cnt,flag=1;    //a[i]記錄改點的行聯通編號            if(l==n-1||str[h][l]!='.') {if(flag)cnt++,flag=0;}     //遇到障礙或一行的結束            if(str[l][h]=='.')b[l*n+h]=cnt1,flag1=1;           //b[i]記錄改點的列聯通編號            if(l==n-1||str[l][h]!='.') {if(flag1)cnt1++,flag1=0;}   //遇到障礙或一列的結束        }        for(int i=0;i<n*n;i++)if(str[i/n][i%n]=='.')mp[a[i]][b[i]]=true;  //改點的行聯通編號和列聯通編號連邊        int ans = 0;        for(int i=1;i<=cnt;i++){            memset(vis,false,sizeof(vis));            if(dfs(i))ans++;        }        printf("%d/n",ans);    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 交城县| 信丰县| 荃湾区| 玉树县| 玉屏| 洛隆县| 兰溪市| 凤冈县| 吴江市| 竹山县| 新郑市| 蓬莱市| 红桥区| 娄烦县| 西吉县| 钟祥市| 铜山县| 平遥县| 石渠县| 镇康县| 左权县| 类乌齐县| 海林市| 江口县| 奉节县| 察隅县| 尼玛县| 乌鲁木齐县| 兰州市| 康乐县| 镇远县| 武宁县| 元氏县| 洛隆县| 胶南市| 南华县| 兰坪| 宁化县| 宁化县| 玉环县| 靖宇县|