Oil Deposits (oil issues)

Meaning of the questions: to find oil layers on the map, each of a plurality of oil reservoirs composed of these reservoirs adjacent to regular search problem, each time searching for the reservoir to the eight directions.

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=1241

Link: click here .

    Here is the code DFS:

#include <stdio.h> 
#include <Queue> 
#include <the iostream> 
#include < String .h>
 the using  namespace STD;
 int m, n-;
 char MP [ 105 ] [ 105 ];
 void DFS ( int A, int B ) 
{ 
    MP [a] [B] = ' * ' ; // find the oil changed it to avoid duplication * look 
    for ( int DX = - . 1 ; DX <= . 1 ; DX ++ ) 
    { 
        for ( intDy = - . 1 ; Dy <= . 1 ; Dy ++ ) 
        { 
            int NX = A + DX;
             int NY = B + Dy;
             IF (NX> = . 1 && NX <= m && NY> = . 1 && NY <= n-&& MP [NX] [NY] == ' @ ' ) 
            the DFS (NX, ny); // new fields most critical areas, recursive find. 
        } 
    } 
} 
Int main ( void ) 
{ 
    the while (CIN >> m >> n-&& && n-m) // where when m and m is 0 when the loop exits 
    {
         int SUM = 0 ;每次初始化sum为0。 
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>mp[i][j];
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(mp[i][j]=='@')
                {
                    the DFS (i, J);
                    sum ++; // each sum to find a reservoir ++. 
                } 
            } 
        } 
        The printf ( " % D \ n- " , SUM); 
    } 
    return  0 ; 
}

Of course, this problem can also be used bfs, just think DFS written easy to understand that, bfs not discussed in detail, after all, my question to do with bfs ML several times ,,,,

#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
#define N 100
using namespace std;
int r,c,ans;
char mp[N][N];
bool vis[N][N]; 
int fx[8]={1,0,-1,0,-1,1,-1,1};
int fy[8]={0,1,0,-1,-1,-1,1,1};
struct node
{
    int x,y;
};

bool check(int x,int y)
{
    if(!vis[x][y] && mp[x][y]=='@' && x>=0 && x<r && y>=0 && y<c )
        return 1;
    return 0;
}

void bfs(int x,int y)
{
    queue<node> q;
    q.push({x,y});
    while(q.size())
    {
        node now=q.front();
        q.pop();
        for(int i=0;i<8;i++)
        {
            int nextx=now.x + fx[i];
            int nexty=now.y + fy[i];
            if(check(nextx,nexty))
            {
                vis[nextx][nexty]=1;
                q.push({nextx,nexty});
            }
        }
    }
    return ;
 } 

int main()
{
    ios_base::sync_with_stdio;
    while(cin>>r>>c && r)
    {
        ans=0;
        memset(vis,0,sizeof vis);
        memset(mp,0,sizeof mp);
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                cin>>mp[i][j];
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if(mp[i][j]=='@' && !vis[i][j])
                {
                    bfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Summary about: This topic is recommended to use a deep search, each search can search out a piece of the reservoir, but these two algorithms have the best grasp.

Guess you like

Origin www.cnblogs.com/YHH520/p/12229546.html