题目来源: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. Tarjan求割点&桥

    概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 2.割点:无向连通图中,如果删 ...

  2. java_初始化器

    1. 执行的顺序 package java20180129_1; public class Demo { // instance variable initializer 实例变量初始化器 Strin ...

  3. Python中*args和**kwargs

    *args *args是可变的positional arguments列表 *args:将参数打包成元组(tuple)给函数调用 在函数中用 args 调用 **kwargs **kwargs是可变的 ...

  4. Docker之 默认桥接网络与自定义桥接网卡

    docker引擎会默认创建一个docker0网桥,它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和宿主机都放到同一个二层网络. 1. docker如何使用网桥 1.1 Linux虚拟网桥的特点 ...

  5. Web APP 日期选择控件

    github地址: https://github.com/yuanzm/simple-date-picker#simple-date-picker simple-date-picker 基于zepto ...

  6. sql job定时备份数据库

    ---------------------------------------- 对TestDB1进行备份 ---------------------------------------- decla ...

  7. unity的一些tips

    主要是我知乎上回答的一个关于unity的tip,备忘. 说说我所看到unity相关的,不好的习惯: 1 尽量不要在Awake(), start()等函数内加入业务逻辑的初始化代码.首先无法简便的直接启 ...

  8. workerman相关

    (1)workerman linxu 内核优化 http://doc.workerman.net/315302 (2)workerman 安装环境配置  http://doc.workerman.ne ...

  9. android AES 加密解密

    import java.security.Provider; import java.security.SecureRandom; import javax.crypto.Cipher; import ...

  10. 使用Oracle DBLink进行数据库之间对象的访问操作

    Oracle中自带了DBLink功能,它的作用是将多个oracle数据库逻辑上看成一个数据库,也就是说在一个数据库中可以操作另一个数据库中的对象,例如我们新建了一个数据database1,我们需要操作 ...