Bee Problem(连通块+贪心)

传送门

题目意思很简单,求最少几个连通快的面积加起来 才能 >= h。BFS完贪心排序一下就行,需要注意的是这个图跟普通的二维搜索不一样,对蜜蜂地图来说每一个蜂巢都是六边形,也就是说每个点都有六个相邻的点,此外,偶数行的点和奇数行的点也不一样。

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cin.tie(0);                  \
    // cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e3 + 10;
const int maxm = 1e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int dis1[6][2] = {0, 1, 1, 0, 0, -1, -1, 0, -1, -1, 1, -1};
int dis2[6][2] = {0, 1, 1, 0, 0, -1, -1, 0, -1, 1, 1, 1};
int vis[maxn][maxn];
char a[maxn][maxn];
int c[maxn * maxn];
int h, n, m, cnt = 0;
struct Node
{
    int x, y;
};
bool check(int x, int y)
{
    if (x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y] && a[x][y] == '.')
        return true;
    else
        return false;
}
void BFS(int x, int y)
{
    vis[x][y] = 1;
    queue<Node> q;
    q.push({x, y});
    int t = 1;
    while (!q.empty())
    {
        Node now = q.front();
        q.pop();
        if (now.x & 1)
        {
            for (int i = 0; i < 6; i++)
            {
                int tx = now.x + dis1[i][0];
                int ty = now.y + dis1[i][1];
                if (check(tx, ty))
                {
                    ++t, vis[tx][ty] = 1;
                    q.push({tx, ty});
                }
            }
        }
        else
        {
            for (int i = 0; i < 6; i++)
            {
                int tx = now.x + dis2[i][0];
                int ty = now.y + dis2[i][1];
                if (check(tx, ty))
                {
                    ++t, vis[tx][ty] = 1;
                    q.push({tx, ty});
                }
            }
        }
    }
    c[cnt++] = t;
}
bool cmp(int a, int b)
{
    return a > b;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    cin >> h >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (!vis[i][j] && a[i][j] == '.')
                BFS(i, j);
    sort(c, c + cnt, cmp);
    // for (int i = 0; i < cnt; i++)
    //     cout << c[i] << " ";
    // cout << endl;
    int sum = 0;
    if (h == sum)
        return cout << 0, 0;
    for (int i = 0; i < cnt; i++)
    {
        sum += c[i];
        if (sum >= h)
            return cout << (i + 1), 0;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值