---恢复内容开始---
https://www.cnblogs.com/OctoptusLian/p/7429645.html
解救小哈——dfs入门
//果然博客园质量高丫
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int dx[] = {0,-1,0,1};
int dy[] = {-1,0,1,0};
int ans,n,m;
char a[25][25];
void dfs(int x,int y)
{
int i,rx,ry;
for(i=0; i<4; i++)
{
rx=x+dx[i];
ry=y+dy[i];
if(rx<0||rx>=m||ry<0||ry>=n)
continue;
if(a[rx][ry]==‘.‘)
{
ans++;
a[rx][ry]=‘#‘;
dfs(rx,ry);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)&&n||m)
{
int i,j;
ans=0;
getchar();
for(i=0; i<m; i++)
{
gets(a[i]);
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(a[i][j]==‘@‘)
{
dfs(i,j);
ans++;
break;
}
}
}
printf("%d\n",ans);
}
}
原文地址:https://www.cnblogs.com/guanwen769aaaa/p/9974625.html