zoukankan      html  css  js  c++  java
  • HDU

    HDU - 1241 链接
    连通块个数统计问题
    选对一块油田八个方向进行dfs。
    dfs的次数就是连通块的个数了,极度水题

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int ax[4] = {1, -1, 0, 0};
    const int ay[4] = {0, 0, 1, -1};
    const int N = 110;
    int maze[N][N], cnt, maxn, n, m;
    bool judge(int x, int y) {
        if(x >= 0 && x < n && y >= 0 && y < m && !maze[x][y])
            return true;
        return false;
    }
    void dfs(int x, int y) {
        for(int i = -1; i <= 1; i++)//八个方向进行搜索
            for(int j = -1; j <= 1; j++) {
                int tempx = x + i;
                int tempy = y + j;
                if(judge(tempx, tempy)) {
                    maze[tempx][tempy] = 1;
                    dfs(tempx, tempy);
                }
            }
    }
    int main() {
        // freopen("in.txt", "r", stdin);
        char c;
        while(cin >> n >> m && n) {
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++) {
                    cin >> c;
                    if(c == '@')    maze[i][j] = 0;
                    else    maze[i][j] = 1;
                }
            cnt = 0;
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++) {
                    if(maze[i][j] == 0) {
                        maze[i][j] = 1;
                        dfs(i, j);
                        cnt++;
                    }
                }
            cout << cnt << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    yii---模型的创建
    yii---控制器的创建
    yii的安装
    windows下安装composer
    wpgcms---列表页数据渲染
    Twig---基本使用
    wpgcms---详情页面数据怎么渲染
    Twig---的使用
    vue---指令怎么写
    vue---设置缩进为4个空格
  • 原文地址:https://www.cnblogs.com/lifehappy/p/12604443.html
Copyright © 2011-2022 走看看