zoukankan      html  css  js  c++  java
  • #1241: Oil Deposits(八向搜索 + 并查集)

    Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

    Total Submission(s): 12461 Accepted Submission(s): 7245


    Problem Description

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.


    Input

    The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.


    Output

    For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.


    Sample Input

    1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0


    Sample Output

    0 1 2 2


    本题是指@表示油点,*表示非油点,油点相邻则表示一个油点而不是多个,找出总油点个数。没啥技巧,就是八个方向搜索,同时利用并查集的效率更高

    DFS搜索:

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 100 + 5;
    char g[N][N];
    bool book[N][N];
    int n, m;
    const int dx[] = { 0,0,1,1,1,-1,-1,-1 };
    const int dy[] = { 1,-1,0,1,-1,0,1,-1 };
    void dfs(int i, int j) {
        book[i][j] = 1;
        for (int k = 0; k < 8; k++){
            int nx = i + dx[k], ny = j + dy[k];
            if (nx >= 0 && nx < m && ny >= 0 && ny < n && book[nx][ny] == 0 && g[nx][ny] == '@')dfs(nx, ny);
        }
    }
    int main() {
        //freopen("in.txt", "r", stdin);
        while (cin >> m >> n && n && m) {
            memset(g, 0, sizeof(g));
            memset(book, 0, sizeof(book));
            for (int i = 0; i < m; i++)cin >> g[i]; int cnt = 0;
            for (int i = 0; i < m; ++i)for (int j = 0; j < n; ++j)if (g[i][j] == '@' && !book[i][j]) { cnt++; dfs(i, j); }
            cout << cnt << endl;
        }
        return 0;
    }
    

    并查集:

    #include<bits/stdc++.h>
    using namespace std;
    char g[101][101];
    int fa[101 * 101];
    int x[] = { 0,0,1,-1,1,-1,1,-1 };
    int y[] = { 1,-1,0,0,1,1,-1,-1 };
    int n, m;
    int id(int x, int y) {
        return (x - 1)*m + y;
    }
    bool isok(int x, int y) {
        if (0 < x&&x <= n && 0 < y&&y <= m)
            return true;
        return false;
    }
    int Find(int x) {
        return fa[x] == x ? x : fa[x] = Find(fa[x]);
    }
    
    int vis[101 * 101];
    int main() {
        while (scanf("%d%d", &n, &m) != EOF && n + m) {
            memset(vis, 0, sizeof vis);
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= m; ++j) {
                    fa[id(i, j)] = id(i, j);
                }
            }
            for (int i = 1; i <= n; ++i)
                scanf("%s", g[i] + 1);
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= m; ++j) {
                    if (g[i][j] == '*') continue;
                    for (int k = 0; k < 7; ++k) {
                        int xx = i + x[k], yy = j + y[k];
                        if (isok(xx, yy) == false || g[xx][yy] == '*') continue;
                        int f1 = Find(id(i, j)), f2 = Find(id(xx, yy));
                        if (f1 != f2) {
                            fa[f1] = f2;
                        }
                    }
                }
            }
            int cnt = 0;
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= m; ++j) {
                    if (g[i][j] != '*'&&vis[Find(id(i, j))] == 0) {
                        cnt++;
                        vis[Find(id(i, j))] = 1;
                    }
                }
            }
            cout << cnt << endl;
        }
    
        return 0;
    }
    
  • 相关阅读:
    IOS快速集成下拉上拉刷新
    无限互联IOS电影项目视频笔记
    Unix时间戳(Unix timestamp)转换工具
    ***php(codeigniter)中如何重定向
    CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
    一个Form中2个按钮,PHP后台如何判断提交的是哪一个按钮
    PHP 判断是否包含某字符串
    Linux那些事儿之我是Hub(大结局)挂起自动化【转】
    Linux USB Host-Controller的初始化代码框架分析【转】
    urb传输的代码分析【转】
  • 原文地址:https://www.cnblogs.com/RioTian/p/12858317.html
Copyright © 2011-2022 走看看