标签:
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=27
#include <stdio.h> #include <string.h> int t; int r,c; int maps[105][105]; int color[105][105]; int to[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; void dfs(int i,int j) { if(maps[i][j]==1&&color[i][j]==0) { color[i][j]=1; for(int k=0; k<4; k++) { int tx=i+to[k][0]; int ty=j+to[k][1]; if(tx>-1&&tx<r&&ty>-1&&ty<c) { dfs(tx,ty); } } } } int main() { scanf("%d",&t); while(t--) { memset(maps,0,sizeof(maps)); memset(color,0,sizeof(color)); scanf("%d%d",&r,&c); for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { scanf("%d",&maps[i][j]); } } int Count=0; for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(color[i][j]==0&&maps[i][j]==1) { Count++; dfs(i,j); } } } printf("%d\n",Count); } return 0; }
标签:
原文地址:http://www.cnblogs.com/TreeDream/p/5312433.html