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 ...
随机推荐
- java初始重点语法
第三章 if基本语法: if(条件){// 表达式 // 代码块 } eg: int a = 10; if(a > 1){ System.out.println("内容"); ...
- java 实现excel 导出功能
实现功能:java导出excel表 1.jsp代码 <form id="zhanwForm" action="<%=path%>/conferences ...
- Centos7更改yum源
每次都要百度一番,还不如自己做个记录,简单粗暴,哈哈哈哈 cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.repo.old wget http: ...
- Visual Studio安装SVN插件
VS的SVN插件 材料 VS安装程序. VisualSVN安装程序,点击下载. VisualSVN-5.0.1 前期准备 在代码管理的服务器上安装SVN server,可参考svn安装部署以及服务器 ...
- Excel函数之vlookup的用法
Vlookup函数用法: 实例: 要将编号对照表中的图书名称根据两表中的图书编码字段引入 订单明细中. Vlookup函数 参数一:键入一个需要搜索的字段,这里需要通过订单明细中的图书编号在编号对照离 ...
- php语法基础(相比C语言)
前言 php的语法跟C语言很类似,相信有一定C的基础的人学起来会非常快. 本篇主要介绍php相比C语言有差异的地方 php代码标记 ASP标记:<% 代码 %> 短标记:<? 代码 ...
- Cookie深度解析
最近在公司做了Web端单点登录(SSO)功能,基于Cookie实现,做完之后感觉有必要总结一下,本文着重讲解Cookie,下文会说明单点登录的实现方案. Cookie简介 众所周知,Web协议(也就是 ...
- 2017-2018-2 20165312实验二《Java面向对象程序设计》实验报告
2017-2018-2 20165312实验二<Java面向对象程序设计>实验报告 实验中遇到的问题 1.增加MyUtil的测试类之后,TestCase是红色的,但是没有找到junit.j ...
- C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新)
历史版本C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有17年的历史,语言本身具有丰富的特性,微软对其更新支持也十分支持.微软将C#提交给标准组织ECMA,C# 5.0目前是ECMA ...
- Kettle在windows下分布式集群的搭建
集群的搭建 我这里用的是kettle7.1版本的 下载解压 我们打开kettle的安装目录,进入到data-integration->pwd目录,找到carte-config-master-80 ...