Oil Deposits (poj 1241) 搜索 (深度优先搜素)

Oil Deposits

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

 


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

在线翻译:

石油矿床
时间限制:2000 / 1000毫秒(java /其他)内存限制:65536 / 32768 K(java /其他)
提交总提交:8274接受提交:4860
问题描述
的geosurvcomp地质调查公司负责探测地下石油储量。geosurvcomp运作在一个大的矩形区域的土地在一个时间,并创建一个网格把土地分成很多广场地块。然后分别对每一个小区分别进行分析,利用传感设备确定小区是否含有油。被称为口袋的阴谋。如果两个口袋是相邻的,那么他们是相同的石油存款的一部分。石油储量相当大,而且可能蕴藏大量的石油储量。你的工作是确定在一个网格中包含多少不同的石油储量。
输入
输入文件包含一个或多个网格。每一个网格开始一行包含米和氮,在网格中的行和列的数目,用一个空格隔开。如果米= 0,它的信号的输入端,否则1个< = = 100和1。下面这是每个字符的行(不算行字符的结束)。每个字符对应一个图,并且是' * ',代表没有油,或' @ ',代表一个石油口袋。
输出
对于每一个网格,输出的数量不同的石油储量。如果他们是相邻的水平,垂直或对角的相同的油存款的一部分,2个不同的口袋是相同的。一个石油储量将不包含超过100个口袋。

解题思路;

     1.深度优先搜素

     2.queue的使用

#include<stdio.h>
#include<queue>
using namespace std;
char map[100][100];       //存图;
int n,m;
int dir[8][2]={-1,-1,0,-1,1,-1,-1,0,1,0,-1,1,0,1,1,1}; //八个方向,无小括号;
struct state
{
    int x,y;
};
void bfs(int i,int j)
{
   int k;
    map[i][j]='*';
    queue<state>q;         //建立一个以state这个数据类型为节点的队列,队列的变量为q;
    state cur,next;        //数据结构中下个节点;
    cur.x=i;
    cur.y=j;
    q.push(cur);           //queue中 进队列;
    while(!q.empty())      //循环的条件 不为空;
    {
        cur=q.front();     //得到队首的值
        q.pop();           //queue中 出队列;
        for(k=0;k<8;k++)
        {
            next.x=cur.x+dir[k][0];
            next.y=cur.y+dir[k][1];
            if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n)
            {
                if(map[next.x][next.y]=='@')
                {
                    map[next.x][next.y]='*';
                    q.push(next);       //queue中 进队列再次执行,直到最后;
                }
            }
        }
    }
}
int main()
{
    int i,j,sum=0;
    while(scanf("%d%d",&m,&n),m)
    {
        getchar();             //消除字符空格;
        sum=0;                 //循环中变量的初始化;
        for(i=0;i<m;i++)
            gets(map[i]);
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                if(map[i][j]=='@')
                {
                    sum++;
                    bfs(i,j);          //调用函数;
                }
        printf("%d\n",sum);
    }
    return 0;
}
<pre name="code" class="html">#include <iostream>
#include <queue>
using namespace std; //这几个头文件必不可少

int main()
{
queue<类型(如int)> q; //使用前需定义一个queue变量,且定义时已经初始化
while(!q.empty()) q.pop(); //重复使用时,用这个初始化
q.push(1); //进队列
q.pop(); //出队列
int v=q.front(); //得到队首的值
int s=q.size(); //得到队列里元素个数


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值