zoukankan      html  css  js  c++  java
  • POJ 1562 Oil Deposits (并查集 OR DFS求联通块)

    Oil Deposits
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 14628   Accepted: 7972

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

    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
    方法1:并查集 
    收获:二维数组转一维数组公式:i*m(为列数)+j;
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <ctime>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const double eps=1e-10;
    const double PI=acos(-1.0);
    #define maxn 500
    #define maxm 100000
    int n, m;
    char mp[maxn][maxn];
    int root[maxm];
    bool solve(int x, int y)
    {
        if(x < 0 || x >= n || y < 0 || y >= m)
            return true;
        return false;
    }
    int find_root(int x)
    {
        if(x != root[x])
            root[x] = find_root(root[x]);
        return root[x];
    }
    void uni(int a, int b)
    {
        int x = find_root(a);
        int y = find_root(b);
        if(x != y)
        {
            root[y] = x;
        }
    }
    void judge(int x, int y)
    {
        for(int i = -1; i <= 1; i++)
            for(int j = -1; j <= 1; j++)
            {
                if(i != 0 || j != 0)
                {
                    int dx = x + i;
                    int dy = y + j;
                    if(mp[dx][dy] == '@' && !solve(dx, dy))
                    {
                        int p = x * m + y;
                        int q = dx * m + dy;
                        uni(p, q);
                    }
                }
            }
    }
    int main()
    {
        while(~scanf("%d%d", &n, &m) && (n+m) != 0)
        {
            memset(root, -1, sizeof root);
            for(int i = 0; i < n; i++)
                scanf("%s", mp[i]);
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    if(mp[i][j] == '@')
                    root[i*m+j] = i * m +j;
                }
            }
    
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++)
                {
                    if(mp[i][j] == '@')
                    {
                        judge(i, j);
                    }
                }
            int ans = 0;
            for(int i = 0; i < n ; i++)
                for(int j = 0; j < m; j++)
                    if(root[i*m+j] == i*m+j)
                        ans++;
                printf("%d
    ", ans);
        }
        return 0;
    }
    方法二:
    DFS入门题
    收获:做搜索的题目调试的时候可以用打印中间路径的方法来调试。
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <ctime>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const double eps=1e-10;
    const double PI=acos(-1.0);
    #define maxn 500
    int n, m;
    char mp[maxn][maxn];
    int vis[maxn][maxn];
    bool solve(int x, int y)
    {
        if(x < 0 || x >= n || y < 0 || y >= m)
            return true;
        return false;
    }
    void dfs(int x, int y)
    {
        for(int i = -1; i <= 1; i++)
            for(int j = -1; j <= 1; j++)
            {
                if(i != 0 || j != 0)
                {
                    int dx = x + i;
                    int dy = y + j; 
                    if(!vis[dx][dy] && mp[dx][dy] == '@' && !solve(dx, dy))
                    {
                        vis[dx][dy] = 1;
                        dfs(dx, dy);
                    }
                }
            }
    }
    int main()
    {
        while(~scanf("%d%d", &n, &m) && (n+m) != 0)
        {
            for(int i = 0; i < n; i++)
                scanf("%s", mp[i]);
            int cnt = 0;
            memset(vis, 0, sizeof vis);
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++)
                {
                    if(mp[i][j] == '@' && !vis[i][j])
                    {
                        vis[i][j] = 1;
                        ++cnt;
                        dfs(i, j);
                    }
                }
                printf("%d
    ", cnt);
        }
        return 0;
    }
  • 相关阅读:
    ionic+cordova开发!
    npm安装出错的时候,如何使用国内的镜像!--解决办法
    wamp环境解决局域网不能访问的问题!
    flex弹性布局属性详解!
    JS判断当前是否是IE浏览器,并返回时IE几?
    thinkphp---手机访问切换模板!
    thinkCMF的使用!
    thinkphp5在集成环境目录访问权限问题
    微信小程序 --- 下拉刷新上拉加载
    jQuery --- 利用a标签的download属性下载文件!
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4639613.html
Copyright © 2011-2022 走看看