hdu 2952 Counting Sheep

本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952

题意:上下左右4个方向为一群。搜索有几群羊

 1 #include <stdio.h>
 2 #include<string.h>
 3 char graph[101][101];
 4 int w,h;
 5 int tab[4][2]={1,0,0,1,-1,0,0,-1};
 6 
 7 void dfs(int x,int y)
 8 {
 9     graph[x][y]='.';
10     for(int i=0;i<4;i++)
11     {
12         int xx=x+tab[i][0];
13         int yy=y+tab[i][1];
14         if(0<=xx&&xx<h&&0<=yy&&yy<w&&graph[xx][yy]=='#')
15             dfs(xx,yy);
16     }
17 }
18 
19 void solve()
20 {
21     int ans=0;
22     for(int i=0;i<h;i++)
23         for(int j=0;j<w;j++)
24             if(graph[i][j]=='#')
25             {
26                 dfs(i,j);
27                 ans++;
28             }
29     printf("%d
",ans);
30 }
31 int main()
32 {
33     int T;
34     scanf("%d",&T);
35     while(T--)
36     {
37         memset(graph,'',sizeof(graph));
38         scanf("%d%d",&h,&w);
39         getchar();
40         // 输入 
41         for(int i=0;i<h;i++)
42         {
43             for(int j=0;j<w;j++)
44                 scanf("%c",&graph[i][j]);
45             getchar();
46         }
47         solve();               // 计算 
48     }
49     return 0;
50 }
原文地址:https://www.cnblogs.com/orange1438/p/3517239.html