hdu_1045Fire Net(二分图匹配)

标签: 图论 二分图匹配


题目链接

Fire Net

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10069 Accepted Submission(s): 5863

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

Zhejiang University Local Contest 2001

题意:

要在一个矩阵格子里面放大炮,每个炮可以攻打一行或一列,注意,这里有墙的地方可以抵挡攻击,问你最多可以在这个图上放几个炮,使得每个炮不能被其他的炮打上.

题解:

首先是建图,这个图因为有了墙的存在把每一行每一列划分成了不同的联通快,即建图的时候,扫描每一行,每一列,把当前联通的部分建成一个点,如果一个点和对应的列节点有交点的话则建立一条从行到列的一个边,然后最后计算这个图的二分图匹配即可

代码:

//二分图一般用链表存边
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 50;
char mp[N][N];
int vis[N];
int head[N];
struct Edge{
int to;
int next;
}edge[N*N];
int rm[N];
int Ecnt;
int id[N][N];//保存节点的编号
void init()
{
Ecnt = 0;
memset(head,-1,sizeof(head));
memset(rm,-1,sizeof(rm));
}
void add(int from, int to){
edge[Ecnt].to = to;
edge[Ecnt].next = head[from];
head[from] = Ecnt++;
edge[Ecnt].to = from;
edge[Ecnt].next = head[to];
head[to] = Ecnt++;
}
int list(int s){
for(int i = head[s]; i != -1; i = edge[i].next){
int t = edge[i].to;
if(vis[t]) continue;
vis[t] = 1;
if(rm[t]==-1||list(rm[t])){
rm[t] = s;
return 1;
}
}
return 0;
}
int Max_match(int n)
{
int ans = 0;
for(int i = 1; i <= n; i++){//注意是从1开始编号的
memset(vis,0,sizeof(vis));
vis[i] = 1;
if(list(i)) ans++;
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n),n)
{
init();
for(int i = 0; i < n; i++){
scanf("%s",mp[i]);
}
int cntx,cnty;
cntx = cnty = 0;
memset(id,0,sizeof(id));
int tp = 1;
//建图,先扫描行
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(mp[i][j] == 'X'){id[i][j] = 0;tp++;}//没有点标记为0
else id[i][j] = tp;
}
tp++;//每一行结束的时候也要区分
}
int tm = tp-1;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(mp[j][i]=='X'){
tp++;
}
else add(id[j][i],tp);
}
tp++;
}
int ans = Max_match(tm);
printf("%d\n",ans);
}
return 0;
}

注释:

这个题给了我们一个建图的思路

hdu_1045Fire Net(二分图匹配)的更多相关文章

  1. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  2. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  3. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  4. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  5. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  6. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  7. BZOJ 1059 & 二分图匹配

    题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...

  8. 【POJ 3020】Antenna Placement(二分图匹配)

    相当于用1*2的板覆盖给定的h*w的格子里的点,求最少的板.可以把格子相邻的分成两个集合,如下图,0为一个集合,1的为一个,也就是(行数+列数)为奇数的是一个集合,为偶数的为另一个集合.1010101 ...

  9. BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)

    蛋蛋安利的双倍经验题 1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1901 Solved: 951 ...

随机推荐

  1. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  2. 优化css选择器

    1.css选择器效率排行从高到低如下: id选择器(#head) 类选择器(.content) 标签选择器(p,h1) 相邻选择器(h1+p) 子选择器(ul < li)

  3. Java-单例模式详解(图文并茂,简单易懂)

    PS:首先我们要先知道什么是单例,为什么要用单例,用的好处是什么等问题来看. 1:java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍两种:懒汉式单例.饿汉式单例单例模式有以下 ...

  4. bat获取系统时间修改系统密码

    @echo off %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...

  5. java 操作本地数据库 mysql

    单线程版 /** * */ import java.sql.*; import java.util.Date; import org.omg.CORBA.PUBLIC_MEMBER; /** * @a ...

  6. 我搞zabbix的那两天(2)

    摘要:前一篇(我搞zabbix的那两天(1))我介绍了Zabbix的安装部署以及遇到的问题,这一篇将介绍zabbix 使用及短信等告警实现!!! Zabbix主界面及汉化方法介绍 1.1 初始化主界面 ...

  7. java多线程(五)-访问共享资源以及加锁机制(synchronized,lock,voliate)

    对于单线程的顺序编程而言,每次只做一件事情,其享有的资源不会产生什么冲突,但是对于多线程编程,这就是一个重要问题了,比如打印机的打印工作,如果两个线程都同时进行打印工作,那这就会产生混乱了.再比如说, ...

  8. Ubuntu16.04 IDE: 用Vim逐步打造一个IDE

    目前打造完成的IDE主要有: terminator+Bundle+NERDtree+YCF(youcompleteme)+UltiSnips+新创建文件自动补充注释和作者,版权信息等 1,当任务比较多 ...

  9. 微信小程序开发之picker选择器组件用法

    picker组件时一个从底部弹起的可滚动的选择器(嵌入页面滚动器组件picker-view查看https://mp.weixin.qq.com/debug/wxadoc/dev/component/p ...

  10. 使用git工具将项目上传到github

    注册github账号 https://github.com/ 安装git工具: https://git-for-windows.github.io/ 上面的准备工作完成后,现在开始操作. 一.进入gi ...