DFS PKU 1562

简单的DFS

Oil Deposits

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12801   Accepted: 6998

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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
//#include <map>
using namespace std;
#define MAXN 100 + 10
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int m,n;
//int count;
int xx[8] = {-1,-1,0,1,1,1,0,-1};
int yy[8] = {0,1,1,1,0,-1,-1,-1};

void DFS(int x,int y){
    int i;
    for(i=0;i<8;i++){
        int dx = x+xx[i];
        int dy = y+yy[i];
        if(dx>=0 && dx<m && dy>=0 && dy<n){
            if(map[dx][dy]=='@' && vis[dx][dy]==0){
                vis[dx][dy] = 1;
                DFS(dx,dy);
                //vis[dx][dy] = 0;
            }
        }
    }

    return ;
}

int main(){
    int i,j;
    int ans;

    while(~scanf("%d%d",&m,&n)){
        if(m==0 && n==0){
            break;
        }
        for(i=0;i<m;i++){
            scanf("%s",map[i]);
        }
        ans = 0;
        memset(vis,0,sizeof(vis));
        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                if(map[i][j]=='@' && vis[i][j]==0){
                    vis[i][j] = 1;
                    ans++;
                    DFS(i,j);
                    //vis[i][j] = 0;
                }
            }
        }
        printf("%d\n",ans);
    }
}

时间: 2024-10-05 14:01:34

DFS PKU 1562的相关文章

PKU 1562/HDU 1241 Oil Deposits(原油有多少块区域---BFS,DFS)

Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region o

DFS PKU 1979

水题保平安 Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22640   Accepted: 12223 Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile.

pku 2488 A Knight&#39;s Journey (搜索 DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28697   Accepted: 9822 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey aro

1562 poj dfs

#include<stdio.h> char grid[101][101];int n, m;int dir[8][2] = {    {-1, -1}, {-1,  0},    {-1,  1}, { 0,  1},    { 1,  1}, { 1,  0},    { 1, -1}, { 0, -1}}; void dfs(int x, int y){    int a, b;    grid[x][y] = '*';    for(int i = 0; i < 8; i++)

DFS + 剪枝 PKU 1088

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 78527   Accepted: 29187 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

[POJ] 1562 Oil Deposits (DFS)

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

PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)

题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合:true  B集合:false 解法一:dfs+剪枝 #include<iostream> #include<cstring> using namespace std; int n,ans; bool in[25]; int graph[25][25]; void dfs(int i

POJ 1562(L - 暴力求解、DFS)

油田问题(L - 暴力求解.DFS) 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 numerou

poj 1562 DFS+枚举

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