Fire Net

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

Total Submission(s): 8791    Accepted Submission(s): 5076
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
 
Recommend
We have carefully selected several similar problems for you:  1050 1067 1258 1053 1789 

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
}rec[100][100];
vector<int>G[1000];
int pipei[1000],used[1000],n,cur,row;
char map[4][4];
void getmap()
{
cur=row=0;
bool flag=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(map[i][j]=='.')
rec[i][j].x=row,flag=true;
if(map[i][j]=='X')
row++,flag=false;
}
if(flag)
row++;
}
for(int j=0;j<n;j++)
{
for(int i=0;i<n;i++)
{
if(map[i][j]=='.')
rec[i][j].y=cur,flag=true;
if(map[i][j]=='X')
cur++,flag=false;
}
if(flag)
cur++;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(map[i][j]=='.')
{
int x=rec[i][j].x;
int y=rec[i][j].y;
G[x].push_back(y);
}
}
}
}
int find(int x)
{
for(int i=0;i<G[x].size();i++)
{
int y=G[x][i];
if(!used[y])
{
used[y]=1;
if(pipei[y]==-1||find(pipei[y]))
{
pipei[y]=x;
return 1;
}
}
}
return 0;
}
void solve()
{
int ans=0;
memset(pipei,-1,sizeof(pipei));
for(int i=0;i<row;i++)
{
memset(used,0,sizeof(used));
ans+=find(i);
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<100;i++)
G[i].clear();
for(int i=0;i<n;i++)
scanf("%s",map[i]);
getmap();
solve();
}
return 0;
}

hdoj--1045--Fire Net(二分图)的更多相关文章

  1. DFS ZOJ 1002/HDOJ 1045 Fire Net

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

  2. HDU 1045 Fire Net 二分图建图

    HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...

  3. hdoj 1045 Fire Net

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

  4. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  5. hdu 1045 Fire Net 二分图匹配 && HDU-1281-棋盘游戏

    题意:任意两个个'车'不能出现在同一行或同一列,当然如果他们中间有墙的话那就没有什么事,问最多能放多少个'车' 代码+注释: 1 //二分图最大匹配问题 2 //难点在建图方面,如果这个图里面一道墙也 ...

  6. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  7. HDU 1045 Fire Net 【连通块的压缩 二分图匹配】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    ...

  8. HDU1045 Fire Net —— 二分图最大匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)  ...

  9. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  10. HDU 1045 Fire Net(dfs,跟8皇后问题很相似)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)   ...

随机推荐

  1. Vue初级

    来学习vue.js的宝宝们应该都了解了什么是vue并且是有兴趣去学的,我也是自己学习vue,欢迎大家一起讨论. 现在,我么先自己尝试用vue来写个hello world吧. 一.创建一个 .html  ...

  2. VHDL之FSM

    1 Intro The figure shows the block diagram of a single-phase state machine. The lower section contai ...

  3. jquery radio、 checkbox、 select 操作

    转载:http://www.haorooms.com/post/checkandselect $("input[id^='code']");//id属性以code开始的所有inpu ...

  4. 作业07之《MVC模式》

    MVC(Model View Controller)模型-视图-控制器 MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的 ...

  5. logger日志

    Level 描述 ALL 各级包括自定义级别 DEBUG 指定细粒度信息事件是最有用的应用程序调试 ERROR 错误事件可能仍然允许应用程序继续运行 FATAL 指定非常严重的错误事件,这可能导致应用 ...

  6. PAT_A1129#Recommendation System

    Source: PAT A1129 Recommendation System (25 分) Description: Recommendation system predicts the prefe ...

  7. 【剑指Offer】28、数组中出现次数超过一半的数字

      题目描述:   数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.   例如:输入如下所示的一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过 ...

  8. 八进制、十进制、操作符(day04)

    把二进制表示的数字从右向左每三个数位分成 一组,每组用一个0到7之间的数字替换. 这个替换结果叫做数字的八进制表示方式 (八进制) 可以直接在程序里用八进制方式表示数字, 这种数字必须以0做开头 可以 ...

  9. Qt5.11+opencv3.4的配置安装

    系统:Windows 10 64位 前期准备: 1.CMake下载安装 下载地址:https://cmake.org/download/ 选择msi安装文件,按照提示一步一步按照就好 可以参考:htt ...

  10. 2、在1.VMware虚拟机上安装ubantu系统

    1.新建新的虚拟机系统 2.使用自定义高级安装 3.选择下一步操作 4.选择稍后安装 4.因为我们要安装的是Linux的发行版本ubuntu,所以这里选择Linux(L),版本是Ubuntu 64位, ...