AcWing 1113. 红与黑
原题链接
简单
作者:
自豪的澡巾QAQ
,
2021-02-15 12:37:09
,
所有人可见
,
阅读 267
总体写的还算简洁明了
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
const int N = 30;
char arr[N][N];
int vis[N][N];
int ans = 0;
int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
void dfs(int x,int y,int n,int m) {
if (x > m || x < 1 || y > n || y < 1 || vis[x][y] || arr[x][y] == '#') return;
ans++;
vis[x][y] = 1;
for (int i = 0;i < 4;i++) {
dfs(x + dx[i],y + dy[i],n,m);
}
return;
}
int main() {
int n,m;
cin >> n >> m;
while (n != 0 && m != 0) {
ans = 0;
memset(vis,0,sizeof(int) * N * N);
int start_x,start_y;
for (int i = 1;i <= m;i++) {
for (int j = 1;j <= n;j++) {
cin >> arr[i][j];
if (arr[i][j] == '@') {
start_x = i;
start_y = j;
}
}
}
dfs(start_x,start_y,n,m);
cout << ans << endl;
cin >> n >> m;
}
return 0;
}