【POJ】1562 Oil Deposits(DFS)

原创 2018年04月16日 10:44:34

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 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

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

AC代码:

//
//  main.cpp
//  POJ 1562
//
//  Created by showlo on 2018/4/16.
//  Copyright © 2018年 showlo. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define max 1000
char map[max][max];
int vis[max][max],num[max][max];
int m,n;

void dfs(int x,int y){
    int i,j,nx,ny;
    vis[x][y]=1;
    for (i=-1; i<=1; i++) {
        for (j=-1; j<=1; j++) {
            nx=x+i;
            ny=y+j;
            if (nx>=0&&nx<m&&ny>=0&&ny<n&&map[nx][ny]=='@'&&vis[nx][ny]==0)
            {
                vis[nx][ny]=1;
                dfs(nx, ny);
            }
            else
                continue;
        }
    }
    return;
}
int main() {
    int i,j,num;
    while (scanf("%d %d",&m,&n)!=EOF) {
        if (m==0)
            break;
        num=0;
        memset(vis, 0, sizeof(vis));
        for(i=0;i<m;i++)
            scanf("%s",map[i]);
        for (i=0; i<m; i++) {
            for (j=0; j<n; j++) {
                if (map[i][j]=='@'&&vis[i][j]==0) {
                    dfs(i,j);
                    num++;
                }
            }
        }
        printf("%d\n",num);
    }
    return 0;
}



Windows Server 2008 R2 部署并管理文件服务器视频课程(DFS)

-
  • 1970年01月01日 08:00

POJ 1562:Oil Deposits

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14462   ...
  • u010885899
  • u010885899
  • 2015-07-31 16:06:07
  • 447

POJ-1562-Oil Deposits

这个题就是说以@代表油田,它周边8个方向若有@则表示同一个油田。给你一个图,问你图中有多少个油田。 算是比较简单的DFS题,直接进行搜索, 只是注意对已经搜索过的点进行标记操作。 代码: #in...
  • z309241990
  • z309241990
  • 2013-03-09 19:15:24
  • 1592

ZOJ1709/POJ1562 油田问题/搜索/DFS

GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石...
  • AgoniAngel
  • AgoniAngel
  • 2015-07-25 10:46:23
  • 1544

hdu1241 Oil Deposits 同poj 2386 Lake Counting

所给实例的“5 5 ”第二个5后还有一个空格,测试的时候出错,但后台数据没有那个空格,可以过#include #include using namespace std; struct node { ...
  • qq172108805
  • qq172108805
  • 2011-07-27 09:45:46
  • 453

Oil Deposits(基础dfs)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T...
  • tree__water
  • tree__water
  • 2016-02-28 09:46:59
  • 533

ACM-DFS之Oil Deposits——hdu1241

Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible for detecti...
  • lx417147512
  • lx417147512
  • 2014-02-25 12:47:46
  • 1939

ACM--DFS--poj 1562--Oil Deposits

poj地址:http://poj.org/problem?id=1562                                                         Oil ...
  • qq_26891045
  • qq_26891045
  • 2016-05-05 18:29:21
  • 1060

Oil Deposits(dfs深搜)

Problem Description The GeoSurvComp geologic survey company is responsible for detecting undergro...
  • Scarlett_geng
  • Scarlett_geng
  • 2015-08-13 02:25:42
  • 346

杭电1241 HDU acm --Oil Deposits---DFS深度优先算法实现详解

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1241 又是经典搜索题!还是那句话,数据量小,要求简单,果断DFS! 这道题目跟以前的题目稍...
  • zhuihunmiling
  • zhuihunmiling
  • 2013-05-27 14:15:33
  • 2393
收藏助手
不良信息举报
您举报文章:【POJ】1562 Oil Deposits(DFS)
举报原因:
原因补充:

(最多只允许输入30个字)