POJ 1562 Oil Deposits

简单DFS,搜索八个方向。

# include <stdio.h>



const int dir[][2] = {{-1,0}, {0,-1}, {0,1}, {1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};



int n, m;

char f[105][105];



char read_data(void)

{

    int i, j;

    

    scanf("%d", &m);

    scanf("%d", &n);

    if (m == 0) return 0;

    for (i = 1; i <= m; ++i)

    {

        scanf("%s", f[i]+1);

    }

    

    return 1;

}



void dfs(int ci, int cj)

{

    int i, ni, nj;

    

    f[ci][cj] = '*';

    for (i = 0; i < 8; ++i)

    {

        ni = ci + dir[i][0];

        nj = cj + dir[i][1];

        if (1<=ni && ni<=m && 1<=nj && nj<=n && f[ni][nj]=='@')

        {

            dfs(ni, nj);

        }    

    }

}



void solve(void)

{

    int cnt, i, j;

    

    cnt = 0;

    for (i = 1; i <= m; ++i)

    for (j = 1; j <= n; ++j)

    {

        if (f[i][j] == '@')

        {

            dfs(i, j);

            ++cnt;

        }

    }

    

    printf("%d\n", cnt);

}



int main()

{

    while (read_data() == 1)

    {

        solve();

    }

    

    return 0;

}

你可能感兴趣的:(poj)