11.17 dfs poj1979 Red and Black

---恢复内容开始---

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);
}
}

posted @ 2018-11-17 16:51  观稳769  阅读(91)  评论(0编辑  收藏  举报