Fire Net


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.

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.

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: Zhejiang University Local Contest 2001

 //2014-03-16 19:29:03     Accepted    1002    C++    0    172    姜伯约
/* 题意:
最多可在棋盘上放多少个不冲突的棋(无障碍时不能同行同列) 二分匹配:
二分匹配的经典题,也可以用搜索做。
用二分匹配做的难点是建图,具体细节要好好体会。 */
#include<stdio.h>
#include<string.h>
char c[][],mapc[][],mapr[][];
int g[][];
int vis[];
int match[];
int n,N,M;
void build()
{
memset(mapc,,sizeof(mapc));
memset(mapr,,sizeof(mapr));
memset(g,,sizeof(g));
N=M=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
if(c[i][j]=='X')
mapr[i][j]=mapc[i][j]=-;
int cnt=;
for(int i=;i<n;i++)
for(int j=;j<n;j++){
while(mapr[i][j]==- && j<n) j++;
cnt++;
while(mapr[i][j]!=- && j<n){
mapr[i][j]=cnt;
if(cnt>N) N=cnt;
j++;
}
}
cnt=;
for(int j=;j<n;j++)
for(int i=;i<n;i++){
while(mapc[i][j]==- && i<n) i++;
cnt++;
while(mapc[i][j]!=- && i<n){
mapc[i][j]=cnt;
if(cnt>M) M=cnt;
i++;
}
}
for(int i=;i<n;i++)
for(int j=;j<n;j++)
if(mapr[i][j]!=- && mapc[i][j]!=-)
g[mapr[i][j]-][mapc[i][j]-]=;
}
int dfs(int x)
{
for(int i=;i<M;i++){
if(!vis[i] && g[x][i]){
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
}
int hungary()
{
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<N;i++){
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
return ans;
}
int main(void)
{
while(scanf("%d",&n),n)
{
for(int i=;i<n;i++)
scanf("%s",c[i]);
build();
printf("%d\n",hungary());
}
return ;
}

 

zoj 1002 Fire Net (二分匹配)的更多相关文章

  1. hdu 1045 Fire Net(二分匹配 or 暴搜)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  2. [ZOJ 1002] Fire Net (简单地图搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...

  3. ZOJ 3156 Taxi (二分匹配+二分查找)

    题目链接:Taxi Taxi Time Limit: 1 Second      Memory Limit: 32768 KB As we all know, it often rains sudde ...

  4. ZOJ 1002 Fire Net(dfs)

    嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...

  5. ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...

  6. ZOJ 1002 Fire Net

    题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...

  7. zoj 1002 Fire Net 碉堡的最大数量【DFS】

    题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...

  8. DFS ZOJ 1002/HDOJ 1045 Fire Net

    题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...

  9. hdu-1045.fire net(缩点 + 二分匹配)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. iOS MapKit地图

    地图框架:#import <MapKit/MapKit.h> 基本属性和方法: 属性: 地图类视图:MKMapView 地图类型:MKMapType mapType 地图旋转:rotate ...

  2. jquery 筛选元素 (2)

    .add() 创建一个新的对象,元素添加到匹配的元素集合中. .add(selector) selector 一个字符串表示的选择器表达式.找到更多的元素添加到匹配的元素集合. $("p&q ...

  3. 微信小程序之setData原理以及几点关于小程序的吐槽

    小程序不方便之处: 1: 想做个获取列表中每个item所在的对应滚动条的高度在哪里,或者元素本身的高度也不好获取,极其不方便,网上看的别人做的滚动条相关的,都是写死或者已知的item高度,目前没找到正 ...

  4. Java分享笔记:使用keySet方法获取Map集合中的元素

    /*--------------------------- Map集合中利用keySet方法获取所有的元素值: ....keySet方法:将Map中的所有key值存入到Set集合中, ....利用Se ...

  5. Thymeleaf显示Map集合数据

    <select class="form-control zz-set-input-size" id="channel"> <option va ...

  6. Spark调优之JVM调优

    一.JVM调优 JVM: 老年代: 存放少量生命周期长的对象,如连接池 年轻代: Spark task执行算子函数自己创建的大量对象 JVM机制: 对象进入java虚拟机之后会放在eden区域和一个s ...

  7. 图解HTTP总结(2)——简单的HTTP协议

    HTTP协议是一种不保存状态,即无状态(stateless)协议.HTTP协议自身不对请求和响应之间的通信状态进行保存.也就是说在HTTP这个级别,协议对于发送过的请求或响应都不做持久化处理. 使用H ...

  8. chroot: cannot run command `/bin/bash': No such file&nbs

    最近在使用chroot去重新的挂载一个根目录,总是出现上面的问题,很烦,好久了没有解决, 然后自己就写了一个复制依赖库的脚本,然后发现可以切换了,然后就重新试着去挂载根目录 终于发现了原因. ---- ...

  9. 华为模拟器ensp代码错误2,41,40问题的解决

    win8+ensp320 ensp这是个神奇的软件,问题竟然出现的这么莫名其妙..前一秒还是好的时候,后一秒就立马出现了问题.不过不要慌...沉住气,把这篇文章看下去. 博主从昨天开始,ensp神奇的 ...

  10. ln -s 软链接产生Too many levels of symbolic links错误

    不能使用相对路径, ln -s ./cmake /usr/bin/ 而是要 ln -s /usr/local/bin/cmake /usr/bin/