ZOJ 1002 Fire Net

Fire Net

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.

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.

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

 思路:

  放在1002也是蛮尴尬的。花了一些时间把它写了出来顺便复习了递归。纯粹的暴力搜索,遇到‘.’判断其是否可以放置,如果可以那么就将其放置上去递归,退出递归后将其取出递归。如果该位置不能放置,将指针移到下一个保存现有的状态递归。遇到‘X’直接下一个递归。大体上就是DFS的思路,返回值为当前的最大值。

  值得思考的是见过一些类似的题可以用状压dp解决,等对状压dp有了更深了解再来尝试一下。

扫描二维码关注公众号,回复: 58806 查看本文章

代码:

 1 #include<stdio.h>
 2 
 3 int maxn,c,r;
 4 char data[500][500];
 5 
 6 int cmpmax(int a,int b){
 7     return a>b?a:b;
 8 }
 9 
10 int searchSit(int n,int count){
11     int res = count;
12     if(n == maxn - 1){
13         if(data[r-1][c-1]=='X'){
14             return res;
15         }
16         int sign = 1;
17         for(int i = r-1; i >= 0&& data[i][c-1]!='X';i-- ){
18             if(data[i][c-1] == 'O'){
19                 sign = 0;
20                 break;
21             }
22         }
23         for(int i = c-1; i >= 0&& data[r-1][i]!='X';i-- ){
24             if(data[r-1][i] == 'O'){
25                 sign = 0;
26                 break;
27             }
28         }
29         res += sign;
30         return res;
31 
32     }
33 
34 
35     if(n < maxn - 1&&data[n/c][n%c]!='X'){
36         int sign = 1;
37         for(int i = n/c + 1; i < r&& data[i][n%c]!='X';i++ ){
38             if(data[i][n%c] == 'O'){
39                 sign = 0;
40                 break;
41             }
42         }
43         for(int i = n/c - 1; i >= 0&& data[i][n%c]!='X';i-- ){
44             if(data[i][n%c] == 'O'){
45                 sign = 0;
46                 break;
47             }
48         }
49         for(int i = n%c + 1; i < c&& data[n/c][i]!='X';i++ ){
50             if(data[n/c][i] == 'O'){
51                 sign = 0;
52                 break;
53             }
54         }
55         for(int i = n%c - 1; i >= 0&& data[n/c][i]!='X';i-- ){
56             if(data[n/c][i] == 'O'){
57                 sign = 0;
58                 break;
59             }
60         }
61         if(sign){
62             data[n/c][n%c] = 'O';
63             res = cmpmax(res,searchSit(n+1,count+1));
64             data[n/c][n%c] = '.';
65         }
66 
67     }
68     if(n < maxn - 1){
69         res = cmpmax(res,searchSit(n+1,count));
70     }
71     return res;
72 }
73 
74 int main(){
75 
76     scanf("%d",&c);
77     while(c){
78         r = c;
79         maxn = r*c;
80         for(int i = 0; i < c; i++){
81             scanf("%s",data[i]);
82         }
83         printf("%d\n",searchSit(0,0));
84         scanf("%d",&c);
85     }
86 
87     return 0;
88 }

注:1.C99或C++编译环境下可成功运行。

  2.进行尝试后发现当c = 8时明显感觉到了计算的延迟。

猜你喜欢

转载自www.cnblogs.com/jinjin-2018/p/8932136.html