HDU 1045(二分图)
来源:互联网 发布:js style.display 编辑:程序博客网 时间:2024/06/13 14:28
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
题意:给你 N × N 的方格,堡垒不能在一条直线上,不然会互相伤害,但是有墙就好了, x 是墙, '.' 是可以放的位置,然后让你求怎么放置最多的堡垒
题解:看出来是一道二分匹配的题,二分两端时行与列,这里要用到压缩的方法,将每行每列的点压缩,具体是将一行的 '.'压缩成一个,一列的'.'也压缩成一个,遇到墙就停止,遇到点再次压缩,于是可以构建一个行与列的二分图,求最大匹配就好了
#include <cstdio>#include <cstring>#include <vector>using namespace std;#define M 10vector<int> vec[M];char map[M][M];int vis_col[M][M], vis_row[M][M], n, row, col, mark[M];bool used[M];bool dfs(int u){ for(int i=0; i<vec[u].size(); i++) { int v = vec[u][i]; if(!used[v]) { used[v] = true; if(mark[v] == -1 || dfs(mark[v])) { mark[v] = u; return true; } } } return false;}int main(){ while(scanf("%d", &n) && n) { for(int i=0; i<n; i++) { scanf("%s", map[i]); } memset(vis_row, -1, sizeof(vis_row)); memset(vis_col, -1, sizeof(vis_col)); row = col = 0; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(map[i][j] == '.' && vis_col[i][j] == -1)//每列压缩 { for(int k=j; k<n; k++) { if(map[i][k] == '.') { vis_col[i][k] = col;//记录二分图两端的值 } else { break;//遇墙退出 } } col++; } if(map[i][j] == '.' && vis_row[i][j] == -1)//每行压缩 { for(int k=i; k<n; k++) { if(map[k][j] == '.') { vis_row[k][j] = row; } else { break; } } row++; } } } for(int i=0; i<M; i++) { vec[i].clear(); } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(map[i][j] == '.') { vec[vis_row[i][j]].push_back(vis_col[i][j]);//找出可以互相伤害的堡垒 } } } int ans = 0; memset(mark, -1, sizeof(mark)); for(int i=0; i<row; i++) { memset(used, false, sizeof(used)); if(dfs(i)) { ans++; } } printf("%d\n", ans); } return 0;}
- HDU 1045(二分图)
- HDU-1045-二分图
- hdu 1045 二分图
- 二分图匹配 hdu 1045
- HDU 1045 二分图匹配
- hdu 3118 (二分图)
- hdu 2063 (二分图)
- hdu 1045 Fire Net (dfs || 二分图)
- hdu 1045 Fire Net(二分图匹配)
- 二分图的最大分配(hdu 1045)
- HDU 1045 Fire Net(二分图最大匹配)
- HDU 1045 Fire Net(二分图最大匹配)
- HDU 2236 无题II(二分图匹配+二分)
- hdu 1045 Fire Net (二分图)
- hdu 1045 二分图最大匹配
- HDU 1045 Fire Net 二分图
- HDU 1045 Fire Net(二分匹配)
- HDU 1045 二分匹配
- centos locate 命令安装
- wpf记忆(界面随鼠标移动)
- get请求中文乱码问题
- python元组(Tuple)、字典(Dictionary)详解
- 文章标题
- HDU 1045(二分图)
- 元素分类--块级元素
- cookie机制与session机制的区别
- Spring Boot 入门
- poj3253Fence Repair(贪心,哈夫曼编码)
- poj 1061 扩展gcd
- SAX解析xml
- 利用linux shell script来测试linux c程序------脚本自动化测试用例代替手动测试用例
- Android ViewSwitcher简介和使用