UVA 572 DFS 搜尋演算法題解
阿新 • • 發佈:2019-02-13
memset真的 很好用的
#include<bits/stdc++.h>
using namespace std;
#define MAXN 105
int m,n;
char mapp [MAXN][MAXN];
bool idex[MAXN][MAXN];
int direct[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}};
void dfs(int ii , int jj);
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(scanf("%d%d",&m,&n))
{
int num = 0;
memset(mapp,sizeof(mapp),0);
if(m==0&&n==0) break;
for(int i = 0 ;i<m ;i++)
for(int j = 0 ;j<n ;j++)
{
idex[i][j]=false;
cin >> mapp[i][j];
}
for(int i = 0 ;i<m ;i++)
for(int j = 0 ;j<n ;j++)
if(mapp[ i][j]=='@'&&idex[i][j]==0)
{
num++;
idex[i][j] = 1;
dfs(i,j);
}
cout<<num<<endl;
}
return 0;
}
void dfs(int ii , int jj)
{
for(int i = 0 ;i < 8;i++)
{
int newx = ii + direct [i][0];
int newy = jj + direct [i][1];
if(mapp[newx][newy]=='@'&&idex[newx] [newy]==0&&newx>=0&&newy>=0&&newx<m&&newy<n)
{
idex[newx][newy] = 1;
dfs(newx,newy);
}
}
}