UVA Oil Deposits (BFS)

 Oil Deposits 

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  and .
Following this are mlines 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

       题意:问地图中有多少个@,前提如果@的八个方向中如果还是@,则相邻的@不进入计数。(简单的BFS)

代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<queue>

using namespace std;

struct node
{
    int x;
    int y;
} q[10010];
int jx[] = {0,1,0,-1,1,1,-1,-1};
int jy[] = {1,0,-1,0,1,-1,1,-1};
int n,m;
char map[101][101];
int v[101][101];
int pt;
void BFS()
{
    node t,f;
    memset(v,0,sizeof(v));
    queue<node>p;
    int count = 0;
    for(int i=0; i<pt; i++)
    {
        if(v[q[i].x][q[i].y] == 0)
        {
            t.x = q[i].x;
            t.y = q[i].y;
            v[t.x][t.y] = 1;
            count++;
            p.push(t);
            while(!p.empty())
            {
                t = p.front();
                p.pop();
                for(int j=0;j<8;j++)
                {
                    f.x = t.x + jx[j];
                    f.y = t.y + jy[j];
                    if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && map[f.x][f.y] == ‘@‘ && v[f.x][f.y] == 0)
                    {
                        p.push(f);
                        v[f.x][f.y] = 1;
                    }
                }
            }
        }
    }
    printf("%d\n",count);

}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n == 0 && m == 0)
        {
            break;
        }
        pt = 0;
        struct node a;
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(int j=0; j<m; j++)
            {
                if(map[i][j] == ‘@‘)
                {
                    a.x = i;
                    a.y = j;
                    q[pt++] = a;
                }
            }
        }
        BFS();
    }
    return 0;
}

Miguel A. Revilla

1998-03-10

时间: 2024-07-30 07:28:17

UVA Oil Deposits (BFS)的相关文章

POJ 1562-Oil Deposits(BFS)

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 6888 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

Oil Deposits(油田)

题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides the land into numerous square plots. It then analyzes each plot s

题目--oil Deposits(油田) 基础DFS(深度搜索)

上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--depth first search,话不多说,直接上题了解: Description:某石油勘探公司正在按计划勘探地下油田资源,工作在一片长方形的地域中.他们首先将该地域划分为许多小正方形区域,然后使用探测设备分别探测每一块小正方形区域内是否有油.若在一块小正方形区域中探测到有油,则标记为’@’,否则标

uva 10603 Fill (BFS)

uva 10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third is completely filled with water. It is allowed to pour

UVA 11624 - Fire!(BFS)

UVA 11624 - Fire! 题目链接 题意:一个迷宫,一些格子着火了,火每秒向周围蔓延,现在J在一个位置,问他能走出迷宫的最小距离 思路:BFS2次,第一次预处理每个位置着火时间,第二次根据这个再BFS一次 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; co

UVA - 572_Oil Deposits(FloodFill)

要求:计算二维中连通块个数. #include<bits/stdc++.h> using namespace std; const int maxn = 100 + 3; const int maxm = 100 + 3; int m, n; char buf[maxm][maxn]; bool book[maxm][maxn]; void floodfill(int r, int c) { book[r][c] = true; for(int dr = -1; dr < 2; dr++

UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常量数组或者写8条DFS调用. 下述算法是:种子填充(floodfill) 两种连通区域 四连通区域:从区域内一点出发,可通过上.下.左.右四个方向的移动组合,在不越出区域的前提下,能到达区域内的任意像素 八连通区域:从区域内每一像素出发,可通过八个方向,即上.下.左.右.左上.右上.左下.右下移动的

HDU 1241 Oil Deposits(石油储藏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

UVA 816 - Abbott&#39;s Revenge(BFS)

UVA 816 - Abbott's Revenge 题目链接 题意:一个迷宫,每个点限制了从哪一方向来的,只能往左右前走,然后问起点到终点的最短路径 思路:BFS,每个点拆成4个方向的点,对应能走的方向建图跑一下bfs即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace