现在的位置: 首页 > 综合 > 正文

HDU 1241Oil Deposits(简单搜索题)

2013年11月02日 ⁄ 综合 ⁄ 共 2999字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.