题目意思很简单,求最少几个连通快的面积加起来 才能 >= 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;
}