zoukankan      html  css  js  c++  java
  • ZOJ1709 DFS和BFS两种搜索方法

    Oil Deposits

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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

    膜拜安神,看他做的PPT学会的BFS和DFS。。。。

    下面是BFS的代码

    #include <iostream>
    #include <queue>
    using namespace std;
    
    struct node
    {
        int x;
        int y;
        void init(int x1,int y1)
        {
            x=x1;y=y1;
        }
    };
    int m,n;
    int dir[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,-1},{1,1},{-1,1},{-1,-1}};
    char grid[110][110];
    void bfs(node z1)
    {
        queue<node>q;
        q.push(z1);
        while (!q.empty())
        {
            node c=q.front();
            q.pop();
            grid[c.x][c.y]='*';
            for (int k=0;k<8;k++)
            {
                int x1=c.x+dir[k][0];
                int y1=c.y+dir[k][1];
                if (x1<0||y1<0||x1>=m||y1>=n)
                  continue;
                if (grid[x1][y1]=='@')
                {
                    node b;
                    b.init(x1,y1);
                    q.push(b);
                }
            }
        }
    }
    
    
    int main()
    {
        int sum;
        while (cin>>m>>n&&m&&n)
        {
            cin.get();
            for (int i=0;i<m;i++)
                for (int j=0;j<n;j++)
            {
                cin>>grid[i][j];
            }
            cin.get();
            sum=0;
            for (int q=0;q<m;q++)
                for (int w=0;w<n;w++)
            {
                if (grid[q][w]=='@')
                {
                 node fi;
                 fi.x=q;fi.y=w;
                 sum++;
                 bfs(fi);
                }
    
            }
            cout<<sum<<endl;
        }
        return 0;
    }

    下面是DFS代码

    #include <iostream>
    using namespace std;
    char grid[110][110];
    int m,n;
    int dir[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,-1},{1,1},{-1,1},{-1,-1}};
    
    void dfs(int x,int y)
    {
        grid[x][y]='*';
        for (int k=0;k<8;k++)
        {
            int x1=x+dir[k][0];
            int y1=y+dir[k][1];
            if (x1<0||y1<0||x1>=m||y1>=n)
                continue;
            if (grid[x1][y1]=='@')
                dfs(x1,y1);
        }
        return;
    }
    int main()
    {
        int sum;
        while (cin>>m>>n&&m&&n)
        {
            cin.get();
            for (int i=0;i<m;i++)
                for (int j=0;j<n;j++)
            {
                cin>>grid[i][j];
            }
            cin.get();
            sum=0;
            for (int q=0;q<m;q++)
                for (int w=0;w<n;w++)
            {
                if (grid[q][w]=='@')
                {
                    sum++;
                    dfs(q,w);
    
                }
            }
            cout<<sum<<endl;
        }
        return 0;
    }

    膜拜安神,在油田的问题上,为了防止被重复搜索,最简单的办法就是马上把这块油田立即转化为非油田状态,由于DFS和BFS都可以搜到本节点下的所有子节点,所有转化后并不妨碍搜索。

  • 相关阅读:
    第10组 Beta冲刺 (4/5)
    第10组 Beta冲刺 (3/5)
    第10组 Beta冲刺 (2/5)
    第10组 beta冲刺(1/5)
    软工实践个人总结
    第01组 每周小结(3/3))
    第01组 每周小结(2/3)
    第01组 每周小结 (1/3)
    第01组 beta冲刺总结
    第01组 beta冲刺(5/5)
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2949950.html
Copyright © 2011-2022 走看看