HDU 1045 Fire Net(二分匹配)

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8549    Accepted Submission(s): 4936


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

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#define F first
#define S second
#define PI acos(-1.0)
#define E  exp(1.0)
#define inf 0x3fffffff
#define MAX -INF
#define eps 1e-8
#define MAXN 10000
#define len(a) (__int64)strlen(a)
#define mem0(a) (memset(a,0,sizeof(a)))
#define mem1(a) (memset(a,-1,sizeof(a)))
using namespace std;
__int64 gcd(__int64 a, __int64 b) {
	return b ? gcd(b, a % b) : a;
}
__int64 lcm(__int64 a, __int64 b) {
	return a / gcd(a, b) * b;
}
int max1(int a, int b) {
	return a > b ? a : b;
}
__int64 min(__int64 a, __int64 b) {
	return a < b ? a : b;
}
char mp[50][50];
int mpr[50][50];
int mpc[50][50];
int used[50];
int g[50][50];
int fa[50];
int n, maxr, maxc;
bool dfs(int x) {
	for (int i = 1; i <= maxc; i++) {
		if (!used[i] && g[x][i]) {
			used[i] = 1;
			if (fa[i] == -1 || dfs(fa[i])) {
				fa[i] = x;
				return 1;
			}
		}
	}
	return 0;
}
int hungary() {
	mem0(mpr);
	mem0(mpc);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (mp[i][j] == 'X')
				mpr[i][j] = mpc[i][j] = -1;
		}
	}
	//给行编号
	int p1 = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			while (mpr[i][j] == -1 && j < n) {
				j++;
			}
			p1++;
			while (mpr[i][j] != -1 && j < n) {
				mpr[i][j] = p1;
				if (maxr < p1)
					maxr = p1; //更新行的最大块数
				j++; //要记得写在后面
			}
		}
	}
	//给列编号
	int p2 = 0;
	for (int j = 0; j < n; j++) {
		for (int i = 0; i < n; i++) {
			while (mpc[i][j] == -1 && i < n) {
				i++;
			}
			p2++;
			while (mpc[i][j] != -1 && i < n) {
				mpc[i][j] = p2;
				if (maxc < p2)
					maxc = p2; //更新列的最大块数
				i++; //要记得写在后面
			}
		}
	}
	//建图
	mem0(g);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (mpr[i][j] != -1 && mpc[i][j] != -1)
				g[mpr[i][j]][mpc[i][j]] = 1;
		}
	}
	int cnt = 0;
	mem1(fa);
	for (int i = 1; i <= maxr; i++) {
		mem0(used);
		if (dfs(i))
			cnt++;
	}
	return cnt;
}
int main() {
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	while (scanf("%d", &n) != EOF && n) {
		maxr = maxc = 0;
		for (int i = 0; i < n; i++) {
			scanf("%s", mp[i]);
		}
		printf("%d\n", hungary());
	}
	return 0;
}
</pre><pre name="code" class="cpp"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 15.833333015441895px; background-color: rgb(255, 255, 255);">暴搜也能过的,附暴搜代码:</span>
<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 15.833333015441895px; background-color: rgb(255, 255, 255);">
</span>
<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 15.833333015441895px; background-color: rgb(255, 255, 255);"></span><pre name="code" class="cpp">/*
HDU 1045(暴搜方法) 
*/
#include<stdio.h>
using namespace std;

char map[5][5];
int maxnum;
int n;
bool Build(int row,int col)
{
    int i,j;
    for(i=row;i>=0;i--)
    {
        if(map[i][col]=='O') return false;
        if(map[i][col]=='X') break;
    }    
    for(j=col;j>=0;j--)
    {
        if(map[row][j]=='O') return false;
        if(map[row][j]=='X') break;
    } 
    return true;   
}    
void dfs(int i,int num)
{
    int row,col;
    if(i==n*n)
    {
        if(num>maxnum) maxnum=num;
        return ;
    } 
    else
    {
        row=i/n;
        col=i%n;
        if(map[row][col]=='.'&&Build(row,col))
        {
            map[row][col]='O';
            dfs(i+1,num+1);
            map[row][col]='.';
        }    
        dfs(i+1,num);
    }       
}
int main()
{
    int i;
    while(scanf("%d",&n),n)
    {
        maxnum=0;
        for(i=0;i<n;i++)
          scanf("%s",&map[i]);
        dfs(0,0);
        printf("%d\n",maxnum);
    }
    return 0;    
}

另外 再转一种贪心的做法,没得到证明,不知道是否为正解

小记:这题是暑期训练放在贪心这一章的题,我瞎蒙的, 没想到真的对了

思路:我的这个想法,我自己试了几组数据,也试图去举出点反例,但是都没用,答案都是正确的,而且也比较的简单可行,所以我抱着试试的态度做了。

贪心的思路就是,对方格的每个点记录下它上下左右四个方向所能覆盖的点的个数,

即因为你要放在这点上的话,那么这点的上下左右四个方向就不能再放了,当然碰到墙就打止

例如:

  
  
4 .X.. .... XX.. ....
题目这组数据

那么转化过来就是

2 0 5 5

5 4 7 7

0 0 5 5

4 4 7 7

就这样

然后对个数值进行从小到大排序,依次选择,选择了该点之后,那么它所覆盖的点都不能在被选择了,标记起来即可。

答案就是你所选择的点的个数

<pre name="code" class="cpp">#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8
const int MAX_ = 110;
const int N = 100010;
const int INF = 0x7fffffff;
struct node {
	int s, e;
	int num;
} t[MAX_];
int vis[MAX_][MAX_];
char str[MAX_][MAX_];
bool cmp(const node& a, const node& b) {
	return a.num < b.num;
}
int main() {
	int T;
	int n, m;
	//scanf("%d",&T);
	while (scanf("%d", &n), n) {
		REP(i, 0, n)
		{
			scanf("%s", str[i]);
		}
		int cnt = 0;
		//mst(vis, 0);
		REP(i, 0, n)
		{
			REP(j, 0, n)
			{
				if (str[i][j] == '.') {
					vis[i][j] = 0;
					int tmp = 0;
					int h = 0;
					while (i - h > -1 && str[i - h][j] == '.') {
						h++;
						tmp++;
					}
					h = 1;
					while (i + h < n && str[i + h][j] == '.') {
						h++;
						tmp++;
					}
					h = 1;
					while (j - h > -1 && str[i][j - h] == '.') {
						h++;
						tmp++;
					}
					h = 1;
					while (j + h < n && str[i][j + h] == '.') {
						h++;
						tmp++;
					}
					t[cnt].s = i;
					t[cnt].e = j;
					t[cnt].num = tmp;
					cnt++;
				} else
					vis[i][j] = 1;
			}
		}
		sort(t, t + cnt, cmp);
		int ans = 0;
		REP(i, 0, cnt)
		{
			int x = t[i].s, y = t[i].e;
			if (!vis[x][y]) {
				ans++;
				int h = 0;
				while (x - h > -1 && str[x - h][y] == '.') {
					vis[x - h][y] = 1;
					h++;
				}
				h = 1;
				while (x + h < n && str[x + h][y] == '.') {
					vis[x + h][y] = 1;
					h++;
				}
				h = 1;
				while (y - h > -1 && str[x][y - h] == '.') {
					vis[x][y - h] = 1;
					h++;
				}
				h = 1;
				while (y + h < n && str[x][y + h] == '.') {
					vis[x][y + h] = 1;
					h++;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值