Hdu

上题目:

Fire Net

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


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
 
  题意和8王后问题有点像,就是往棋盘上放火力点,火力点可以向四周直线攻击,但攻击不能穿过墙壁,问最大4*4的格子里面可以放多少个火力点,棋盘的大小1*1~4*4,地图数据输入。
  由于数据量很少,直接暴力就可以过,但是因为很少做这种太凶残太暴力的题目,所以还是写一篇题解留着。
  枚举任何的点作为起点(反正数据不大),往上面放火力点,然后判定,这是图是否合法,如果合法就继续递归。这里需要用一个全局变量来保存每一次枚举起点以后得到的最终火力点个数,然后保留最大值。
 
上代码:
  1 #include <stdio.h>
  2 #include <string.h>
  3 #define MAX 10
  4 using namespace std;
  5 
  6 int s[MAX][MAX],max;
  7 
  8 bool jadge2(int n)
  9 {
 10     int i,j,a;
 11     for(i=1;i<=n;i++)
 12     {
 13         a=0;
 14         for(j=1;j<=n;j++)
 15         {
 16             if(s[i][j]==1)
 17             {
 18                 a++;
 19                 if(a>1) return 0;
 20             }
 21             else if(s[i][j]==-1) a=0;
 22         }
 23     }
 24     for(j=1;j<=n;j++)
 25     {
 26         a=0;
 27         for(i=1;i<=n;i++)
 28         {
 29             if(s[i][j]==1)
 30             {
 31                 a++;
 32                 if(a>1) return 0;
 33             }
 34             else if(s[i][j]==-1) a=0;
 35         }
 36     }
 37     return 1;
 38 }
 39 
 40 
 41 void jadge(int n,int count)
 42 {
 43     int i,j;
 44     for(i=1;i<=n;i++)
 45     {
 46         for(j=1;j<=n;j++)
 47         {
 48             if(s[i][j]) continue;
 49             s[i][j]=1;
 50             if(jadge2(n))
 51             {
 52                 max=count+1 > max ? count+1 : max;
 53                 jadge(n,count+1);
 54             }
 55             s[i][j]=0;
 56         }
 57     }
 58 }
 59 
 60 
 61 int main()
 62 {
 63     int n,i,j;
 64     char c,o;
 65     //freopen("data.txt","r",stdin);
 66     while(scanf("%d",&n),n)
 67     {
 68         scanf("%c",&o);
 69         memset(s,-1,sizeof(s));
 70         for(i=1;i<=n;i++)
 71         {
 72             j=1;
 73             while((c=getchar()),(c=='.' || c=='X'))
 74             {
 75                 if(c=='.') s[i][j]=0;
 76                 else s[i][j]=-1;
 77                 j++;
 78             }
 79         }
 80         /*
 81         for(i=1;i<=n;i++)
 82         {
 83             for(j=1;j<=n;j++)
 84             {
 85                 printf("%d",s[i][j]);
 86             }
 87             printf("
");
 88         }
 89         */
 90         max=0;
 91         for(i=1;i<=n;i++)
 92         {
 93             for(j=1;j<=n;j++)
 94             {
 95                 jadge(n,0);
 96             }
 97         }
 98         printf("%d
",max);
 99     }
100     return 0;
101 }
1045
 
 
 
原文地址:https://www.cnblogs.com/sineatos/p/3203172.html