首页 > 代码库 > POJ 2386 lake counting DFS

POJ 2386 lake counting DFS

朴素DFS

计算有多少个水坑

TEST

10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.3

  

/* DFS,挑战程序设计竞赛 巫 */#include<cstdio>#include<iostream>using namespace std;string s[110];int m,n;bool inbound(int x,int bound) { return (x>=0&&x<bound)?true:false; }void DFS(int x,int y){    s[x][y]=.;    for(int i=-1;i<=1;++i)        for(int j=-1;j<=1;++j){            int dx=x+i,dy=y+j;            if(inbound(dx,m)&&inbound(dy,n)&&s[dx][dy]==W)                 DFS(dx,dy);        }}int main(){    cin >> m >> n;    for(int i=0;i<m;++i)        cin >> s[i];    int cnt=0;    for(int i=0;i<m;++i)        for(int j=0;j<n;++j)            if(s[i][j]==W) { DFS(i,j); cnt++; }    cout << cnt << endl;    return 0;}

 

POJ 2386 lake counting DFS