Oil Deposits(poj 1526 DFS入门题)

http://poj.org/problem?id=1562

Oil Deposits

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12595   Accepted: 6868

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题目大意:如果‘@‘周围的8个方向有‘@‘就认为它们是同一油田。求总油田的个数。本题是dfs的入门题,我的dfs只能用很烂来形容,dfs利用了栈的思想,先进后出。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define N 1000001
using namespace std;
int n,m;
char map[101][101];
int v[101][101];
int jx[]={1,1,1,-1,-1,-1,0,0};
int jy[]={-1,0,1,-1,0,1,-1,1};
void dfs(int x,int y)
{
    v[x][y]=1;
    int tx,ty;
    for(int i=0;i<8;i++)
    {
       tx=x+jx[i];
       ty=y+jy[i];
       if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]==‘@‘&&v[tx][ty]==0)
       {
           dfs(tx,ty);
       }
    }
}
int main()
{
    int ans=0;
    while(scanf("%d%d",&n,&m)!=EOF&&n!=0)
    {
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%*c%s",map[i]);
        }
        memset(v,0,sizeof(v));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(map[i][j]==‘@‘&&v[i][j]==0)
                {
                    dfs(i,j);
                    ans++;
                }

            }
        }
        printf("%d\n",ans);

    }
    return 0;
}

Oil Deposits(poj 1526 DFS入门题),布布扣,bubuko.com

时间: 2024-08-05 16:13:02

Oil Deposits(poj 1526 DFS入门题)的相关文章

poj1562 Oil Deposits(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

hdu 1241 Oil Deposits (一次dfs搞定有某有)

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 char map[105][105]; 7 8 int dir[8][2]={0, 1, 1, 0, -1, 0, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; 9 int n, m; 10 int ans; 11 12

UVa 572 - Oil Deposits【图DFS】

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

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

poj 1154 LETTERS dfs入门题

//poj 1154 //sep9 #include <iostream> using namespace std; const int maxR=32; char a[maxR][maxR]; int r,s; int ans=1; int vis[200]; void dfs(int i,int j,int len) { ans=max(ans,len+1); if(i+1<r&&vis[a[i+1][j]]==0){ vis[a[i+1][j]]=1; dfs(i+

UVa 572 Oil Deposits(简单DFS)

题意: 给出油田的数量,八连通的“@”认为是一个油田.和POJ 2386 Lake Counting这个题几乎一样. 直接上代码: #include<iostream> using namespace std; char maze[105][105]; int n,m; void dfs(int x,int y) { maze[x][y]='*'; for(int dx=-1;dx<=1;dx++) for(int dy=-1;dy<=1;dy++) { int nx=x+dx,n

poj 1979 dfs水题

// 练练水题,夯实基础吧 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib

[HDU]1016 DFS入门题

题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tle了(服务器负载的问题吧),一模一样的第二次提交就ac了,侧面也反应了递归对stack的开销影响效率也是严重的.好了,上代码!! 题目传送门: HDU_1016 import java.util.Scanner; public class Main { public static final int

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M