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;
    }


     

  • 相关阅读:
    CSS display使用
    WPF触发器
    WPF动画2
    WPF动画2
    WPF 动画1
    CSS 媒体查询
    [Leetcode] Rotate List
    [Leetcode] Add Two Numbers
    [Leetcode] Sort List
    [Leetcode] Reverse Linked List II
  • 原文地址:https://www.cnblogs.com/2014acm/p/3900647.html
Copyright © 2011-2022 走看看