NYOJ 题目587 blockhouses(二分图最大匹配)

blockhouses

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述
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.
样例输入
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
样例输出
5
1
5
2
4
来源
2012年10月份月赛(高年级组)
上传者
ACM_丁国强

ac代码

#include<stdio.h>
#include<string.h>
char map[10][10];
int ans[20][20],vis[20],link[20],n,x[10][10],y[10][10],n1,n2;
int dfs(int x)
{
	int i;
	for(i=1;i<n2;i++)
	{
		if(!vis[i]&&ans[x][i])
		{
			vis[i]=1;
			if(link[i]==-1||dfs(link[i]))
			{
				link[i]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	while(scanf("%d",&n)!=EOF,n)
	{
		int i,j;
		for(i=1;i<=n;i++)
		{
			scanf("%s",map[i]+1);
		}
		//int n1,n2;
		n1=n2=1;
		memset(ans,0,sizeof(ans));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(map[i][j]=='.')
				{
					x[i][j]=n1;
				}
				if(map[i][j]=='X')
					n1++;
			}
			n1++;
		}
		for(j=1;j<=n;j++)
		{
			for(i=1;i<=n;i++)
			{
				if(map[i][j]=='.')
					y[i][j]=n2;
				if(map[i][j]=='X')
					n2++;
			}
			n2++;
		}
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(map[i][j]=='.')
				{
					ans[x[i][j]][y[i][j]]=1;
				}
			}
		}
		memset(link,-1,sizeof(link));
		int res=0;
		for(i=1;i<n1;i++)
		{
			memset(vis,0,sizeof(vis));
			if(dfs(i))
				res++;
		}
		printf("%d\n",res);
	}
}
时间: 2024-10-10 09:44:46

NYOJ 题目587 blockhouses(二分图最大匹配)的相关文章

NYOJ 587 blockhouses 【DFS】

blockhouses 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 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 th

nyoj 239 月老的难题 二分图最大匹配

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=239 思路:二分图最大匹配~~~ 邻接表A过~~ #include <iostream> #include <cstring> using namespace std; int num; int n,k; int s; struct node { int u,v,w; int next; }map[10010]; int head[510]; int match[510];

图论——LCA、强联通分量、桥、割顶、二分图最大匹配、网络流

A: 交通运输线 时间限制: 5 Sec  内存限制: 128 MB 题目描述 战后有很多城市被严重破坏,我们需要重建城市.然而,有些建设材料只能在某些地方产生.因此,我们必须通过城市交通,来运送这些材料的城市.由于大部分道路已经在战争期间完全遭到破坏,可能有两个城市之间没有道路.当然在运输线中,更不可能存在圈. 现在,你的任务来了.给你战后的道路情况,我们想知道,两个城市之间是否存在道路,如果存在,输出这两个城市之间的最短路径长度. 输入 第一行一个整数Case(Case<=10)表示测试数据

POJ2239 Selecting Courses(二分图最大匹配)

题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 using namespa

POJ - 1422 Air Raid 二分图最大匹配

题目大意:有n个点,m条单向线段.现在问要从几个点出发才能遍历到所有的点 解题思路:二分图最大匹配,只要一条匹配,就表示两个点联通,两个点联通只需要选取其中一个点即可,所以有多少条匹配,就可以减去多少个点 #include<cstdio> #include<cstring> using namespace std; const int N = 130; int g[N][N], vis[N], link[N]; int n, m; void init() { memset(g, 0

[HDU] 2063 过山车(二分图最大匹配)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 #include<stdbool.h> 7 #include<ti

51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题

题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左边的点和没用过的右边的点连起来, 如果遇到一个点已经连过就试着把原来的拆掉 把现在这条线连起来看能不能多连上一条线. 总结来说就是试和拆,试的过程很简单,拆的过程由于使用递归写的,很复杂.很难讲清楚,只能看代码自己理会. 代码(有注释): #include <bits\stdc++.h> usin

二分图最大匹配|UOJ#78|匈牙利算法|边表|Elena

#78. 二分图最大匹配 从前一个和谐的班级,有 nlnl 个是男生,有 nrnr 个是女生.编号分别为 1,-,nl1,-,nl 和 1,-,nr1,-,nr. 有若干个这样的条件:第 vv 个男生和第 uu 个女生愿意结为配偶. 请问这个班级里最多产生多少对配偶? 输入格式 第一行三个正整数,nl,nr,mnl,nr,m. 接下来 mm 行,每行两个整数 v,uv,u 表示第 vv 个男生和第 uu 个女生愿意结为配偶.保证 1≤v≤nl1≤v≤nl,1≤u≤nr1≤u≤nr,保证同一个条件

ZOJ1654 Place the Robots(二分图最大匹配)

二分图最大匹配也叫二分图最大边独立数,就是二分图中最多能取出两两不相邻的边的数目. 如果题目没有墙,那就是一道经典的二分图最大匹配问题: 把地图上的行和列分别作为点的X部和Y部,地图上每一块空地看作边,边的两个端点就是它所在的x行y列.这样,求最大边独立集即可. 而这一题有墙,然后我不会了.. 其实这题的建模也是一样的,也是行和列作为点,空地作为边: 对于每一行把被墙分隔的每一块连通的区域缩成一点,列也一样: 行缩成的点作为X部,列Y部: 某行连通区域最多就只能在区域内某一块空地放机器人,列也是