HDU 1241Oil Deposits(簡單搜尋題)

果7發表於2013-09-05

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8471    Accepted Submission(s): 4949


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
 

Source
 


     題目大意:@表示油田,可以向周圍八個方向延伸,問你有多少塊油田。下面是DFS,BFS兩種解法。

        題目地址:Oil Deposits


DFS很好解決,直接每次往八個方向擴充套件,dfs即可。
AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
char map1[105][105];
int visi[105][105];

void dfs(int i,int j)
{
    if(map1[i][j]=='@'&&!visi[i][j])
    {   //向周圍八個方向搜尋
        visi[i][j]=1;
        dfs(i-1,j-1);
        dfs(i+1,j+1);
        dfs(i-1,j);
        dfs(i+1,j);
        dfs(i-1,j+1);
        dfs(i+1,j-1);
        dfs(i,j-1);
        dfs(i,j+1);
    }
    return;
}

int main()
{
    int i,j,m,n,sum;
    while(scanf("%d%d",&m,&n))
    {
        if(m==0&&n==0)
            break;
        memset(visi,0,sizeof(visi));
        for(i=1;i<=m;i++)  //邊界處理
        {
            map1[i][0]='*';
            map1[i][n+1]='*';
        }
        for(i=1;i<=n;i++)
        {
            map1[0][i]='*';
            map1[m+1][i]='*';
        }
        for(i=1;i<=m;i++)
            scanf("%s",map1[i]+1);

        /*for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
              cout<<map1[i][j];
            cout<<endl;
        }*/
        sum=0;
        for(i=1;i<=m;i++)
            for(j=1;j<=n;j++)
               if(map1[i][j]=='@'&&!visi[i][j])
               {
                   dfs(i,j);
                   sum++;
               }
        cout<<sum<<endl;
    }
    return 0;
}

//0MS 396K


BFS也比較好寫,座標需要轉換,當時腦子被驢踢了,行和列一直在糾結,最後WA了幾發!
 AC程式碼:
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
char map1[105][105];
int visi[105][105];
int sum,m,n;
int dir[8][2]=
{
    {-1,-1},{-1,0},{-1,1},{0,-1},
    {0,1},{1,-1},{1,0},{1,1}
};
queue<int>q;

void bfs()
{
    int i,j,k;
    for(i=0;i<m;i++)
       for(j=0;j<n;j++)
       {
           if(map1[i][j]=='@'&&!visi[i][j])
           {
               int x,y,s,r;
               q.push(i*n+j);  //二維壓成一維的
               while(!q.empty())
               {
                   int head;
                   head=q.front();
                   q.pop();  //取出隊首元素
                   r=head/n; //橫座標
                   s=head%n; //縱座標
                   for(k=0;k<8;k++)
                   {
                       x=r+dir[k][0];//橫座標
                       y=s+dir[k][1];//縱座標
                       if(x>=0&&x<m&&y>=0&&y<n&&map1[x][y]=='@'&&visi[x][y]==0)
                       {
                           visi[x][y]=1;
                           q.push(x*n+y);
                       }
                   }
               }
               sum++;
           }
       }
}

int main()
{
    int i;
    while(scanf("%d%d",&m,&n))
    {
        if(m==0&&n==0) break;
        memset(visi,0,sizeof(visi));
        for(i=0;i<m;i++)
            scanf("%s",map1[i]);
        sum=0;
        bfs();
        cout<<sum<<endl;
    }
    return 0;
}

//0MS 340K


相關文章