水池数目(nyoj27)(BFS)

2015-01-27 06:10:23 · 作者: · 浏览: 8

水池数目

时间限制:3000 ms | 内存限制:65535 KB 难度:4
描述
南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池。
输入
第一行输入一个整数N,表示共有N组测试数据
每一组数据都是先输入该地图的行数m(0 输出
输出该地图中水池的个数。
要注意,每个水池的旁边(上下左右四个位置)如果还是水池的话的话,它们可以看做是同一个水池。
样例输入
2
3 4
1 0 0 0 
0 0 1 1
1 1 1 0
5 5
1 1 1 1 0
0 0 1 0 1
0 0 0 0 0
1 1 1 0 0
0 0 1 1 1
样例输出
2
3
来源
[张云聪]原创
上传者

张云聪

#include
      
       
#include
       
         int s[105][105]; int a[4]={0,0,1,-1};//向四个方向搜索。 int b[4]={1,-1,0,0}; void bfs(int x,int y) { int k,v,t; s[x][y]=0; for(k=0;k<4;k++) { v=x+a[k]; t=y+b[k]; if(s[v][t]) { bfs(v,t); } } } int main() { int test,n,m,i,j,ans; scanf("%d",&test); while(test--) { scanf("%d%d",&m,&n); memset(s,0,sizeof(s)); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) scanf("%d",&s[i][j]); } for(i=1,ans=0;i<=m;i++) { for(j=1;j<=n;j++) if(s[i][j]) { bfs(i,j); ans++; } } printf("%d\n",ans); } return 0; }
       
      

<script type="text/java script">
<script type="text/java script">BAIDU_CLB_fillSlot("771048");
点击复制链接 与好友分享! 回本站首页
<script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];