NYOJ--27--dfs--水池数目
/* Name: NYOJ--27--水池数目 Author: shen_渊 Date: 17/04/17 15:42 Description: 经典dfs水题,,, */ #include<iostream> #include<cstring> using namespace std; int m,n,map[105][105],ct; void dfs(int,int); int dir[8][2] = { {0,1}, {0,-1}, {1,0}, {-1,0}, };//4个方向递归 int main() { ios::sync_with_stdio(false); // freopen("in.txt","r",stdin); int N;cin>>N; while(N--) { cin>>m>>n; ct = 0; memset(map,0,sizeof(map)); for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ cin>>map[i][j]; } } for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ if(map[i][j] == 1){ map[i][j] = 0; ct++; dfs(i,j); } } } cout<<ct<<endl; } return 0; } void dfs(int x,int y){ for(int i=0; i<8; ++i){ int a = x + dir[i][0]; int b = y + dir[i][1]; if(a<0 || b<0 || a>=m || b>=n || map[a][b] == 0)continue; map[a][b] = 0; dfs(a,b); } }