D - Lake Counting-dfs

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
char map[101][101];


int floorr,k;
int dx[]={0,-1,-1,0,1,1,1,0,-1};
int dy[]={0,0,-1,-1,-1,0,1,1,1};


int n,m;


int main()
{
int i,j;

int bfs(int r,int c);

scanf("%d%d",&n,&m);getchar();

for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
}
getchar();
}



 
k=0;

for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
if (map[i][j]=='.') continue;
floorr=0;

map[i][j]='.';

bfs(i,j);
if (floorr>=0) k++;
}
}


printf("%d\n",k);
return 0;





int legal(int r,int c)
{

if ( r>=1 && r<=n && c>=1 && c<=m) return 1; 
return 0;
}


int bfs(int r,int c)
{


int i;
for (i=1;i<=8;i++)
{
if (  map[r+dx[i]][c+dy[i]]=='W' && legal(r+dx[i],c+dy[i])     )
{
map[r+dx[i]][c+dy[i]]='.'; 
floorr++;
bfs(r+dx[i],c+dy[i]); 
 
 






}
}


return 0;
}

你可能感兴趣的:(D - Lake Counting-dfs)