poj1562DFS

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 char map[110][110];
 6 int vis[110][110];
 7 int sum,n,m;
 8 int dfs(int x,int y)
 9 {
10      int u[][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
11      int tx,ty,i,j;
12      for(i=0;i<8;i++)
13      {
14          tx=x+u[i][0];
15          ty=y+u[i][1];
16          if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]=='@')
17          {
18              map[tx][ty]='.';
19              dfs(tx,ty);
20         }
21      }
22 }
23 int main()
24 {
25     int i,j;
26     while(cin>>n>>m&&n&&m)
27     {
28         sum=0;
29         memset(map,'.',sizeof(map));
30         for(i=0;i<n;i++)
31         {
32             for(j=0;j<m;j++)
33             cin>>map[i][j];
34             getchar();
35         }
36         for(int k=0;k<n;k++)
37         {
38             for(int h=0;h<m;h++)
39             if(map[k][h]=='@')
40             {
41                 map[k][h]='.';
42                 dfs(k,h);
43                 sum++;
44             }
45         }
46         cout<<sum<<endl;
47     }
48 } 

 

posted @ 2017-06-08 22:04  TTTCoder  阅读(202)  评论(0编辑  收藏  举报