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. 可复用 React 的 HOC 以及的 Render Props

    重复是不可能的,这辈子都不可能写重复的代码 当然,这句话分分钟都要被产品(领导)打脸,真的最后一次改需求,我们烦恼于频繁修改的需求 虽然我们不能改变别人,但我们却可以尝试去做的更好,我们需要抽象,封装 ...

  2. c++学习(一)

    基本数据类型 类型 关键字 描述 所占字节数 最大值 最小值 布尔型 bool 存储值 true 或 false bool 1 0 字符型 char 通常是一个八位字节(一个字符).这是一个整数类型 ...

  3. Problem 1004-2017 ACM/ICPC Asia Regional Shenyang Online

    题目来源:array array array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...

  4. C#进阶学习笔记(个人整理)

    学习笔记 第一章: 一.回顾数组 1.变量 : 只能存储一个数据 2.数组 :存储固定大小的相同类型的数据 3.对象 : 存储多个相同/不同类型的数据 4.集合 : 特殊的容器,存储N个相同/不同类型 ...

  5. mysql 1055 的错误

    1.Err1055,出现这个问题往往是在执行sql语句时候,在最后一行会出现这个问题. [Err] 1055 - Expression #1 of ORDER BY clause is not in ...

  6. Spring入门注解版

    参照博文Spring入门一,以理解注解的含义. 项目结构: 实现类:SpringHelloWorld package com.yibai.spring.helloworld.impl; import ...

  7. python join() 提示UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 0: unexpected end of的原因及解决办法

    问题: 在使用join()将列表元素连接成字符串时出错如下 return split.join(result) UnicodeDecodeError: 'utf8' codec can't decod ...

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

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

  9. Python学习笔记:sqlite3(sqlite数据库操作)

    对于数据库的操作,Python中可以通过下载一些对应的三方插件和对应的数据库来实现数据库的操作,但是这样不免使得Python程序变得更加复杂了.如果只是想要使用数据库,又不想下载一些不必要的插件和辅助 ...

  10. 小白对异步IO的理解

    前言 看到越来越多的大佬都在使用python的异步IO,协程等概念来实现高效的IO处理过程,可是我对这些概念还不太懂,就学习了一下. 因为是初学者,在理解上有很多不到位的地方,如果有错误,还希望能够有 ...