Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17669    Accepted Submission(s): 10749

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
 
Source
 
 
 
 /*
本题大意:给出一个n * n的矩阵,让你在这个矩阵中放置尽可能多的炮台。
放置炮台的规则为,任意两个炮台都不在同一行同一列,除非他们之间有wall。 本题思路:这题很明显回溯可以做,但是下面我们用二分匹配讲解一下这个题,
很明显我们知道如果一个点被选取,那么和他在同一行或者同一列的中间不存
在wall的所有点都不能被选取,所以我们就可以考虑如何限制?我们可以把
这个图按照行缩点,再按照列缩点,那么我们就可以知道,对于缩点之后的行
标号i和列标号j,如果i -> j之间存在一条边,那就说明在方格(i, j)上放了
一个点,那么i行j列不能再放其它其它点,那么我们可以想到对得到的缩点进行
建立二分图,行和列分别为二部图的两部分,然后依据原图建边,寻找到最大匹配
即为答案。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
bool used[maxn];//表示是否在交错路中
int linker[maxn];//存储匹配点
int g[maxn][maxn];//存缩点之后的图
char str[maxn][maxn];
int x[maxn][maxn], cntx;//行点集
int y[maxn][maxn], cnty;//列点集 bool dfs(int u) {
for(int v = ; v <= cnty; v ++) {
if(g[u][v] && !used[v]) {
used[v] = true;
if(linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int hungary() {
int res = ;
memset(linker, -, sizeof linker);
for(int u = ; u <= cntx; u ++) {
memset(used, false, sizeof used);
if(dfs(u)) res ++;
}
return res;
} int main() {
int n;
while(scanf("%d", &n) && n) {
memset(x, , sizeof x);
memset(y, , sizeof y);
memset(g, false, sizeof g);
for(int i = ; i < n; i ++) {
scanf("%s", str[i]);
}
cntx = ; //对行缩点
for(int i = ; i < n; i ++) {
for(int j = ; j < n; j ++) {
if(str[i][j] == '.') x[i][j] = cntx;
if(str[i][j] == 'X') cntx ++;
}
cntx ++;
} //对列缩点
cnty = ;
for(int j = ; j < n; j ++) {
for(int i = ; i < n; i ++) {
if(str[i][j] == '.') y[i][j] = cnty;
if(str[i][j] == 'X') cnty ++;
}
cnty ++;
}
for(int i = ; i < n; i ++) {
for(int j = ; j < n; j ++) {
if(str[i][j] == '.') g[x[i][j]][y[i][j]] = true;
}
}
printf("%d\n", hungary());
}
return ;
}

最新文章

  1. Android(Linux)控制GPIO方法二
  2. Sqoop1.4.6配置和使用
  3. Linux and the Device Tree
  4. Framework7 – 赞!功能齐全的 iOS7 App 前端框架
  5. 【C#】【MySQL】C#获取存储过程的Output输出参数值
  6. React组件生命周期-正确执行初始化阶段的函数
  7. Flex随笔
  8. 由于“Table(T_Test)”没有主键,因此无法在其上执行 Create、Update 或 Delete 操作
  9. 页面loading提示效果
  10. UVA - 10048 Audiophobia (Floyd应用)
  11. 跟着刚哥梳理java知识点——IO(十五)
  12. scott表结构
  13. 《ZeroC Ice 权威指南》笔记
  14. java日志文件用法总结
  15. sourceTree 代码未同步合并
  16. dskinlite(uieasy mfc界面库)使用记录4:listbox测试
  17. Sunscreen [POJ3614] [贪心]
  18. python的类的 静态属性 类方法 静态方法
  19. Techniques for HA IT Management
  20. vertex shader(3)

热门文章

  1. 原始http下载图片生成文件
  2. #pragma 的使用(转)
  3. Arduino通讯串口
  4. hihocode 1584 : Bounce (找规律)(2017 北京网络赛G)
  5. 6392. 【NOIP2019模拟2019.10.26】僵尸
  6. Shell入门02
  7. 阿里云产品家族再添新丁:视觉AI、CPFS一体机助力企业全面上云
  8. 超大文件上传方案( Java )
  9. 小程序cover-view
  10. 按ECS退出全屏模式