CodeForces

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
    int x,y;
    ll setp;
};
queue<node> Q,border[10];
const int maxn=1005;
char a[maxn][maxn];
int vis[maxn][maxn];
int n,m,p;
ll ans[10],s[10];
int f[4][2]={
        {1,0},{0,1},{0,-1},{-1,0}
};
void bfs(int w)
{
    node temp;
    int x,y;
    while(!Q.empty())
    {
        temp=Q.front();
        Q.pop();
        if(temp.setp==0)
            border[w].push(temp);
        else{
            for(int i=0;i<4;i++)
            {
                x=temp.x+f[i][0];
                y=temp.y+f[i][1];
                if(x<1||x>n||y<1||y>m||vis[x][y]!=0||a[x][y]!='.') continue;
                vis[x][y]=w;
                Q.push(node{x,y,temp.setp-1});
            }
        }
    }
}
bool expand(int w)
{
    node temp;
    while(!border[w].empty())
    {
        temp=border[w].front();
        border[w].pop();
        temp.setp=s[w];
        Q.push(temp);
    }
    bfs(w);
    return !border[w].empty();
}
int main()
{
    cin>>n>>m>>p;
    for(int i=1;i<=p;i++)
        cin>>s[i];
    for(int i=1;i<=n;i++)
        cin>>a[i]+1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]>='1'&&a[i][j]<='9')
            {
                int num=a[i][j]-'0';
                border[num].push(node{i,j,s[num]});
                vis[i][j]=num;
            }
        }
    }
    while(1)
    {
        bool flag= false;
        for(int i=1;i<=p;i++)
            flag|=expand(i);
        if(!flag)
            break;
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            ans[vis[i][j]]++;
    for(int i=1;i<=p;i++)
        cout<<ans[i]<<" ";
}
原文地址:https://www.cnblogs.com/033000-/p/10457144.html