HDOJ 1045

来源:互联网 发布:软件度量指标 编辑:程序博客网 时间:2024/05/22 12:17

Fire Net

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


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
 
这道题解法很多,LCY把它归到了贪心
我就用贪心写了一遍 ,感觉贪心好牵强啊
#include <iostream>#include <cmath>using namespace std;char a[5][5];int main(){    int  n ,min ,i0 , j0 , num , end , sum ;    while(cin >> n , n)    {        end = 0 ;        num = 0;        for(int i = 0 ; i < n ; i++)        {            for(int j = 0 ; j < n ; j++)            {                cin >> a[i][j];                if(a[i][j] == '.')end++;            }        }        while(end > 0)        {            int flag = 1;            for(int i = 0 ; i < n ; i++)//找当前状态下sum最小的位置            {                for(int j = 0 ; j < n ; j++)                {                    if(a[i][j] == '.')                    {                        sum = 0;                        for(int k = i + 1 ; k < n ; k++)                        {                            if(a[k][j] == '.')sum++;                            else if(a[k][j] == 'X')break;                        }                        for(int k = i - 1 ; k >= 0 ; k--)                        {                            if(a[k][j] == '.')sum++;                            else if(a[k][j] == 'X')break;                        }                        for(int k = j + 1 ; k < n ; k++)                        {                            if(a[i][k] == '.')sum++;                            else if(a[i][k] == 'X')break;                        }                        for(int k = j - 1 ; k >= 0 ; k--)                        {                            if(a[i][k] == '.')sum++;                            else if(a[i][k] == 'X')break;                        }                        if(flag == 1||sum < min)                        {                            min = sum;                            i0 = i;                            j0 = j;                            flag = 0;                        }                    }                }            }            for(int k = i0 ; k < n ; k++)//对i0 , j0自身及上下左右进行修改            {                if(a[k][j0] == 'X')break;                else a[k][j0] = '*';            }            for(int k = j0 ; k < n ; k++)            {                if(a[i0][k] == 'X')break;                else a[i0][k] = '*';            }            for(int k = i0 - 1 ; k >= 0 ; k--)            {                if(a[k][j0] == 'X')break;                else a[k][j0] = '*';            }            for(int k = j0 - 1 ; k >= 0 ; k--)            {                if(a[i0][k] == 'X')break;                else a[i0][k] = '*';            }            num++;            end -= min + 1;        }        cout << num << endl;    }    return 0;}
总体思路是每次找当前状态下是原状态改变最小的解,使得改变次数最大即能放下更多blockhouses 
网上有的大神用DFS 50行搞定,直接拷过来膜拜
#include <stdio.h>char maze[6][6];int num,result;bool placeable(int row,int col){    int i;    i=col-1;    while(i>=0&&maze[row][i]!='X')        if(maze[row][i--]=='*') return false;    i=row-1;    while(i>=0&&maze[i][col]!='X')        if(maze[i--][col]=='*') return false;    return true;}void dfs(int cnt,int max){    int row,col;    if(cnt==num*num)    {        max>result?result=max:1;        return;    }    else    {        row=cnt/num;        col=cnt%num;        if(maze[row][col]=='.'&&placeable(row,col))        {            maze[row][col]='*';            dfs(cnt+1,max+1);            maze[row][col]='.';        }        dfs(cnt+1,max);    }}int main(){    //freopen("Fire Net.txt","r",stdin);    while(scanf("%d",&num)!=EOF&&num)    {        for(int i=0;i<num;i++)            scanf("%s",maze[i]);        result=0;        dfs(0,0);        printf("%d\n",result);    }    return 0;}
源自http://www.cnblogs.com/AdaByron/archive/2011/10/10/2205525.html
DFS简单 , 粗暴 , 好理解。
还有的大神直接二分图
#include <stdio.h>#include <string.h>#define N 16char map[5][5];int idx[5][5],idy[5][5];int n;int g[N][N];int x[N],y[N],cnt1,cnt2;int vis[N];int path(int u){    for(int v=1;v<=cnt2;v++)    if(g[u][v] && !vis[v])    {        vis[v]=1;        if(y[v]==-1 || path(y[v]))        {            x[u]=v;            y[v]=u;            return 1;        }    }    return 0;}int maxmatch(){    int i,ret=0;    memset(x,-1,sizeof(x));    memset(y,-1,sizeof(y));    for(i=1;i<=cnt1;i++)    {        memset(vis,0,sizeof(vis));        if(x[i]==-1)    ret+=path(i);    }    return ret;}void build(){    int i,j;    cnt1=0;    for(i=0;i<n;i++)    {        for(j=0;j<n;j++)    if(map[i][j]=='.')        {            if(j==0 || map[i][j-1]!='.')    cnt1++;            idx[i][j]=cnt1;        }    }    cnt2=0;    for(j=0;j<n;j++)    {        for(i=0;i<n;i++)    if(map[i][j]=='.')        {            if(i==0 || map[i-1][j]!='.')    cnt2++;            idy[i][j]=cnt2;        }    }    memset(g,0,sizeof(g));    for(i=0;i<n;i++)    {        for(j=0;j<n;j++)    if(map[i][j]=='.')  g[idx[i][j]][idy[i][j]]=1;    }}int main(){    int i;    while(scanf("%d",&n),n)    {        for(i=0;i<n;i++)    scanf("%s",map[i]);        build();        printf("%d\n",maxmatch());    }    return 0;}
源自http://www.cnblogs.com/algorithms/archive/2012/07/30/2615832.html
这个我完全不懂 , 图论学得微垃圾
总结一下,这题我认为最好的解法是DFS , 贪心的话比较难想 , 二分图理解的话貌似也不难写。这种解法千奇百怪的题人家最喜欢了害羞