[kuangbin带你飞]专题一 简单搜索- L - Oil Deposits

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f
int gcd(int a, int b) {return b==0 ? a:gcd(b,a%b);}
int n, m;
char s[110][110];
int mp[110][110];
int vis[110][110];
int xx[] = {0,0,-1,1,-1,1,-1,1};
int yy[] = {-1,1,0,0,-1,1,1,-1};
void dfs(int x, int y)
{
    vis[x][y] = 1;
    for(int i = 0; i < 8; i++)
    {
        int tx = x+xx[i];
        int ty = y+yy[i];
        if(tx<1||ty<1||tx>n||ty>m||vis[tx][ty]||!mp[tx][ty]) continue;
        dfs(tx,ty);
    }
}

int main()
{
    while(~scanf("%d %d", &n, &m)&&m&&n)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", s[i]+1);
        }
        memset(mp,0,sizeof(mp));
        memset(vis,0,sizeof(vis));
        int cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(s[i][j]=='@') mp[i][j] = 1;
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(vis[i][j]||!mp[i][j]) continue;
                dfs(i,j);
                cnt++;
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}

你可能感兴趣的:(kuangbin)