HDU - 1241 Oil Deposits

Posted lifehappy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 1241 Oil Deposits相关的知识,希望对你有一定的参考价值。

HDU - 1241 链接
连通块个数统计问题
选对一块油田八个方向进行dfs。
dfs的次数就是连通块的个数了,极度水题

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int ax[4] = {1, -1, 0, 0};
const int ay[4] = {0, 0, 1, -1};
const int N = 110;
int maze[N][N], cnt, maxn, n, m;
bool judge(int x, int y) {
    if(x >= 0 && x < n && y >= 0 && y < m && !maze[x][y])
        return true;
    return false;
}
void dfs(int x, int y) {
    for(int i = -1; i <= 1; i++)//八个方向进行搜索
        for(int j = -1; j <= 1; j++) {
            int tempx = x + i;
            int tempy = y + j;
            if(judge(tempx, tempy)) {
                maze[tempx][tempy] = 1;
                dfs(tempx, tempy);
            }
        }
}
int main() {
    // freopen("in.txt", "r", stdin);
    char c;
    while(cin >> n >> m && n) {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++) {
                cin >> c;
                if(c == ‘@‘)    maze[i][j] = 0;
                else    maze[i][j] = 1;
            }
        cnt = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++) {
                if(maze[i][j] == 0) {
                    maze[i][j] = 1;
                    dfs(i, j);
                    cnt++;
                }
            }
        cout << cnt << endl;
    }
    return 0;
}

以上是关于HDU - 1241 Oil Deposits的主要内容,如果未能解决你的问题,请参考以下文章

DFS——hdu1241Oil Deposits

HDU - 1241:Oil Deposits

HDU 1241Oil Deposits (DFS)

HDU - 1241 E - Oil Deposits

Oil Deposits hdu-1241 DFS

(经典DFS)HDU_1241 Oil Deposits