Codeforces1105D. Kilani and the Game(BFS+优先队列)

                                                                                                    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.

 

一、原题地址

点我传送

 

二、大致题意

    每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色。在所有颜色不能在继续扩张的时候停止游戏。询问此时各种颜色的数量。

 

三、大致思路

    对于一种颜色而言,每一次真正需要被更新的点其实是这种颜色最外围的一圈(很好理解,因为如果内部的点能够达到,那么外部的点肯定能够达到),什么叫做外围的一圈,可以理解的是每种颜色的速度,实际上就是意味着这种颜色能跑的格子数量。那么这里外围可以理解为那些拥有的剩余速度比较大的点(其实就是为了防止部分点被BFS中途提前打上标记)。那么用优先队列来维护这些最外圈的点,来保证下一个状态一定是最有效的点转移过去的。

  并且为了满足颜色是按照顺序更新的,需要开一个临时队列temp(这里不是优先队列,只是用来储存先后关系)。每次取出临时队列中同一种类的颜色进行更新。当临时队列中没有点时,表示状态已经没有地方可以更新了。这样做的复杂度大概就是遍历一张图所需的时间,代码跑了108ms。

 

四、代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const LL INF=1e17+10;

int n,m,p;
int vec[15];
struct Node
{
    int x,y,sec,ty;
    bool operator <(Node tet)const
    {
        return sec<tet.sec;
    }
    Node (int _x,int _y,int _s,int _ty)
    {
        x=_x;y=_y;sec=_s;ty=_ty;
    }
};
priority_queue<Node>q;
queue<Node>temp;
char mmp[1005][1005];
int ans[1005];
int SumK;
int x[]={0,0,1,-1};
int y[]={1,-1,0,0};
void BFS()
{
    int type=temp.front().ty;
    while(!temp.empty()&&temp.front().ty==type)
    {
        q.push(temp.front());temp.pop();
    }
    while(!q.empty())
    {
        Node t=q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=t.x+x[i],yy=t.y+y[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
            {
                if(mmp[xx][yy]=='.')
                {
                    mmp[xx][yy]=type+'0';
                    ans[type]++;
                    SumK--;
                    if(t.sec-1==0)
                        temp.push(Node(xx,yy,vec[type],type));
                    else
                        q.push(Node(xx,yy,t.sec-1,type));
                }
            }
        }
    }
    return ;
}

vector<Node>vv[15];


int main()
{
    scanf("%d %d %d",&n,&m,&p);
    for(int i=1;i<=p;i++)
        scanf("%d",&vec[i]),ans[i]=0;
    SumK=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%s",mmp[i]+1);
        for(int j=1;j<=m;j++)
        {
            if(mmp[i][j]>='1'&&mmp[i][j]<='9')
            {
                vv[mmp[i][j]-'0'].push_back(Node(i,j,vec[mmp[i][j]-'0'],mmp[i][j]-'0'));
                ans[mmp[i][j]-'0']++;
            }
            if(mmp[i][j]=='.')SumK++;
        }
    }
    for(int i=1;i<=p;i++)
    {
        int Size=vv[i].size();
        for(int j=0;j<Size;j++)
        {
            temp.push(vv[i][j]);
        }
    }
    while(!temp.empty()&&SumK)
    {
        BFS();
    }
    for(int i=1;i<=p;i++)
    {
        printf("%d ",ans[i]);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值