题目来源: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. 记一次Chrome冒充QQ浏览器领取奖励之行

      DNF游戏十周年活动,但是看到活动页面竟然是QQ浏览器专属活动,可是对于QQ浏览器,我内心是拒绝的,所以本着能不下载就不下载的原则,当然是选择放弃它了..... 开玩笑,看到这一活动,虽然奖励不高 ...

  2. 前端 --- 5 BOM 和 DOM

    一.BOM BOM(Browser Object Model)是指浏览器对象模型, 它使 JavaScript 有能力与浏览器进行“对话”. 1. window 对象 一些常用的Window方法: ( ...

  3. Excel技巧--按内容分列与合并

    上表的A列,如果想要按横线分隔开多列,复制粘贴很麻烦,可以使用“数据”-->“分列”来分隔开: 1.选择A列,在A列后预先插入三列空列.点击“数据”—>“分列”,对话框选择按“分隔符号”分 ...

  4. Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和导出. 该 ...

  5. Windows启动配置数据(BCD)存储文件包含一些无效信息

    Windows启动配置数据(BCD)存储文件包含一些无效信息-照牛排 http://www.zhaoniupai.com/archives/223.html 1)近来封装Windows 7,遇到挫折. ...

  6. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

  7. ubuntu crontab python 定时任务备记

    crontab -e 写入: # at a.m every week with: # * * tar -zcf /var/backups/home.tgz /home/ # # For more in ...

  8. Linux-Shell基础(变量,字符串,数组)

    一. 什么是shell shell是一个用C语言编写的程序,它是用户使用Linux的桥梁,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统 ...

  9. JS高级-虚拟DOM

    virtual dom 虚拟DOM是Vue和React的核心 用JS模拟DOM结构 DOM变化的相比,放在JS层来做 遇到问题 DOM操作是“昂贵”的,js运行效率高 尽量减少DOM操作,而不是“推到 ...

  10. php多维数组排序

    数组 array(11) { [0] => array(7) { ["food_id"] => string(2) "31" ["food ...