hdu1045 Fire Net(最大匹配+建模)
来源:互联网 发布:c语言的未来 编辑:程序博客网 时间:2024/06/05 09:01
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12998 Accepted Submission(s): 7884
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
题意:黑点是一个攻击装置,攻击上下左右,方块是墙可以阻挡攻击,问在装置不能相互攻击的前提下,最多可以放置多少个装置
我们可以将边界与墙之前抽象为一个集合,因为这之间只能放一个装置;
用这种方法分别将行和列抽象,此时,每个集合的编号都得出,那么(i,j)这点就可以建图为 i这点的行集合编号——>j这点的列集合编号
然后求行集合与列集合之间的最大匹配即可
#include<bits/stdc++.h>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;char e[10][10];int er[10][10],ec[10][10];int g[10][10];int match[10];bool vis[10];int n1,n2;bool dfs(int u){ for(int i=1;i<=n2;i++) { if(!vis[i]&&g[u][i]) { vis[i]=true; if(match[i]==-1||dfs(match[i])) { match[i]=u; return true; } } } return false;}int main(){ int n; while(scanf("%d",&n)&&n) { mem(match,-1); mem(g,0); mem(ec,0); mem(er,0); for(int i=1;i<=n;i++) { scanf("%s",e[i]+1); } n1=0,n2=0; int p1=0,p2=0; //获得行集合的编号 for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { while(e[i][j]=='X'&&j<=n) { j++; } p1++; while(e[i][j]!='X'&&j<=n) { er[i][j]=p1; n2=n2>p1?n2:p1; j++; } } } //获得列集合的编号 for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { while(e[j][i]=='X'&&j<=n) { j++; } p2++; while(e[j][i]!='X'&&j<=n) { ec[j][i]=p2; n1=n1>p2?n1:p2; j++; } } } //建图 for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(e[i][j]!='X') { g[ec[i][j]][er[i][j]]=1; } } } int ans=0; for(int i=1;i<=n1;i++) { mem(vis,0); if(dfs(i))ans++; } printf("%d\n",ans); } return 0;}
阅读全文
0 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(DFS)
- HDU1045 Fire Net
- HDU1045:Fire Net(DFS)
- HDU1045 Fire Net 【DFS】
- hdu1045 Fire Net
- hdu1045---Fire Net
- HDU1045 Fire Net
- HDU1045 Fire Net
- Fire Net--hdu1045
- HDU1045 Fire Net(DFS)
- hdu1045 Fire Net
- C++基础代码—20余种数据结构和算法的实现
- 网易2017秋招编程题:暗黑的字符串 [python]
- SparkML-note-Kmeans
- C语言常用操作符总结
- SQL Server中日期时间类型字段只取年月日
- hdu1045 Fire Net(最大匹配+建模)
- 开发者和工程师的区别?
- 唯快不破:TIME_WAIT重用与RFC1337
- git 管理多个私钥
- 网易2017秋招编程题:最大奇约束 [python]
- ajaxupload返回带有pre标签的数据处理方法
- pytorch学习笔记(十五):pytorch 源码编译碰到的坑总结
- 文章标题
- 从概念到案例:初学者须知的十大机器学习算法