天天看點

L - Oil Deposits

很清新的一道題,搜尋裡面最基礎的題目,深搜廣搜都可以.....不過還是喜歡深搜,寫起來簡單》。。

#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\n ", ans);

    }

     return  0;

轉載于:https://www.cnblogs.com/liuxin13/p/4650378.html