L

很清新的一道题,搜索里面最基础的题目,深搜广搜都可以.....不过还是喜欢深搜,写起来简单》。。
////////////////////////////////////////////////
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;

const int maxn = 105;
const int oo = 0xfffffff;

int dir[8][2] = { {0,1},{0,-1},{1,0},{-1,0},
                  {1,-1},{1,1},{-1,1},{-1,-1}};
int M, N;
char G[maxn][maxn];

void DFS(int x, int y)
{
    if(x<0||x==M||y<0||y==N||G[x][y] != '@')
        return ;
    G[x][y] = '*';

    for(int i=0; i<8; i++)
        DFS(x+dir[i][0], y+dir[i][1]);
}

int main()
{
    while(scanf("%d%d", &M, &N), M+N)
    {
        int i, j, ans=0;

        for(i=0; i<M; i++)
            scanf("%s", G[i]);

        for(i=0; i<M; i++)
        for(j=0; j<N; j++)
        {
            if(G[i][j] == '@')
            {
                ans++;
                DFS(i, j);
            }
        }

        printf("%d ", ans);
    }

    return 0;

} 

原文地址:https://www.cnblogs.com/liuxin13/p/4650378.html