博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Kilani and the Game CodeForces - 1105D (bfs)
阅读量:5053 次
发布时间:2019-06-12

本文共 4072 字,大约阅读时间需要 13 分钟。

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 (1n,m10001≤n,m≤1000, 1p91≤p≤9) — the size of the grid and the number of players.

The second line contains pp integers sisi (1s1091≤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 (1xp1≤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 21 11.......2
Output
6 3
Input
3 4 41 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
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 1e3+5;int n,m,t;struct node{ int x,y,t;};int dx[]={ 1,-1,0,0};int dy[]={ 0,0,1,-1};int s[20];queue
q1[20];queue
q2[20];char map[maxn][maxn];int vis[maxn][maxn];int expand(int p){ int newx = 0; while(!q2[p].empty()) { node x = q2[p].front(); q2[p].pop(); x.t = 0; //此句话要与后面有句话连起来看,就是每次放进 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 = 0; i < 4; i++) { int xx = x.x + dx[i]; int yy = x.y + dy[i]; if(xx<1 || yy<1 || xx>n || yy>m || map[xx][yy]=='#' || vis[xx][yy] != 0 || x.t+1>s[p]) continue; newx++; q1[p].push(node{xx,yy,x.t+1}); //步数+1 vis[xx][yy] = p; //记录棋盘 } } if(newx >= 1) return 1; else return 0;}int main(){ scanf("%d %d %d",&n,&m,&t); for(int i=1;i<=t;i++) scanf("%d",&s[i]); for(int i=1;i<=n;i++) { for (int j = 1; j <= m; j++) { cin >> map[i][j]; if (map[i][j] >= '1' && map[i][j] <= '9') { vis[i][j] = map[i][j] - '0'; q2[vis[i][j]].push(node{i,j,0}); } } } while(true) { int flag = 0; for(int i=1;i<=t;i++) flag += expand(i); if(flag == 0) break; } int count[20]; memset(count,0,sizeof count); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) count[vis[i][j]]++; for(int i=1;i<=t;i++) printf("%d ",count[i]);}/*Input3 3 21 11.......2Output6 3Input3 4 41 1 1 1....#...1234Output1 4 3 3 */

 

转载于:https://www.cnblogs.com/smallhester/p/10322572.html

你可能感兴趣的文章
201521123069 《Java程序设计》 第4周学习总结
查看>>
线性表的顺序存储——线性表的本质和操作
查看>>
【linux】重置fedora root密码
查看>>
pig自定义UDF
查看>>
输入名字显示其生日,没有则让输入生日,做记录
查看>>
Kubernetes 运维学习笔记
查看>>
并查集 经典 畅通工程
查看>>
Spark MLlib 之 Naive Bayes
查看>>
php修改SESSION的有效生存时间
查看>>
spring security 11种过滤器介绍
查看>>
Hibernate一对多、多对一关联
查看>>
一、记录Git使用中遇到的问题及解决方法
查看>>
学习网址
查看>>
前端表格插件datatables
查看>>
内部类
查看>>
树链剖分入门
查看>>
图解算法时间复杂度
查看>>
UI_搭建MVC
查看>>
一个样例看清楚JQuery子元素选择器children()和find()的差别
查看>>
代码实现导航栏分割线
查看>>