hdu1045——Fire Net(二分图+行列匹配)
来源:互联网 发布:招聘网络女主播 编辑:程序博客网 时间:2024/05/16 18:37
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
如图,求最多能放置的点能覆盖所有空白区域
把每一行能连在一起的编号,因为这些区域最多放一个点就够了,不能的就编另一个号;同理每一列也是这样
然后照着这两图再建一个二分图,行和列相交的区域可以匹配
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>//#include <map>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 50#define Mod 10001using namespace std;bool vis[MAXN];char mp[MAXN][MAXN];int n,cnt_c,cnt_r;int G[MAXN][MAXN],row[MAXN][MAXN],col[MAXN][MAXN],r[MAXN],c[MAXN]; //匹配路径;int find(int cur){ for(int i=0; i<cnt_c; ++i) { if(!vis[i]&&G[cur][i]) { vis[i] = true; if(c[i] == -1 || find(c[i])) { c[i]=cur; r[cur]=i; return 1; } } } return 0;}int main(){ while(~scanf("%d",&n)&&n) { cnt_r=cnt_c=0; memset(col,-1,sizeof(col)); memset(row,-1,sizeof(row)); for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) cin>>mp[i][j]; 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_r; cnt_r++; } if(mp[j][i]=='.'&&col[j][i]==-1) { for(int k=j; mp[k][i]=='.'&&k<n; ++k) col[k][i]=cnt_c; cnt_c++; } } memset(G,0,sizeof(G)); for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) if(mp[i][j]=='.') G[row[i][j]][col[i][j]]=1; memset(r,-1,sizeof(r)); memset(c,-1,sizeof(c)); int ans=0; for(int i=0; i<cnt_r; ++i) if(r[i]==-1) { memset(vis,0,sizeof(vis)); if(find(i)) ans++; } printf("%d\n",ans); } return 0;}
- hdu1045——Fire Net(二分图+行列匹配)
- hdu1045—Fire Net(二分图最大匹配)
- hdu1045 Fire Net(二分图最大匹配)
- hdu1045 Fire Net(二分图匹配)
- 【二分匹配】hdu1045 Fire Net
- HDU1045 Fire Net 二分图
- hdu1045 Fire Net(最大二分匹配)
- HDU1045 Fire Net-二分匹配&缩点
- hdu 1045 Fire Net 二分行列匹配
- hdu1045 Fire Net —— dfs
- hdu1045-Fire Net(匹配,增广路,思路)
- hdu1045 Fire Net(最大匹配+建模)
- HDU1045 - Fire Net (深搜)
- HDU1045-Fire Net(回溯)
- HDU1045 Fire Net(dfs)
- hdu1045二分匹配(难在建图)
- 二分图匹配(缩点建图)—— Fire Net ( HDU 1045 )
- hdu 1045 Fire Net(二分图匹配)
- c++(18)使用using
- 函数dev_get_drvdata()
- Android 获取应用的点击次数和使用时长
- 优先读者/写者问题—操作系统
- hibernate实体类中为何要重写equals与hashcode方法
- hdu1045——Fire Net(二分图+行列匹配)
- LeetCode------33. Search in Rotated Sorted Array(旋转数组中查找)
- Lua table 详解
- 快速开发框架一套
- [二进制]1000桶药,有一桶有毒,用多少小白鼠测试
- 随机数范围扩展方法总结
- [LeetCode-Java]22. Generate Parentheses
- 例题25(侏罗纪)(中途相遇法)!
- 《React-Native系列》35、 RN在Android下支持gif的另一种方案