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
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
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就可以了,记录一下什么先跑什么后跑就好了,一开始题意理解错了以为不可以转弯。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+; int n,m,t;
struct node{
int x,y,t;
};
int dx[]={,-,,};
int dy[]={,,,-};
int s[];
queue<node>q1[];
queue<node>q2[];
char map[maxn][maxn];
int vis[maxn][maxn]; int expand(int p)
{
int newx = ;
while(!q2[p].empty())
{
node x = q2[p].front();
q2[p].pop();
x.t = ; //此句话要与后面有句话连起来看,就是每次放进 q2[i] 队列的都要将步数为0
q1[p].push(x);
}
while(!q1[p].empty())
{
node x = q1[p].front();
q1[p].pop();
if (x.t == s[p])
{
q2[p].push(x); //如果已经满 s[p] 步,就将它重新放进 q1 的队列,并将步数归0
continue;
} for (int i = ; i < ; i++)
{
int xx = x.x + dx[i];
int yy = x.y + dy[i];
if(xx< || yy< || xx>n || yy>m || map[xx][yy]=='#' || vis[xx][yy] != || x.t+>s[p])
continue;
newx++;
q1[p].push(node{xx,yy,x.t+}); //步数+1
vis[xx][yy] = p; //记录棋盘
}
}
if(newx >= )
return ;
else
return ;
}
int main()
{
scanf("%d %d %d",&n,&m,&t);
for(int i=;i<=t;i++)
scanf("%d",&s[i]);
for(int i=;i<=n;i++)
{
for (int j = ; j <= m; j++)
{
cin >> map[i][j];
if (map[i][j] >= '' && map[i][j] <= '')
{
vis[i][j] = map[i][j] - '';
q2[vis[i][j]].push(node{i,j,});
}
}
} while(true)
{
int flag = ;
for(int i=;i<=t;i++)
flag += expand(i);
if(flag == )
break;
}
int count[];
memset(count,,sizeof count);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
count[vis[i][j]]++;
for(int i=;i<=t;i++)
printf("%d ",count[i]);
}
/*
Input
3 3 2
1 1
1..
...
..2
Output
6 3
Input
3 4 4
1 1 1 1
....
#...
1234
Output
1 4 3 3
*/

Kilani and the Game CodeForces - 1105D (bfs)的更多相关文章

  1. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

  2. codeforces #Round354-div2-D(BFS)

    题目链接:题目链接 题意:一个n*m的区域,每个格子都有上下左右四个门,相邻的两个格子A可以通向B当且仅当A对B的门和B对A的门都打开,问从起点S到终点T需要的最短时间 #include<bit ...

  3. Fire Again CodeForces - 35C (BFS)

    After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows ...

  4. Statues CodeForces - 129C(bfs)

    In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the oppo ...

  5. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  6. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  9. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

随机推荐

  1. Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22499   Accepted: 7679 De ...

  2. LR C语言语句复习,几个简单代码

    嵌套循环 Action() { int i,j; ;i<=;i++) { ) beark; else lr_output_message("i=%d",i); ;j<= ...

  3. ORACLE将查询的多条语句拼在一个字段下

    select listagg(字段名,'分隔符') within group (order by 某个字段)

  4. (转载)C#中的lock关键字

    lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互斥锁来实现的. 先来看看执行过程,代码示例如下: 假设线程A先执行,线程B稍微慢一点.线程A执 ...

  5. https 双向验证

    服务器配置 服务器秘钥   服务器公钥证书  ,客户端公钥证书 客户端配置  客户端秘钥+密码 服务器公钥证书 目前android验证ok,pc浏览器添加客户端秘钥证书  ,访问还是失败,待继续查找资 ...

  6. N 叉树的层序遍历

    给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右,逐层遍历). 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4], [5,6] ] 说明: 树的深度不会超过 100 ...

  7. [转贴] 2016一月12日起.NET 4, 4.5 and 4.5.1 停止安全更新、技术支持 or hotfix

    [转贴] 2016一月12日起.NET 4, 4.5 and 4.5.1 停止安全更新.技术支持 or hotfix https://www.dotblogs.com.tw/mis2000lab/20 ...

  8. POJ 3050 Hopscotch(dfs,stl)

    用stack保存数字,set判重.dfs一遍就好.(或者编码成int,快排+unique #include<cstdio> #include<iostream> #includ ...

  9. hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...

  10. 为什么L1稀疏,L2平滑?

    使用机器学习方法解决实际问题时,我们通常要用L1或L2范数做正则化(regularization),从而限制权值大小,减少过拟合风险.特别是在使用梯度下降来做目标函数优化时,很常见的说法是,  L1正 ...