AcWing 1113. 红与黑
原题链接
简单
作者:
十六
,
2021-01-18 15:15:49
,
所有人可见
,
阅读 289
#include<bits/stdc++.h>
using namespace std;
const int MAX = 20+10;
int w, h, x, y, ans;
char g[MAX][MAX];
bool v[MAX][MAX];
int d[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
void dfs(int x, int y){
v[x][y] = true;
for(int i=0; i<4; i++){
int a = x+d[i][0], b = y+d[i][1];
if(a<0 || a>=h || b<0 || b>=w) continue;
if(v[a][b]) continue;
if(g[a][b]=='#') continue;
ans++;
dfs(a, b);
}
}
int main(){
while(~scanf("%d%d", &w, &h) && w){
for(int i=0; i<h; i++) scanf("%s", g[i]);
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
if(g[i][j]=='@'){
x = i, y = j;
break;
}
}
}
ans = 1;
memset(v, false, sizeof v);
dfs(x, y);
cout<< ans<< endl;
}
return 0;
}