题目来源:http://codeforces.com/contest/1105/problem/D

题意:编号为1-k的点在一张n*m的表格中依次扩散,每个结点有各自的扩散速度且只可以往上下左右四个方向扩散,表格中每个空白的区域只能被一个结点占领,求最后各个结点所占领的区域的数量。

解题思路:我们可以定义两个队列:que1,que2,que1存放源点的信息,每次取que1中编号相同的结点的信息加入剩余扩散的步数存放入que2中,对que2的结点进行BFS,这样就能保证结点的扩散顺序了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
inline ll gcd(ll i,ll j){
return j==?i:gcd(j,i%j);
}
inline ll lcm(ll i,ll j){
return i/gcd(i,j)*j;
}
const int maxn=1e3+;
int vis[maxn][maxn];
int ans[maxn];
char mp[maxn][maxn];
int v[maxn];
int m,n,p;
int dir[][]={{-,},{,},{,-},{,}};
bool jug(int x,int y){
if(x<||x>=m||y<||y>=n||vis[x][y]!=||mp[x][y]!='.')
return false;
return true;
}
struct node{
int x,y,id;
node(){
x=y=id=;
}
node(int x,int y,int id):x(x),y(y),id(id){
}
};
struct node1{
int x,y,wi,id;
node1(int x,int y,int wi,int id):x(x),y(y),wi(wi),id(id){
}
node1(){
x=y=wi=id=;
}
};
queue<node> que;
void bfs(){
while(!que.empty()){
node tem=que.front();
que.pop();
queue<node1> que1;
//cout<<tem.id<<endl;
que1.push(node1(tem.x,tem.y,v[tem.id],tem.id));
while(!que.empty()&&que.front().id==tem.id){
que1.push(node1(que.front().x,que.front().y,v[tem.id],tem.id));//找到编号相同的点同时进行扩散
que.pop();
}
while(!que1.empty()){
node1 tem1;
tem1=que1.front();
que1.pop();
int dx,dy;
if(tem1.wi<=){
que.push(node(tem1.x,tem1.y,tem1.id));
continue;
}
for(int i=;i<;i++){
dx=tem1.x+dir[i][];
dy=tem1.y+dir[i][];
if(jug(dx,dy)){
vis[dx][dy]=tem1.id;
ans[tem1.id]++;
que1.push(node1(dx,dy,tem1.wi-,tem1.id));
}
}
}
}
}
int main(){
scanf("%d%d%d",&m,&n,&p);
for(int i=;i<=p;i++){
scanf("%d",&v[i]);
}
for(int i=;i<m;i++){
scanf("%s",mp[i]);
}
for(int i=;i<=p;i++){
for(int j=;j<m;j++){
for(int k=;k<n;k++){
if(mp[j][k]==i+''){
vis[j][k]=i;
ans[i]++;
que.push(node(j,k,i));//保证结点按顺序扩散
}
}
}
}
bfs();
/*for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(vis[i][j]==0){
cout<<"#"<<" ";
}
else
cout<<vis[i][j]<<" ";
}
cout<<endl;
}*/
for(int i=;i<=p;i++){
cout<<ans[i]<<" ";
}
return ;
}

D. Kilani and the Game(多源BFS)的更多相关文章

  1. CodeForces - 1105D Kilani and the Game(多源BFS+暴力)

    题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...

  2. CF 986A Fair——多源bfs

    题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...

  3. 牛客网 牛客练习赛7 D. 珂朵莉的无向图(多源BFS)

    题目链接  Problem D 比赛的时候完全想不到 直接对给定的这些点做多源$BFS$,把给定的这些点全都压到队列里,然后一个个做. 最后统计被访问的点的个数即可. #include <bit ...

  4. bzoj 2252 [ 2010 Beijing wc ] 矩阵距离 —— 多源bfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2252 又没能自己想出来... 一直在想如何从每个1开始广搜更新答案,再剪剪枝,什么遇到1就不 ...

  5. D. Kilani and the Game 解析(裸BFS、實作)

    Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...

  6. Codeforces H. Kilani and the Game(多源BFS)

    题目描述: Kilani and the Game time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  8. Kilani and the Game CodeForces - 1105D (bfs)

    沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...

  9. CF 986A Fair(多源BFS)

    题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...

随机推荐

  1. python中表示False的一些内置对象

    By default, an object is considered true unless its class defines either a __bool__() method that re ...

  2. spring中使用@PostConstruct和@PreConstruct注解

    1.@PostConstruct说明 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法.被@PostCo ...

  3. 安装phpssdbadmin

    1:先安装nginx+php   参考博文:http://www.cnblogs.com/lemon-le/p/7161356.html 里面有详细的搭建lnmp架构,这里只需参照安装php和ngin ...

  4. easyUI默认图标的使用

    使用格式如下: <table id="table" class="easyui-datagrid" style="width:600px;hei ...

  5. 根据CPU核心数确定线程池并发线程数

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  6. PHP批量保存图片到服务器再上传阿里云

    /* * 批量传输产品主图到阿里云 */ public function transferImage(){ $num = 50; $p = isset($this->request->ge ...

  7. 命令:jstack(查看线程)、jmap(查看内存)和jstat(性能分析)命令

    命令:jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令 这些命令 必须 在 linux jdk bin 路径 下执行 eq: ./jstack 10303 即可  如果想把 ...

  8. multiprocessing还是threading?

    今夜看了一篇分析python中多进程与多线程优劣的文章,文章通过几组性能测试强调了多进程的性能优势,同时也深入分析了为何python中多线程性能较差的原因,GIL就是解释器全局锁,该机制限制每个pyt ...

  9. 一次Windows 安装问题

    在 thinpad x250上安装 windows 10时,提示"安装程序无法创建新的系统分区,也无法定位现有系统分区". x250 的主板上自带一个16G的闪存且标识为 主分区, ...

  10. 20165312 2017-2018-2《Java程序设计》课程总结

    20165312 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安 ...