DFS之连通性模型2:1113
作者:
总打瞌睡的天天啊
,
2024-10-21 19:35:46
,
所有人可见
,
阅读 3
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=22;
char g[N][N];
bool st[N][N];
int res;
int n,m;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void dfs(int x,int y)
{
//cout<<x<<y<<endl;
for(int i=0;i<4;i++)
{
int wx=x+dx[i],wy=y+dy[i];
if(wx<0||wy<0||wx>=n||wy>=m||g[wx][wy]=='#')continue;
if(st[wx][wy])continue;
res++;
st[wx][wy]=true;
//cout<<wx<<' '<<wy<<endl;
dfs(wx,wy);
}
}
int main()
{
while (cin >> m >> n, n || m)
{
res=0;
memset(st, 0, sizeof st);
for (int i = 0; i < n; i ++ ) cin >> g[i];
int x, y;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (g[i][j] == '@')
{
x = i;
y = j;
}
st[x][y]=1;
res++;
dfs(x,y);
cout<<res<<endl;
}
return 0;
}