HDU 1045 Fire Net(dfs)
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.
![](http://acm.hdu.edu.cn/data/images/1045-1.jpg)
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.
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.
![](http://acm.hdu.edu.cn/data/images/1045-1.jpg)
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
跟八皇后问题类似,就是多了一个差边界时看看有没有墙能不能放
找了好久的错,后来发现,把输入发地图数组定义成了整型,哎
1 #include<stdio.h> 2 #include<string.h> 3 int n; 4 char Map[10][10]; 5 int ans; 6 7 bool check(int n,int m){ 8 for(int i=n-1;i>=0;i--){//一行一行往前搜 9 if(Map[i][m]=='O') 10 return 0;//在遇到墙前先遇到碉堡,不能放 11 if(Map[i][m]=='X') 12 break;//行前有墙,行符合 13 } 14 for(int j=m-1;j>=0;j--){ 15 if(Map[n][j]=='O') 16 return 0;//同理 17 if(Map[n][j]=='X') 18 break; 19 } 20 return 1; 21 } 22 23 void dfs(int k,int cnt){ 24 int c,l; 25 if(k==n*n){ 26 if(cnt>ans) 27 ans=cnt; 28 return; 29 } 30 else{ 31 c=k/n;//行数 32 l=k%n;//列数 33 if(Map[c][l]=='.'&&check(c,l)){ 34 Map[c][l]='O';//标记放了碉堡 35 dfs(k+1,cnt+1);//这格放了,继续搜 36 Map[c][l]='.'; 37 } 38 dfs(k+1,cnt);//这格不放,继续搜 39 } 40 } 41 42 int main(){ 43 while(scanf("%d",&n)!=EOF&&n){ 44 ans=0; 45 for(int i=0;i<n;i++){ 46 scanf("%s",Map[i]); 47 } 48 dfs(0,0); 49 printf("%d\n",ans); 50 } 51 }
(今年最後の日)
posted on 2018-12-31 09:09 甜甜圈不懂巧克力的苦 阅读(107) 评论(0) 编辑 收藏 举报