Codeforces Round #533 (Div. 2) D. Kilani and the Game

D. Kilani and the Game

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Kilani is playing a game with his friends. This game can be represented as a grid of size n×mn×m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell).

The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player ii can expand from a cell with his castle to the empty cell if it's possible to reach it in at most sisi (where sisi is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that.

The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends.

Input

The first line contains three integers nn, mm and pp (1≤n,m≤10001≤n,m≤1000, 1≤p≤91≤p≤9) — the size of the grid and the number of players.

The second line contains pp integers sisi (1≤s≤1091≤s≤109) — the speed of the expansion for every player.

The following nn lines describe the game grid. Each of them consists of mm symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit xx (1≤x≤p1≤x≤p) denotes the castle owned by player xx.

It is guaranteed, that each player has at least one castle on the grid.

Output

Print pp integers — the number of cells controlled by each player after the game ends.

Examples

input

Copy

3 3 2
1 1
1..
...
..2

output

Copy

6 3 

input

Copy

3 4 4
1 1 1 1
....
#...
1234

output

Copy

1 4 3 3 

Note

The picture below show the game before it started, the game after the first round and game after the second round in the first example:

In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.

题目大意:有q个人,每个人一开始有至少一个城堡,q个人轮流扩展(向四周)自己的城堡,第i个人能扩展v[i]步,当无人能继续扩展时结束,问每个人最终能有多少个城堡

注意:可能最终还有空白,如

.....

.###.

.#1#.

.###.

代码:

#include<bits/stdc++.h>
using namespace std;
char board[1005][1005];
int vis[1005][1005],n,m,q,v[10],size[10];
struct node
{
    int x,y,dis;
    node(){}
    node(int x,int y,int dis):x(x),y(y),dis(dis){}
};
queue<node> qu[10];
int mov[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
inline bool check(int x,int y)
{
    if(!vis[x][y] && x >=0 && x < n && y >= 0 && y < m)
        return true;
    return false;
}
int main()
{
    memset(vis,0,sizeof(vis));
    scanf("%d%d%d",&n,&m,&q);
    for(int i = 1;i <= q;++i)
        scanf("%d",&v[i]);
    for(int i = 1;i <= q;++i)
        while(!qu[i].empty())
            qu[i].pop();
    for(int i = 0;i < n;++i)
    {
        scanf("%s",board[i]);
        for(int j = 0;j < m;++j)
        {
            if(board[i][j] != '.')
            {
                vis[i][j] = 1;
            }
            if(board[i][j] != '.' && board[i][j] != '#')
                qu[board[i][j]-'0'].push(node(i,j,0));
        }
    }
    for(int i = 1;i <= q;++i)
        size[i] = qu[i].size();
    int turn = 0;
    while(++turn)
    {
        bool flag = false;
        for(int i = 1;i <= q;++i)
        {
            node nn,nn2;
            while(!qu[i].empty())
            {
                nn = qu[i].front();
                if(nn.dis >= turn * v[i])
                    break; 
                qu[i].pop();
                for(int j = 0;j < 4;++j)
                {
                    nn2.x = nn.x + mov[j][0];
                    nn2.y = nn.y + mov[j][1];
                    nn2.dis = nn.dis + 1;
                    if(check(nn2.x,nn2.y))
                    {
                        vis[nn2.x][nn2.y] = 1;
                        ++size[i];
                        qu[i].push(nn2);
                    }
                }
            }
            if(!qu[i].empty())
                flag = true;
        }
        if(!flag)
            break;
    }
    for(int i = 1;i <= q;++i)
        printf("%d ",size[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xing_mo/article/details/86569557