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. Oracle子查询和多表查询

    多表查询需要用到表的连接 连接可以分为:(自行百度) 交叉连接(数字逻辑的笛卡尔积,不做解释) 等值连接 例如:select * from t_a, t_b where t_a.xx = t_b.xx ...

  2. java常见数据结构举例

    1. ArrayList(参考) import java.util.*; public class Test{ public static void main(String [] args){ Arr ...

  3. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  4. 白话SpringCloud | 第零章:前言

    说在前面 大清早醒来,觉得睡不着了.还是起来,写写博客.但最后发现关于SpringBoot的安全相关的还是比较多内容的,也比较专业,怕是一个多小时完不成的,也罢,那就来写写关于SpringCloud前 ...

  5. 使用Quartz任务调用的时候报错Based on configured schedule, the given trigger will never fire.

    org.quartz.SchedulerException: Based on configured schedule, the given trigger will never fire. 大概意思 ...

  6. ajax post方式表单提交的注意事项。

    当我们创建一个异步对象XMLHttpRequest同时post方式向后台传输数据的时候. 我们要设置异步对象的xhr.setRequestHeader成员的值为 XMLHttpRequest.setR ...

  7. 安卓usb数据接收

    之前在论坛里面求助了关于监听数据接收的问题,因为第一次做这方面,可能我提的问题太简单了,大神都不愿意回答我,(之前的帖子)晚上FQ浏览网站发现问题的解决办法, 原文是:最近老板让弄安卓和一块板子通信, ...

  8. 【Python】python2 html safe string

    import cgi s = '<>&' s += u'哈哈' print type(s) print s r = cgi.escape(s) print type(r) prin ...

  9. MATLAB之易经卜卦程序+GUI

    MATLAB之易经卜卦程序+GUI   日月为易,刚柔相推. 是故易有太极,是生两仪,两仪生四象,四象生八卦,八卦定吉凶,吉凶生大业.是故法象莫大乎天地,变通莫大乎四时,悬象著明莫大乎日月.   本文 ...

  10. cms-首页搭建

    主页面主要有3个部分构成,头部.主体内容部分.尾部 1.头部: <%@ page language="java" contentType="text/html; c ...