zoj 1002 Fire Net (二分匹配)
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 (二分匹配)的更多相关文章
- hdu 1045 Fire Net(二分匹配 or 暴搜)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- [ZOJ 1002] Fire Net (简单地图搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002 题目大意: 给你一个n*n的地图,地图上的空白部分可以放棋 ...
- ZOJ 3156 Taxi (二分匹配+二分查找)
题目链接:Taxi Taxi Time Limit: 1 Second Memory Limit: 32768 KB As we all know, it often rains sudde ...
- ZOJ 1002 Fire Net(dfs)
嗯... 题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501 这道题是想出来则是一道很简单的dfs: 将一 ...
- ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...
- ZOJ 1002 Fire Net
题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...
- zoj 1002 Fire Net 碉堡的最大数量【DFS】
题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...
- DFS ZOJ 1002/HDOJ 1045 Fire Net
题目传送门 /* 题意:在一个矩阵里放炮台,满足行列最多只有一个炮台,除非有墙(X)相隔,问最多能放多少个炮台 搜索(DFS):数据小,4 * 4可以用DFS,从(0,0)开始出发,往(n-1,n-1 ...
- hdu-1045.fire net(缩点 + 二分匹配)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- 瓣呀,一个基于豆瓣api仿网易云音乐的开源项目
整体采用material design 风格,本人是网易云音乐的粉丝,所以界面模仿了网页云音乐,另外,项目中尽量使用了5.0之后的新控件. 项目整体采用mvp+rxjava+retrofit 框架,使 ...
- MyString类的实现--基础中的基础C语言
MyString 类是学习 C++ 的过程中一个很重要的例子,涉及到面向对象的封装.堆内存申请和释放.函数的重载以及 C++ 的 “Big Three”.本例子重点在于复习和理解上述的 C++ 特性, ...
- 【MYSQL笔记1】mysql的基础知识
首先进去mysql.打开电脑命令提示符(cmd):输入mysql -uroot -p 代表的意思是使用ruser使用者root的方式,打开mysql,-p代表password,如果有的话,回车之后 ...
- 制作linux系统U盘并使用U盘安装CentOS7.6系统
目录 一.制作linux启动盘 1.1. 准备工作 1.2. 制作linux系统U盘 二.使用U盘安装Centos7.6 2.1. 使用U盘启动 2.2. 更改 ...
- spring框架中@PostConstruct的实现原理
在spring项目经常遇到@PostConstruct注解,首先介绍一下它的用途: 被注解的方法,在对象加载完依赖注入后执行. 此注解是在Java EE5规范中加入的,在Servlet生命周期中有一定 ...
- 一次完整的http请求处理过程
一次完整的HTTP请求需要的7个步骤 HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先 ...
- 精读《12 个评估 JS 库你需要关心的事》
1 引言 作者给出了从 12 个角度全面分析 JS 库的可用性,分别是: 特性. 稳定性. 性能. 包生态. 社区. 学习曲线. 文档. 工具. 发展历史. 团队. 兼容性. 趋势. 下面总结一下作者 ...
- myeclipse从SVN上拉项目,各种报错,jar包没有引入
问:项目中myeclipse从SVN上拉项目,各种报错,jar包没有引入 答: 从SVN拉项目步骤一定不能出错,一有点差异就会出非常多的事情 1-右键项目checkout的时候 第一页选默认值就行 点 ...
- cx_freeze的安装使用
python是一个非常非常优秀的编程语言,它最大的特性就是跨平台.python程序几乎可以在所有常见的平台中进行使用,而且大部分无需修改任何代码!不过,python也有一点点小缺憾(这个是由于自身本质 ...
- OSI七层模型 学习摘要
OSI参考模型是计算机网路体系结构发展的产物.它的基本内容是开放系统通信功能的分层结构.这个模型把开放系统的通信功能划分为七个层次,从邻接物理媒体的层次开始,分别赋于1,2,……7层的顺序编号,相应地 ...