No.2 POJ1562
DFS// ShellDawn// 2019-07-10// POJ1562#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#define MM(x) memset(x,0,sizeof(x));using namespace std;#...
·
DFS
// ShellDawn
// 2019-07-10
// POJ1562
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define MM(x) memset(x,0,sizeof(x));
using namespace std;
#define maxn 105
int n,m;
char E[maxn][maxn];
int ans;
const int dirx[] = {-1,-1,-1,0,1,1,1,0};
const int diry[] = {-1,0,1,1,1,0,-1,-1};
void dfs(int x,int y){
E[x][y] = 0;
for(int i=0;i<8;++i){
int xx = x+dirx[i];
int yy = y+diry[i];
if(E[xx][yy] == '@'){
dfs(xx,yy);
}
}
}
int main(){
//freopen("LeetCodeIn.txt","r",stdin);
while(scanf("%d%d",&n,&m)&&n!=0){
MM(E);
ans = 0;
for(int i=1;i<=n;++i){
scanf("%s",E[i]+1);
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(E[i][j] == '@'){
dfs(i,j);
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
}
更多推荐
已为社区贡献4条内容
所有评论(0)