hdu 1312 red and black

秒杀题,不用说了,DFS求出@所在位置黑方块的最大连通区域

AC Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <stdlib.h>
#include <string.h>
int W,H;
int sum;
char map[50][50];
int find[50][50];
typedef struct{ int x,y;} node;
int iPlus[4] = {-1,0,1,0};
int jPlus[4] = {0,-1,0,1};
void (node p)
{
int k;
find[p.x][p.y] = 1;
node next;
sum++;
for ( k = 0; k < 4; k++)
{
next.x = p.x + iPlus[k];
next.y = p.y + jPlus[k];
if ( next.x >= 1 && next.x <= H && next.y >= 1 && next.y <= W)
{
if ( map[next.x][next.y]=='.' && !find[next.x][next.y] )
DFS(next);
}
}
}
int main()
{
freopen("Sample Input.txt","r",stdin);
system("Color F0");
int i,j,k;char ch;
node stand;
while ( scanf("%d%d",&W,&H),W&&H)
{
getchar();
for ( i = 1; i <= H; i++)
{
for ( j = 1; j <= W; j++)
{
scanf("%c",&ch);
if ( ch=='@') { stand.x = i; stand.y = j;}
map[i][j] = ch;
}
getchar();
}
sum = 0;
memset(find,0,sizeof(find));
DFS(stand);
printf("%dn",sum);
}
getch();
return 0;
}