zoukankan      html  css  js  c++  java
  • 1562 poj dfs

    #include<stdio.h>
     
    char grid[101][101];
    int n, m;
    int dir[8][2] =
    {
        {-1, -1}, {-1,  0},
        {-1,  1}, { 0,  1},
        { 1,  1}, { 1,  0},
        { 1, -1}, { 0, -1}
    };
     
    void dfs(int x, int y)
    {
        int a, b;
        grid[x][y] = '*';
        for(int i = 0; i < 8; i++) {
            a = x + dir[i][0];
            b = y + dir[i][1];
            if(a >= 0 && a < m && b >= 0 && b < n && grid[a][b] == '@') dfs(a, b);
        }
    }
     
    int main()
    {
        while(scanf("%d %d", &m, &n), m) {
            int ans = 0;
            for(int i = 0; i < m; i++) {
                scanf("%s", grid[i]);
            }
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    if(grid[i][j] == '@') {
                        dfs(i, j);
                        ans++;
                    }
                }
            }
            printf("%d ", ans);
        }
        return 0;
    }


     

  • 相关阅读:
    hdu1242 Rescue BFS广搜 + 优先队列
    hdu 1430 魔板
    康托展开
    hdu 4394 Digital Square(bfs)
    hdu 1969 Pie
    KMP模板
    hdu 1846 Brave Game
    循环赛日程表
    hdu 1022 Train Problem I
    整数划分问题
  • 原文地址:https://www.cnblogs.com/2014acm/p/3900647.html
Copyright © 2011-2022 走看看