智障错误,
void dfs(int x,int y){
map[x][y]=false;
int nx,ny;
for(int i=0;i<8;i++){
nx=x+tx[i],ny=y+ty[i];
if(map[nx][ny])
dfs(nx,ny);
}
}
dfs中参数用了i,j,导致总是做不出,要注意函数参数应该有意义,如果用i,j,很容易for循环就炸掉了
#include <iostream>
using namespace std;
const int maxn=120;
bool map[maxn][maxn];
int N,M;
int cnt;
int tx[]={-1,-1,-1,0,0,1,1,1};
int ty[]={-1,0,1,-1,1,-1,0,1};
void dfs(int x,int y){
map[x][y]=false;
int nx,ny;
for(int i=0;i<8;i++){
nx=x+tx[i],ny=y+ty[i];
if(map[nx][ny])
dfs(nx,ny);
}
}
int main(int argc, char const *argv[])
{
cin>>N>>M;
char in;
cnt=0;
memset(map,false,sizeof(map));
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
cin>>in;
if(in=='W') map[i][j]=true;
else map[i][j]=false;
}
}
// for(int i=1;i<=N;i++){
// for(int j=1;j<=M;j++){
// if(map[i][j]){
// cout<<1;
// }
// else cout<<0;
// cout<<" ";
// }
// cout<<endl;
// }
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
if(map[i][j]){
dfs(i,j);
cnt++;
// for(int i=1;i<=N;i++){
// for(int j=1;j<=M;j++){
// if(map[i][j]){
// cout<<1;
// }
// else cout<<0;
// cout<<" ";
// }
// cout<<endl;
// }
// cout<<endl;
}
}
}
cout<<cnt<<endl;
return 0;
}