HDU1241-Oil Deposits

 

题目链接:点击打开链接

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

 

0122

 

 

 

思路:明显是一道深搜或者广搜的模板题。发现一个点可以进入,就把与它相连通的位置置为访问过,类似于数连通块。

AC代码:

 

 

BFS

 

#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
const int MAX = 10010;
const int INF = 0X3f3f3f;

char G[110][110];//图的信息

int n, m;

int X[8] = {-1, 1, 0, 0, -1, 1, -1, 1};//增量数组
int Y[8] = {0, 0, -1, 1, -1, 1, 1, -1};

struct node{
    int x, y;
}a, b, c;//结构体变量

bool judge(int x, int y) {
    if(x < 0 || x >= m || y >= n || y < 0)//出界
        return false;
    if(G[x][y] == '*')//不能进
        return false;
    return true;
}

void bfs(int x, int y) {//模板
    queue<node> q;
    a.x = x;
    a.y = y;
    q.push(a);
    while(!q.empty()) {
        b = q.front();
        q.pop();
        for(int i = 0; i < 8; i++) {
            c.x = b.x + X[i];
            c.y = b.y + Y[i];
            if(judge(c.x, c.y)) {
                G[c.x][c.y] = '*';//这里没有设置访问数组,直接在原图上进行更改
                q.push(c);
            }
        }
    }
}

int main() {
    while(scanf("%d %d", &m, &n) != EOF) {
        if(m == 0)
            break;
        memset(G, 0, sizeof(G));
        for(int i = 0; i < m; i++) {
            scanf("%s", G[i]);
        }
        int sum = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(G[i][j] == '@') {//可以进
                    G[i][j] = '*';//更改
                    bfs(i, j);//广搜
                    sum++;
                }
            }
        }
        cout << sum << endl;
    }
    return 0;
}

 

DFS

 

#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
const int MAX = 10010;
const int INF = 0X3f3f3f;

char G[110][110];

int n, m;

int X[8] = {-1, 1, 0, 0, -1, 1, -1, 1};
int Y[8] = {0, 0, -1, 1, -1, 1, 1, -1};

struct node{
    int x, y;
}a, b, c;

bool judge(int x, int y) {
    if(x < 0 || x >= m || y >= n || y < 0)
        return false;
    if(G[x][y] == '*')
        return false;
    return true;
}

void dfs(int x, int y) {
        for(int i = 0; i < 8; i++) {
            c.x = x + X[i];
            c.y = y + Y[i];
            if(judge(c.x, c.y)) {//这个点可以
                G[c.x][c.y] = '*';//更改
                dfs(c.x, c.y);//搜
            }
        }
}

int main() {
    while(scanf("%d %d", &m, &n) != EOF) {
        if(m == 0)
            break;
        memset(G, 0, sizeof(G));
        for(int i = 0; i < m; i++) {
            scanf("%s", G[i]);
        }
        int sum = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(G[i][j] == '@') {
                    G[i][j] = '*';//更改
                    dfs(i, j);//dfs深搜
                    sum++;
                }
            }
        }
        cout << sum << endl;
    }
    return 0;
}

 

posted @ 2018-04-20 08:57  Frontierone  阅读(91)  评论(0编辑  收藏  举报