首页 > 其他 > 详细

hdu 2952 Counting Sheep

时间:2014-01-16 00:20:16      阅读:460      评论:0      收藏:0      [点我收藏+]

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

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

 

bubuko.com,布布扣
 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\n",ans);
30 }
31 int main()
32 {
33     int T;
34     scanf("%d",&T);
35     while(T--)
36     {
37         memset(graph,\0,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 }
bubuko.com,布布扣

hdu 2952 Counting Sheep

原文:http://www.cnblogs.com/orange1438/p/3517239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!