ACM学习之路__HDU 1045

              Fire Net

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
   
  //第一次写,没啥经验^_^,刚开始看到题的时候,啊西八,好长的题,还是英文,还有图,一下就懵逼了,仔细一看最大也就4*4的数据量,一下就有了兴趣怼它,正好集训时把它放在搜索里,简单BFS。
  直接上代码
 1 #include <iostream>
 2   #include <cstdio>
 3 
 4   using namespace std;
 5 
 6   char str[5][5];
 7   int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};// up down left right 四个方向
 8   int ans,n;
 9   int judge(int r,int c)// 判断当前位置能否放炮台
10   {
11     int i;  
12     bool flag=1;//默认当前位置符合条件
13     for(i=0;i<4;i++)//对当前位置,跑完它的四个方位
14     {
15       int rr,cc;
16       rr=r;
17       cc=c;
18       while(1)
19       {
20         rr=rr+dir[i][0];
21         cc=cc+dir[i][1];
22         if(rr<1 || cc<1 || rr>n || cc>n)//当到达边界时跳出
23           break;
24         else if( str[rr][cc] == '@')
25           {
26             flag=0;
27             break;
28           }//当前位置所在行,列已有炮台,不符合条件
29           else
30             {
31               if(str[cc][rr] == 'X')
32               break;
33             }//遇到碉堡,跳出
34       }//当前位置是否符合要求
35   }
36   return flag;
37 }
38 void dfs(int res)
39 {
40   for(int i=1 ; i<=n ; i++)
41   {
42     for(int j=1 ; j<=n ; j++)
43     {
44       if(str[i][j]=='.' && judge(i,j))
45       {
46         str[i][j]='@';//当前位置是空地,且可以放炮台
47         dfs(res+1);//当前位置符合要求,进行下一层搜索
48         str[i][j]='.';//记得还原
49       }
50     }
51   }
52       if(res>ans)
53       ans=res;//当前最优解
54 return;
55 }
56 int main()
57 {
58   int i,j;
59   while(~scanf("%d%*c",&n) && n)
60   {
61     ans=0;
62     for(i=1 ; i<=n ; i++)
63     {
64       for(j=1 ; j<=n ; j++)
65       cin>>str[i][j];
66     }
67   dfs(0);
68   cout<<ans<<endl;
69   }
70 return 0;
71 }
 

  

原文地址:https://www.cnblogs.com/x-1204729564/p/5676015.html