显然是模拟跑一边bfs就行,每次将所有属于这个点的格子全部pop并且push到另一个队列里,然后在这个队列进行bfs,扩张\(q_i\) 步终止,将新扩展到的格子加入原队列,但是要注意的是
AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int n, m, q;
char ss[1005][1005];
long long aa[15];
int anss[15] = {0};
int ds[2][4] = {{0, 1, 0, -1}, {1, 0, -1, 0}};
struct ab
{
int xx;
int yy;
int st;
};
vector<struct ab>aq[15];
queue<struct ab> qq;
void bfs2()
{
struct ab start = qq.front();
queue<struct ab> q;
int s = start.st;
int c = ss[start.yy][start.xx] - ‘0‘;
while (ss[start.yy][start.xx] - ‘0‘ == c)
{
q.push(start);
qq.pop();
if (qq.empty())
{
break;
}
start = qq.front();
}
while (!q.empty())
{
start = q.front();
q.pop();
if (start.st - s == aa[c])
{
break;
}
int xxx = start.xx;
int yyy = start.yy;
for (int i = 0; i < 4; ++i)
{
if (yyy + ds[0][i] >= 1 && yyy + ds[0][i] <= n && xxx + ds[1][i] <= m && xxx + ds[1][i] >= 1 && ss[yyy + ds[0][i]][xxx + ds[1][i]] == ‘.‘)
{
struct ab dd;
dd.xx = xxx + ds[1][i];
dd.yy = yyy + ds[0][i];
dd.st = start.st + 1;
ss[yyy + ds[0][i]][xxx + ds[1][i]] = c + ‘0‘;
q.push(dd);
if (dd.st - s == aa[c])
{
qq.push(dd);
}
++anss[c];
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= q; ++i)
{
scanf("%lld", &aa[i]);
}
for (int i = 1; i <= n; ++i)
{
scanf("%s", ss[i] + 1);
for (int j = 1; j <= m; ++j)
{
if (ss[i][j] >= ‘0‘ && ss[i][j] <= ‘9‘)
{
aq[ss[i][j] - ‘0‘].push_back((struct ab){j, i, 0});
++anss[ss[i][j] - ‘0‘];
}
}
}
for (int i = 1; i <= q; ++i)
{
for (int j = 0; j < aq[i].size(); ++j)
{
qq.push(aq[i][j]);
}
}
while (!qq.empty())
{
bfs2();
}
for (int i = 1; i < q; ++i)
{
printf("%d ", anss[i]);
}
if (q)
{
printf("%d\n", anss[q]);
}
return 0;
}
原文:https://www.cnblogs.com/int-me-X/p/14102091.html