Fire Net(hdu1045)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10462 Accepted Submission(s): 6154
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
Sample Output
5
1
5
2
4
//题意是一个地图,要在城市放尽可能多的碉堡,但是碉堡不能在同一行,或者同一列,在同一行必须有个坚固的墙
问最多可以放多少碉堡
//简单的DFS的应用,代码里写的很详细了。。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 char mp[4][4]; 7 int vis[4][4]; 8 int n; 9 int ans; 10 11 void fang(int x,int y)//放下 12 { 13 vis[x][y]=1; 14 int i; 15 i=x; 16 while (i+1<n&&mp[i+1][y]=='.') 17 { 18 i++; 19 vis[i][y]++; 20 } 21 i=x; 22 while (i-1>=0&&mp[i-1][y]=='.') 23 { 24 i--; 25 vis[i][y]++; 26 } 27 i=y; 28 while (i+1<n&&mp[x][i+1]=='.') 29 { 30 i++; 31 vis[x][i]++; 32 } 33 i=y; 34 while (i-1>=0&&mp[x][i-1]=='.') 35 { 36 i--; 37 vis[x][i]++; 38 } 39 } 40 41 void che(int x,int y)//撤走 42 { 43 vis[x][y]=0; 44 int i; 45 i=x; 46 while (i+1<n&&mp[i+1][y]=='.') 47 { 48 i++; 49 vis[i][y]--; 50 } 51 i=x; 52 while (i-1>=0&&mp[i-1][y]=='.') 53 { 54 i--; 55 vis[i][y]--; 56 } 57 i=y; 58 while (i+1<n&&mp[x][i+1]=='.') 59 { 60 i++; 61 vis[x][i]--; 62 } 63 i=y; 64 while (i-1>=0&&mp[x][i-1]=='.') 65 { 66 i--; 67 vis[x][i]--; 68 } 69 } 70 71 int dfs(int l,int cnt)//去l行放置 72 { 73 if (cnt>ans) 74 { 75 ans=cnt; 76 } 77 if (l>=n) return 0; //出界 78 79 int i; 80 for (i=0;i<n;i++) 81 { 82 if (mp[l][i]=='.'&&vis[l][i]==0) 83 { 84 fang (l,i); 85 dfs(l,cnt+1); //继续在这行放置,因为可能还可以放 86 che(l,i); 87 } 88 } 89 dfs(l+1,cnt); //这一行都放完了就去下一行 90 return 0; 91 } 92 93 int main() 94 { 95 while (cin>>n&&n) 96 { 97 getchar(); 98 int i,j; 99 for (i=0;i<n;i++) 100 { 101 for (j=0;j<n;j++) 102 cin>>mp[i][j]; 103 getchar(); 104 } 105 ans=0; 106 memset(vis,0,sizeof(vis)); 107 dfs(0,0); //从第0行开始放,放置的个数初始值为 0 108 cout<<ans<<endl; 109 } 110 return 0; 111 }