码迷,mamicode.com
首页 > 其他好文 > 详细

poj1562DFS

时间:2017-06-08 23:40:01      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:using   getchar   namespace   while   stream   ring   pre   ios   ace   

 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 } 

 

poj1562DFS

标签:using   getchar   namespace   while   stream   ring   pre   ios   ace   

原文地址:http://www.cnblogs.com/--lr/p/6965033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!