poj 1045 Fire Net(重在建图,二分匹配)
来源:互联网 发布:域名跳转淘宝店铺 编辑:程序博客网 时间:2024/05/20 07:33
题目链接
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10254 Accepted Submission(s): 6003
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.
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
Source
Zhejiang University Local Contest 2001
这一题按照这个数据量暴搜肯定可以过,此处要说的是另一个方法--二分匹配。
这题比较难看出是二分匹配,重在建模,这题是把原始图分别按行和列缩点
建图:横竖分区。先看每一列,同一列相连的空地同时看成一个点,显然这
样的区域不能够同时放两个点。这些点作为二分图的X部。同理在对所有的
行用相同的方法缩点,作为Y部。
X部和Y部连边的条件是两个区域有相交部分(即'.'的地方)。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=5*5+5;char map1[5][5];int map[maxn][maxn],used[maxn],match[maxn],g[maxn][maxn];int row[maxn][maxn],col[maxn][maxn];int n,cnt1,cnt2;bool dfs(int u){for(int i=1;i<=cnt2;i++){if(g[u][i]&&!used[i]){used[i]=1;if(match[i]<0||dfs(match[i])){match[i]=u;return true;}}}return false;}int main(){while(~scanf("%d",&n)&&n){memset(map,0,sizeof(map));memset(row,0,sizeof(row));memset(col,0,sizeof(col));memset(match,-1,sizeof(match));memset(g,0,sizeof(g));for(int i=1;i<=n;i++) scanf("%s",map1[i]+1);for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(map1[i][j]=='X') map[i][j]=-1;cnt1=0,cnt2=0;int p=0;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){p++;while(map[i][j]!=-1&&j<=n){row[i][j]=p;if(cnt1<p) cnt1=p;j++;}}}p=0;for(int j=1;j<=n;j++){for(int i=1;i<=n;i++){p++;while(map[i][j]!=-1&&i<=n){col[i][j]=p;if(cnt2<p) cnt2=p;i++;}}}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(map[i][j]!=-1)g[row[i][j]][col[i][j]]=1;}}int ans=0;for(int i=1;i<=cnt1;i++){memset(used,0,sizeof(used));if(dfs(i)) ans++;}printf("%d\n",ans);}}
0 0
- poj 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-1045-Fire Net-二分图匹配-匈牙利算法-java
- 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
- U-boot中常用参数设定及常用宏的解释和说明
- mysql存储过程中in、out、inout参数使用实际案例
- 访问单个节点的删除
- wordpress移植遇到的相关问题处理
- HashMap和Hashtable的实现原理
- poj 1045 Fire Net(重在建图,二分匹配)
- 0、计算机系统
- Java 方法
- 递归提取目录下的所有json格式的文件,重命名,并保存到另外的目录
- HDOJ 1541 Stars
- 51nod oj 1101 换零钱【完全背包】
- 在 Raspberry Pi 2/B+上安装OpenCV和Python
- 提升自己
- HDU:1513 Palindrome(回文字符串+最长公共子序列+滚动数组)