HDU 1241 Oil Deposits

该博客介绍了如何解决HDU 1241编程题,内容涉及地图上的油田连通性判断,计算地图上@符号(代表油井)形成的油田总数。

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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1241

给出一张地图,@代表有油,*代表没有。对于一个@,它的上下左右,左上,左下,右上,右下的位置若含有@,则它们属于同一块油田,问给出的地图中有几块油田?

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#define pii pair<int, int>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 107;
int dir[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1};
char grid[MAXN][MAXN];
int m, n;
int vis[MAXN][MAXN];
int cnt = 0;
void bfs(int x, int y)
{
    queue<pii> q;
    q.push(pii(x, y));
    while(!q.empty())
    {
        pii now = q.front(); q.pop();
        for(int i = 0; i < 8; i++)
        {
            int nx = now.first + dir[i][0];
            int ny = now.second + dir[i][1];

            if(nx >= 0 && nx < m && ny >= 0 && ny < n
               && grid[nx][ny] != '*' && !vis[nx][ny])
            {
                vis[nx][ny] = 1;
                q.push(pii(nx, ny));
            }
        }
    }
}
int main()
{
    while(cin >> m >> n && (m + n))
    {
        cnt = 0;
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                cin >> grid[i][j];

        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(grid[i][j] == '@' && !vis[i][j])
                {
                    cnt++;
                    vis[i][j] = 1;
                    bfs(i, j);
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值