源码如下:
#include <iostream>
#include <cstring>
using namespace std;
int w, h;
int vis[50][50];
char ch;
int sx, sy;
int cnt = 0;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void dfs(int x, int y){
if (vis[x][y] == 0)
cnt++;
for (int i = 0; i < 4; i++){
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 0 && tx < w && ty >= 0 && ty < h && (vis[tx][ty] == 0))
dfs(tx, ty);
}
return ;
}
int main(){
cin >> w >> h;
for (int i = 0; i < h; i++){
for (int j = 0; i < w; j++){
cin >> ch;
if (ch == '@')
sx = i, sy = j;
else if (ch == '#')
vis[i][j] = 1;
}
}
dfs(sx, sy);
cout << cnt;
return 0;
}