hdu 1045 Fire Net

来源:互联网 发布:永硕e盘 易语言 源码 编辑:程序博客网 时间:2024/05/16 09:36



Fire Net
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1045

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

//爆搜从第一个点搜到最后一个点#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]=='#') return false;        if(map[i][col]=='X') break;    }    for(j=col; j>=0; j--)    {        if(map[row][j]=='#') 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]='#';            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;}//二分匹配////这题是把原始图分别按行和列缩点////建图:横竖分区。先看每一列,同一列相连的空地同时看成一个点,显然这////样的区域不能够同时放两个点。这些点作为二分图的X部。同理在对所有的////行用相同的方法缩点,作为Y部.//////连边的条件是两个区域有相交部分(即'.'的地方)。最后求最大匹配就是答案。////#include<queue>//#include<stack>//#include<math.h>//#include<stdio.h>//#include<iostream>//#include<stdlib.h>//#include<string.h>//#include<algorithm>//using namespace std;////int x, y;//int row[8][8],col[8][8],a[8],b[8];                               //char map[8][8];//bool path[8][8],vis[8];////int dfs(int cur)//{//    for(int i = 0; i < y; ++i)//    {//        //cur到i有路径 且 i没遍历过//        if(path[cur][i] && !vis[i])//        {//            vis[i] = 1;//            if(!a[i] || dfs(a[i]))//若 i 还没匹配过或 跟i//            {//                //匹配的点找到另一个相匹配的点(则i 就可以跟cur匹配)//                a[i] = cur;//                b[cur] = i;//                return 1;//            }//        }//    }//    return 0;//}////int maxmatch()//{//    int ans = 0;//    memset(b, 0, sizeof(b));//    memset(a, 0, sizeof(a));//    for(int i = 0; i < x; ++i)//    {//        if(b[i] == 0)//        {//            memset(vis, 0, sizeof(vis));//            ans += dfs(i);//        }//    }//    return ans;//}////int main()//{//    int n;//    while(scanf("%d%*c", &n), n)//    {//        for(int i = 0; i < n; ++i)//        {//            for(int j = 0; j < n; ++j)//                map[i][j] = getchar();//            getchar();//        }////        memset(row, 0, sizeof(row));//        memset(col, 0, sizeof(col));//        x = y = 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; map[i][k] == '.' && k < n; ++k)//                        row[i][k] = x;  //给相同的区域标记同一个数字//                    x++;//                }//////                if(map[j][i] == '.' && col[j][i] == 0)//                {//                    //纵向缩点//                    for(int k = j; map[k][i] == '.' && k < n; ++k)//                        col[k][i] = y;  //给相同的区域标记同一个数字//                    y++;//                }//            }//        }////        memset(path, 0, sizeof(path));//        for(int i = 0; i < n; ++i)//            for(int j = 0; j < n; ++j)//                if(map[i][j] == '.')    //连边,'.'的地方即为缩点后的 行和列的交点//                    path[ row[i][j] ][ col[i][j] ] = 1;////        printf("%d\n", maxmatch());  //二分图匹配//    }//    return 0;//}














0 0
原创粉丝点击