UVA 572 - Oil Deposits(简单DFS)

题目链接

神似白书上的“八连块”,UVA上难得的1Y。

 1 #include <stdio.h>

 2 #include <string.h>

 3 char map[101][101];

 4 int key[101][101];

 5 int a[9]={0,0,1,-1,1,1,-1,-1};

 6 int b[9]={1,-1,0,0,1,-1,1,-1};

 7 int m,n;

 8 void dfs(int x,int y)

 9 {

10     int i;

11     key[x][y] = 1;

12     for(i = 0;i <= 7;i ++)

13     {

14         if(x+a[i]>=0&&x+a[i]<=m-1&&y+b[i]>=0&&y+b[i]<=n-1)

15         {

16             if(map[x+a[i]][y+b[i]] == '@'&&!key[x+a[i]][y+b[i]])

17             dfs(x+a[i],y+b[i]);

18         }

19     }

20     return ;

21 }

22 int main()

23 {

24     int i,j,num;

25     while(scanf("%d%d%*c",&m,&n)!=EOF)

26     {

27         memset(key,0,sizeof(key));

28         if(m == 0&&n == 0) break;

29         num = 0;

30         for(i = 0;i <= m-1;i ++)

31         gets(map[i]);

32         for(i = 0;i <= m-1;i ++)

33         for(j = 0;j <= n-1;j ++)

34         {

35             if(!key[i][j] && map[i][j] == '@')

36             {

37                 dfs(i,j);

38                 num ++;

39             }

40         }

41         printf("%d\n",num);

42     }

43     return 0;

44 }

你可能感兴趣的:(DFS)