AcWing 1113. 红与黑
原题链接
简单
作者:
codewin
,
2021-01-12 12:35:14
,
所有人可见
,
阅读 327
C with STL 代码
#include <bits/stdc++.h>
using namespace std;
int w, h, maxn = -1;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1};
string s[25];
bool st[25][25];
int dfs(int x, int y)
{
st[x][y]=1;
int cnt=1;
for (int i = 0; i < 4; i++)
{
int xx = dx[i] + x, yy = dy[i] + y;
if (st[xx][yy]==false&&xx < h && xx >= 0 && yy < w && yy >= 0 && s[xx][yy] == '.')
{
cnt+=dfs(xx, yy);
}
}
return cnt;
}
int main()
{
while (cin >> w >> h&&w||h)
{
memset(st,0,sizeof st);
for (int i = 0; i < h; i++)
{
cin >> s[i];
}
int x,y;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
if (s[i][j] == '@')
x = i, y = j;
}
}
cout<<dfs(x, y)<<endl;
}
return 0;
}