Fire Net

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


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  


Source Zhejiang University Local Contest 2001  

HDUOJ---------(1045)Fire Net_搜索专题HDUOJ---------(1045)Fire Net_java_02

1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define maxn 10
5 char maze[maxn][maxn];
6 int ans,n,count,max;
7 /*用p作为人的标记*/
8 /*判断竖横是否有城堡*/
9 bool judge(int x,int y)
10 {
11 int i;
12 bool flag=true;
13 for(i=y+1;i<=n;i++)
14 {
15 if(maze[x][i]=='P')
16 {
17 flag=false;
18 break;
19 }
20 else
21 if(maze[x][i]=='X')
22 break;
23 }
24 if(!flag)
25 return false;
26 for(i=y-1;i>=1;i--)
27 {
28 if(maze[x][i]=='P')
29 {
30 flag=false;
31 break;
32 }
33 else
34 if(maze[x][i]=='X')
35 break;
36 }
37 if(!flag)
38 return false;
39 for(i=x+1;i<=n;i++)
40 {
41 if(maze[i][y]=='P')
42 {
43 flag=false;
44 break;
45 }
46 else if(maze[i][y]=='X')
47 break;
48 }
49 if(!flag)
50 return false;
51 for(i=x-1;i>=1;i--)
52 {
53 if(maze[i][y]=='P')
54 {
55 flag=false;
56 break;
57 }
58 else if(maze[i][y]=='X')
59 break;
60 }
61 if(!flag)
62 return false;
63 else
64 return true;
65 }
66 void dfs()
67 {
68 for(int i=1;i<=n;i++)
69 {
70 for(int j=1;j<=n;j++)
71 {
72 if(maze[i][j]=='.'&&true==judge(i,j))
73 {
74 count++;
75 maze[i][j]='P';
76 dfs();
77 if(count>ans)
78 ans=count;
79 maze[i][j]='.';
80 count--;
81 }
82 }
83 }
84 }
85 int main()
86 {
87 int i,j;
88 while(scanf("%d",&n),n)
89 {
90 getchar();
91 max=0;
92 for( i=1;i<=n;i++)
93 {
94 for( j=1;j<=n;j++)
95 {
96 scanf("%c",&maze[i][j]);
97 }
98 getchar();
99 }
100 for(i=1;i<=n;i++)
101 {
102 for(j=1;j<=n;j++)
103 {
104 if(maze[i][j]!='X')
105 {
106 count=1;
107 ans=0;
108 maze[i][j]='P';
109 dfs();
110 maze[i][j]='.';
111 if(ans==0) max=1;
112 else
113 if(ans>max) max=ans;
114 }
115 }
116 }
117 printf("%d\n",max);
118 }
119 return 0;
120 }

View Code

思路:   每次涂一个点,然后生成一张图,将其作为一张新图,又重新开始涂点.....周而复始.....

并不断记录点的最大个数.用栈,或者回溯都可以实现.....


编程是一种快乐,享受代码带给我的乐趣!!!