D. Kilani and the Game(多源BFS)
题目来源: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)的更多相关文章
- CodeForces - 1105D Kilani and the Game(多源BFS+暴力)
题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...
- CF 986A Fair——多源bfs
题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...
- 牛客网 牛客练习赛7 D. 珂朵莉的无向图(多源BFS)
题目链接 Problem D 比赛的时候完全想不到 直接对给定的这些点做多源$BFS$,把给定的这些点全都压到队列里,然后一个个做. 最后统计被访问的点的个数即可. #include <bit ...
- bzoj 2252 [ 2010 Beijing wc ] 矩阵距离 —— 多源bfs
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2252 又没能自己想出来... 一直在想如何从每个1开始广搜更新答案,再剪剪枝,什么遇到1就不 ...
- D. Kilani and the Game 解析(裸BFS、實作)
Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...
- 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 ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Kilani and the Game CodeForces - 1105D (bfs)
沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...
- CF 986A Fair(多源BFS)
题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...
随机推荐
- Django学习笔记之模板
模板 模板介绍 在之前的章节中,视图函数只是直接返回文本,而在实际生产环境中其实很少这样用,因为实际的页面大多是带有样式的HTML代码,这可以让浏览器渲染出非常漂亮的页面. 目前市面上有非常多的模板系 ...
- py-day3 python 全局变量和局部变量
# 全局变量 如果函数的内容无 global关键字,优先读取全局变量,无法对全局变量重新赋值, name = 'mj' def change_name(): print('change_name',n ...
- SoundManager 2 / API Demo and Code Examples
http://www.schillmania.com/projects/soundmanager2/
- Intellij IDEA 为常用代码添加快捷代码,补全代码
- ijkplayer总结
12.ijkplayer的使用过程: 11.ijkpalyer引言: ==== 12.ijkplayer的使用过程: >>举例mac系统编译.so文件: ijkplayer默认是不支持 ...
- python中赋值,深拷贝,浅拷贝区别
这三种 的区别就是 复制的变量 是否是原变量的引用. 赋值:只是原变量的引用. 浅拷贝和深拷贝的区别 需要通过 子元素 区分 浅拷贝:子元素的 引用相同 深拷贝:所以引用都不相同,完全复制一份 这三种 ...
- scrapy-pipeline的方法
scrapy中多个pipeline作用: 一个项目可能需要爬取多个网站,根据每个网站的数据量(处理方式)不同,可创建多个管道 pipeline class SpideranythingPipeline ...
- ajax多个请求执行顺序
先说结论再说原因 结论:比如点击事件触发了两个ajax请求或者更多的请求,是没有执行顺序的,各个请求的快慢完全取决于返回的快慢,或许你在浏览器调试窗口看见的是先发了一个请求,再发了一个,但是完全没有等 ...
- Java 中 Gson的使用
JSON 是一种文本形式的数据交换格式,它比XML更轻量.比二进制容易阅读和编写,调式也更加方便;解析和生成的方式很多,Java中最常用的类库有:JSON-Java.Gson.Jackson.Fast ...
- linux安装jdk8
1.文件准备 jdk-8u201-linux-x64.tar.gz 下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...