AcWing 1113. 红与黑dfs版本
原题链接
简单
作者:
hai阿卢
,
2021-03-11 21:55:45
,
所有人可见
,
阅读 324
#include<iostream>
#include<cstring>
using namespace std;
const int N = 25;
char a[N][N];
bool s[N][N];
int n, m;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int dfs(int x, int y, int res)
{
s[x][y] = true;
res++;
for(int i = 0; i < 4; i++)
{
int xi = x + dx[i];
int yi = y + dy[i];
if(xi > 0 && yi > 0 && xi <= n && yi <= m && !s[xi][yi] && a[xi][yi] == '.')
{
// cout << xi << " " << yi << endl;
res += dfs(xi, yi, 0);
}
}
return res;
}
int main()
{
cin >> m >> n;
while(n && m)
{
int x, y;
memset(s, 0, sizeof(s));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j] == '@') x = i, y = j;
}
}
// cout << x << " " << y << endl;
cout << dfs(x, y, 0) << endl;
cin >> m >> n;
}
return 0;
}