hdu1241 Oil Deposits(dfs)

本文通过一个具体的迷宫搜索问题,介绍了使用深度优先搜索(DFS)算法来寻找迷宫中连通区域的方法。代码实现中利用了8个方向进行遍历,并通过递归调用实现了对所有可达位置的标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当时年轻不懂事,这题XJBD了,现在改回来555。。。2016/3/1

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>

using namespace std;

const int N = 100;
const int INF = 1000000;

char Map[N][N];
int n, m;

int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

void dfs(int x, int y)
{
    for(int i = 0; i < 8; i ++)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if(xx >= 0 && xx < m && yy >= 0 && yy < n && Map[xx][yy] == '@')
        {
            Map[xx][yy] = '*';
            dfs(xx, yy);
        }
    }
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int ans;
    while(~scanf("%d%d", &m, &n))
    {
        if(m == 0 && n == 0) break;
        ans = 0;
        for(int i = 0; i < m; i ++)
            for(int j = 0; j < n; j ++)
                cin >> Map[i][j];
        for(int i = 0; i < m; i ++)
            for(int j = 0; j < n; j ++)
            {
                if(Map[i][j] == '@')
                {
                    ans ++;
                    dfs(i, j);
                }
            }
        printf("%d\n", ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值