当前位置: 代码迷 >> 综合 >> (DFS八连通)POJ2386 Lake Counting
  详细解决方案

(DFS八连通)POJ2386 Lake Counting

热度:97   发布时间:2023-11-02 22:32:48.0

POJ 2386   P32 挑战程序设计

#include<cstdio>
using namespace std;#define MAX_N 210
char field[MAX_N][MAX_N];
int n,m;void dfs(int x,int y){field[x][y]='.';for(int dx=-1;dx<=1;dx++){for(int dy=-1;dy<=1;dy++){int nx=x+dx,ny=y+dy;if(nx>=0&&nx<n&&ny>=0&&ny<m&&field[nx][ny]=='W') dfs(nx,ny);}}return ;
}void solve(){int res=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(field[i][j]=='W'){dfs(i,j);res++;}}}printf("%d\n",res);
}int main(){scanf("%d%d",&n,&m);for(int i=0;i<n;i++){scanf("%s",field[i]);}solve();return 0;
}