depth first search 入门例题(油田的个数)

B - Oil Deposits POJ - 1562

题意: 输入数据有多组,输入两个整数,表示数组的行和列,下面输出,一个二维的字符串数组,仅包含‘@’和 ‘*’ 两种字符,相邻的‘@’ 表示 同一个油田,求总共个几个油田。
这道题也是dfs 的很简单的题型,也是一个原始的模板。
代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 110
using namespace std;

char mapp[N][N];// 要搜索的地图
int m, n;
int fx[10] = {0, 0, 1, -1, 1, 1, -1, -1};
int fy[10] = {1, -1, 0, 0, 1, -1, -1, 1};

void dfs(int ax, int ay)
{
    mapp[ax][ay] = '*';
    for(int i = 0; i < 8; i++)
    {
        int x = ax + fx[i];
        int y = ay + fy[i];
        if(x >= 0 && x < m && y >= 0 && y < n && mapp[x][y] == '@')
            dfs(x, y);
    }
}

int main()
{
    while(scanf("%d%d", &m, &n) && m+n)
    {
        int countt = 0;
        for(int i = 0; i < m; i++)// 数据输入
            scanf("%s", mapp[i]);

        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                if(mapp[i][j] == '@')
                    countt++, dfs(i, j);

        cout<<countt<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值