HDU1045 Fire Net

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 char a[6][6];
 5 int v[6][6],hash[6][6];//V标记是否已经放过,hash标记是否此位置是否进入过队列
 6 int n,sum,max;
 7 
 8 struct N
 9 {
10     int x,y;
11 };
12 
13 
14 int judge(int x,int y)
15 {
16     int i,j;
17     for(i = x+1; i <= n; i++)
18         if(v[i][y]) return 0;
19         else if(a[i][y] == 'X') break;
20     for(i = x-1; i >= 1; i--)
21         if(v[i][y]) return 0;
22         else if(a[i][y] == 'X') break;
23     for(i = y+1; i <= n; i++)
24         if(v[x][i]) return 0;
25         else if(a[y][i] == 'X') break;
26     for(i = y-1; i >= n; i--)
27         if(v[x][i]) return 0;
28         else if(a[x][i] == 'X') break;
29     return 1;
30 }
31 
32 void bfs(int x,int y)
33 {
34     int i,j;
35     sum = 0;
36     struct N q[5000],t1,t;
37     q[0].x = x;
38     q[0].y = y;
39     int s = 0,e = 1;
40     int fx[] = {-1,-1, 1, 1};
41     int fy[] = { 1,-1,-1, 1};
42     while(s != e)
43     {
44         t.x = q[s].x;
45         t.y = q[s].y;
46         s++;
47         if(a[t.x][t.y] == '.' && !v[t.x][t.y])
48         {
49             if(judge(t.x,t.y))
50             {
51                 v[t.x][t.y] = 1;
52                 sum++;
53             }
54         }
55         for(i = 0;i < 4; i++)
56         {
57             t1.x = t.x + fx[i];
58             t1.y = t.y + fy[i];
59             if(t1.x >= 1 && t1.x <= n && t1.y >= 1 && t1.y <= n && !hash[t1.x][t1.y])
60             {
61                 hash[t1.x][t1.y] = 1;
62                 q[e].x = t1.x;
63                 q[e].y = t1.y;
64                 e++;
65             }
66         }
67         e %= 5000;
68         s %= 5000;
69     }
70     if(max < sum) 
71     {
72         
73         max = sum;
74     }
75 }
76 
77 int main()
78 {
79     int i,j;
80     while(scanf("%d%*c",&n) != EOF && n)
81     {
82         max = 0;
83         for(i = 1;i <= n; i++)
84             scanf("%s",a[i]+1);
85         for(i = 1;i <= n; i++)
86             for(j = 1;j <= n; j++)
87             {
88                 memset(v,0,sizeof(v));
89                 memset(hash,0,sizeof(hash));
90                 if(a[i][j] == '.')
91                 {
92                     bfs(i,j);
93                 }
94             }
95         printf("%d\n",max);
96     }
97     return 0;
98 }
原文地址:https://www.cnblogs.com/zmx354/p/3013580.html