HDU 1045 Fire Net

来源:互联网 发布:android 网络监测 编辑:程序博客网 时间:2024/06/07 07:05

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045

Fire Net

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


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......2XX.X3.X.X.X.X.3....XX.XX4................0
 

Sample Output
51524
给你一张n*n的地图。

问你最多能放几个碉堡。

两个碉堡不能在同一个直线上,除非它们之间有墙挡着。

可以对格子编号,然后用深搜暴搜出所有解,取最大的即可。

#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<sstream>#include<vector>#include<map>#include<list>#include<set>#include<queue>#define LL long longusing namespace std;const int maxn=25;char Map[maxn][maxn];int n,m,Max;bool judge(int x,int y){    for(int i=y-1;i>=0;i--)        if(Map[x][i]=='o') return 0;        else if(Map[x][i]=='X') break;    for(int i=x-1;i>=0;i--)        if(Map[i][y]=='o') return 0;        else if(Map[i][y]=='X') break;    return 1;}void dfs(int d,int cnt){    if(d==n*n)    {        Max=max(Max,cnt);        return ;    }    int x=d/n,y=d%n;    if(Map[x][y]=='.'&&judge(x,y))    {        Map[x][y]='o';        dfs(d+1,cnt+1);        Map[x][y]='.';    }    dfs(d+1,cnt);}int main(){    while(~scanf("%d",&n)&&n)    {        for(int i=0;i<n;i++) scanf("%s",Map[i]);        Max=0;        dfs(0,0);        cout<<Max<<endl;    }    return 0;}

以前,太年轻,总喜欢暴力,现在学了二分图匹配,发现更高效的解法,图为大小50*50都没问题,更何况是这道数据量这么小的题。

二分图最大匹配算法不是问题,用匈牙利算法或者网络流跑一遍就OK了,没学过的话就多看书。。。

这题关键是如何建图。自己好好想想啊,网上各种文章写得很详细,这里就不再赘述了。

下面是二分图的AC代码

#include<iostream>#include<cstdio>#include<vector>using namespace std;const int maxn=15;int girl[maxn],vis[maxn],n,row[maxn][maxn],col[maxn][maxn];int cnt1,cnt2;char Map[maxn][maxn];vector<int>G[maxn];void build_map(){    cnt1=1,cnt2=1;    fill(&row[0][0],&row[maxn][0],0);    fill(&col[0][0],&col[maxn][0],0);    for(int i=0;i<n;i++)        for(int j=0;j<n;j++)        {            if(Map[i][j]=='.'&&row[i][j]==0)            {                for(int k=j;k<n&&Map[i][k]=='.';k++) row[i][k]=cnt1;                cnt1++;            }            if(Map[j][i]=='.'&&col[j][i]==0)            {                for(int k=j;k<n&&Map[k][i]=='.';k++) col[k][i]=cnt2;                cnt2++;            }        }   // for(int i=0;i<n;i++)    //    for(int j=0;j<n;j++)   // {       // if(j==n-1) cout<<row[i][j]<<endl;       // else cout<<row[i][j]<<" ";    //}    for(int i=0;i<=cnt1;i++) G[i].clear();   for(int i=0;i<n;i++)        for(int j=0;j<n;j++)            if(Map[i][j]=='.') G[row[i][j]].push_back(col[i][j]);}bool Find(int x){    for(int i=0;i<G[x].size();i++)    {        int next=G[x][i];        if(!vis[next])        {            vis[next]=1;            if(girl[next]==0||Find(girl[next]))            {                girl[next]=x;                return true;            }        }    }    return false;}int main(){    while(~scanf("%d",&n),n)    {        for(int i=0;i<n;i++) scanf("%s",Map[i]);        build_map();        int ans=0;        fill(girl,girl+maxn,0);        for(int i=1;i<=cnt1;i++)        {            fill(vis,vis+maxn,0);            if(Find(i)) ans++;        }        cout<<ans<<endl;    }    return 0;}



0 0
原创粉丝点击