#include<bits/stdc++.h>
using namespace std;
int w,h,dx[]={0,1,0,-1},dy[]={1,0,-1,0},ans=1,vis[100][100],sx,sy;
char mp[100][100];
void dfs(int x,int y){
if(mp[x][y]=='.'){
return;
}
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(vis[tx][ty]==0&&tx>=1&&tx<=h&&ty>=1&&ty<=w&&mp[tx][ty]=='#'){
vis[tx][ty]=1;
ans++;
dfs(tx,ty);
}
}
}
int main (){
cin>>w>>h;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>mp[i][j];
if(mp[i][j]=='@'){
sx=i,sy=j;
}
if(mp[i][j]=='.'){
vis[i][j]=1;
}
}
}
vis[sx][sy]==1;
dfs(sx,sy);
cout<<ans;
return 0;
}
为什么样例输出41。
各位大佬,我哪里错了??
错误代码,用了洛谷的在线IDE