#include <bits/stdc++.h>
using namespace std;

#define X first
#define Y second

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

string board[1005];
int canExtend[1005][1005];
int dist[10];
int area[10];
queue<tuple<int,int,int>> q[10];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,m,s; 
    cin >> n >> m >> s;
    for(int i=1; i<=s; i++) cin >> dist[i];
    char ch;
    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            cin >> ch;
            if(ch=='.') canExtend[i][j]=1;
            else if(ch=='#') canExtend[i][j]=0;
            else {
                canExtend[i][j]=0;
                area[ch-'0']+=1;
                q[ch-'0'].push({i,j,0});
            }
        }
    }

    while(1){
        int isExend=0;
        for(int i=1; i<=s; i++){
            queue<tuple<int,int,int>> nxt;
            while(!q[i].empty()){
                int x,y,moved;
                tie(x,y,moved)=q[i].front(); q[i].pop();
                if(moved==dist[i]) {
                    nxt.push({x,y,0});
                    continue;
                }
                for(int idx=0;idx<4; idx++) {
                    int nx=x+dx[idx];
                    int ny=y+dy[idx];
                    if(nx<0 || nx>=n || ny <0 || ny>=m) continue;
                    if(!canExtend[nx][ny]) continue;

                    q[i].push({nx,ny,moved+1});
                    area[i]+=1;
                    canExtend[nx][ny]=0;
                    isExend=1;
                }
            }
            q[i]=nxt;
        }
        if(!isExend) break;
    }
    for(int i=1; i<=s; i++) {
        cout << area[i] << ' ';
    }
    return 0;
}
/*
    tie(x, y, moved) = q[i].front();
    if(moved == dist[i]) break;
    
    q[i].pop();
    '
    '
    '
    '
    queue<tuple<int,int,int>> tmp;
    while(!q[i].empty()){
        int x,y,mm;
        tie(x,y,mm)=q[i].front(); q[i].pop();
        tmp.push({x,y,mm})
    }
    q[i]=tmp;
*/

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: