hdu 1045 Fire Net 二分匹配
来源:互联网 发布:一直很安静 知乎 编辑:程序博客网 时间:2024/05/15 11:51
题目链接:
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6890 Accepted Submission(s): 3894
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..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
5
1
5
2
4
题意是
.表示空地可以放炮台 X表示墙。
在 . 处放炮台。X会挡住炮台攻击。 问最多可以放多少炮台,而且使炮台不会互相攻击。
如果没有X 阻挡的限制,就是普通的二分匹配题目, 一排为一个点。
但是有X限制的话要先分别 横向再竖向扫描。X隔开的 每部分都为一个点。然后就可以按二分匹配来做了。
#include<stdio.h>#include<string.h>#include<map>using namespace std;#define N 150int visit[N];int mark[N];int match[N][N];int n,m,k;map<int,int>X,Y;int dfs(int x){ int i; for(i=1;i<=m;i++)//对左边的节点x与右边的节点进行逐一检查 { if(!visit[i]&&match[x][i]) { visit[i]=1;//标记检查过的点 if(mark[i]==-1||dfs(mark[i])) //mark如果没男的,就直接给个老公,如果已经有老公,搜他老公有没有喜欢的,有的话,该女配新男;;; {//|| 前面过了 后面不运行;; mark[i]=x;//修改匹配关系 return 1; } } } return 0;}int hungary (){ memset(mark,-1,sizeof(mark));int max=0,j; for(j=1;j<=n;j++)//对做部分顶点逐个进行遍历{memset(visit,0,sizeof(visit));if(dfs(j))max++;}return max;}char mp[8][8];int main(){ int max,nn; while(scanf ("%d",&nn),nn) //k 个配 {X.clear();Y.clear();m=n=0;k=0; memset(match,0,sizeof(match));for(int i=0;i<nn;i++)scanf("%s",mp[i]);for(int i=0;i<nn;i++){for(int j=0;j<nn;j++){if(j==0&&mp[i][j]=='.')X[i*nn+j]=++n;else if(mp[i][j-1]=='X'&&mp[i][j]=='.')X[i*nn+j]=++n;else if(mp[i][j]=='.')X[i*nn+j]=n;}}for(int j=0;j<nn;j++){for(int i=0;i<nn;i++){if(i==0&&mp[i][j]=='.')Y[i*nn+j]=++m;else if(mp[i-1][j]=='X'&&mp[i][j]=='.')Y[i*nn+j]=++m;else if(mp[i][j]=='.')Y[i*nn+j]=m;}}for(int i=0;i<nn;i++){for(int j=0;j<nn;j++){if(mp[i][j]!='X')match[X[i*nn+j]][Y[i*nn+j]]=1;}} max=hungary(); printf ("%d\n",max); } return 0;}/*4.X......XX......2XX.X3.X.X.X.X.3....XX.XX4................0 Sample Output51524*/
0 0
- hdu 1045Fire Net (建图 、二分匹配)
- hdu 1045 Fire Net-二分匹配
- hdu 1045 Fire Net 二分行列匹配
- hdu 1045 Fire Net 二分匹配
- hdu 1045 Fire Net(二分匹配)
- hdu 1045 Fire Net (二分匹配)
- HDU 1045 Fire Net(二分匹配)
- 二分匹配 hdu 1045 Fire Net
- HDU Fire Net 1045 二分匹配
- 二分匹配-hdu-1045-Fire Net
- HDU 1045 Fire Net (二分图的最大匹配)
- HDU 1045 fire net【二分匹配或者DFS】
- hdu 1045 Fire Net(二分图匹配)
- HDU 1045 Fire Net 最大二分图匹配
- HDU 1045 Fire Net(二分图最大匹配)
- HDU 1045 Fire Net(建模,用二分匹配)
- HDU 1045 Fire Net(二分图最大匹配)
- HDU 1045 Fire Net(搜索/贪心/二分图最大匹配)
- SDJZU_新生_排序_sort(sort方法)
- ajax用例
- c++中new的三种用法详细解析
- Balanced Binary Tree
- hdu 2389 Rain on your Parade (二分匹配 Hc 算法)
- hdu 1045 Fire Net 二分匹配
- 一元多项式的求导---使用链表实现的代码
- Android中的Drawable资源
- Android开发效率—Eclipse快捷键
- 如何使用VS创建SharePoint自定义Ribbon菜单(图文和代码)
- Jenkins+Gradle+Android+多渠道包 (一)
- cocos2dx-js学习笔记、官方tests示例
- C语言中动态分配数组
- 使用MFC的CArray自定义数据类型,编译报错的解决方法