http://codeforces.com/problemset/problem/1105/D

题意:有一片矩阵区域,一开始有多个势力比如1,2,3,4....9,从势力1开始轮流向外扩张,地图上为‘.’可以占领,‘#’则不能占领,被占领后不能再次被别人占领,只能上下左右扩张,每个势力有一个扩张速度,求最后整片区域上各个势力占了多少格子。

解题:

1.显然bfs,但是速度有大有小,多向bfs

2.输入的数据同一势力的起点可能不止一个(坑)

3.从势力1开始轮流扩张,不是队列一次走到头

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int n,m,p;
int last[];///上一次压入了多少个点
struct node
{
int x;
int y;
};
int s[];///速度
char a[][];
int vis[][];
int ans[];
int can[];///是否走投无路
queue<node>que[];
int d[][]={ {-,}, {,}, {,-}, {,} };///上下左右 void bfs()
{
for(int i=;i<=p;i++)///清空
{
while(!que[i].empty())
que[i].pop();
}
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if( a[i][j]!='.' && a[i][j]!='#' )
{
que[ a[i][j]-'' ].push( {i,j} );///压入起点,可能不止一个
vis[i][j]=a[i][j]-'';
last[ a[i][j]-'' ]++; }
}
}
bool flag=true;
while(flag)///有人还能延伸就继续
{
flag=false;
for(int idx=;idx<=p;idx++)///每个队列走一次
{
for(int ss=;ss<=s[idx] && can[idx]== ;ss++)///扩展速度,能走多少步,s[i]可能很大超时,用can数组截断
{
int num=last[idx];///本次允许弹出去扩张的节点的数量,即上次压入的节点数量
last[idx]=;///新一轮扩张加入的节点要清空
for(int t=;t<=num;t++)
{
node now=que[idx].front();
que[idx].pop();
ans[idx]++;///取队头,弹出的时候区域+1
for(int i=;i<;i++)
{
int xx=now.x+d[i][];
int yy=now.y+d[i][];
if( xx>= && xx<n && yy>= && yy<m && vis[xx][yy]== && a[xx][yy]=='.' )
{
que[idx].push( {xx,yy} );
vis[xx][yy]=idx;///压入队列,标记访问过
last[idx]++;///本次压入点的个数
}
} }
if(que[idx].empty())
can[idx]=idx;///走投无路,美德扩展了
}
}
for(int i=;i<=p;i++)
{
if(!que[i].empty())
flag=true;
}
}
for(int i=;i<=p;i++)
printf("%d ",ans[i]);
printf("\n");
} int main()///CF1105D
{
while(scanf("%d %d %d",&n,&m,&p)!=EOF)
{
memset(a,,sizeof(a));
memset(s,,sizeof(s));
memset(ans,,sizeof(ans));
memset(vis,,sizeof(vis));
memset(last,,sizeof(last));
memset(can,,sizeof(can));
for(int i=;i<=p;i++)///速度
scanf("%d",&s[i]);
getchar();
for(int i=;i<n;i++)///地图
scanf("%s",a[i]); bfs();
}
return ;
}

CF1105D-Kilani and the Game-(多向bfs)的更多相关文章

  1. Codeforces 1105D Kilani and the Game【BFS】

    <题目链接> 题目大意: 每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色.在所有颜色不能在继续扩张的时候停止游戏.询问此时各种颜色的数量. 解题分析: ...

  2. [CF1105D]Kilani and the Game

    题目大意:给出一个$n\times m(n,m\leqslant10^3)$的地图,有$k(k\leqslant9)$个玩家,第$i$个玩家速度为$s_i$.地图中$\#$代表障碍:$.$ 代表空地: ...

  3. Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

    题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...

  4. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

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

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

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

    Kilani is playing a game with his friends. This game can be represented as a grid of size n×mn×m, wh ...

  7. 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 ...

  8. D. Kilani and the Game(多源BFS)

    题目来源:http://codeforces.com/contest/1105/problem/D 题意:编号为1-k的点在一张n*m的表格中依次扩散,每个结点有各自的扩散速度且只可以往上下左右四个方 ...

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

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

随机推荐

  1. [转帖]k8s 基本使用(上)

    k8s 基本使用(上) https://www.jianshu.com/p/8d60ce1587e1 本文将介绍 k8s 中的一些最基本的命令,并辅以解释一些基本概念来方便理解,也就是说,本文是一篇偏 ...

  2. 62 网络编程(三)——UDP编程

    UDP编程标准步骤 服务器端 使用DatagramSocket创建服务端:DatagramSocket server = new DatagramSocket(port);//参数为自定义端口号 准备 ...

  3. Java学习:数组的使用和注意事项

    数组 数组的概念:是一种容器,可以同时存放多个数据值 数组的特点: 数组是一种引用数据类型 数组当中的多个数据,类型必须统一 数组的长度在程序运行期间不可以改变 数组的初始化:在内存当中创建一个数组, ...

  4. XML Schema 基本结构

    <?xml version='1.0'?> <Schema name="cangchuSchema" metamodelVersion="4.0&quo ...

  5. js组件

    最近学习了一下js组件相关知识,但找到的资料比较少,一知半解,先做个简单的笔记吧. 首先定义一个类,可以在里面添加方法: //这是个下拉框组件,放在select.js里 var tree = { tr ...

  6. C#读写设置修改调整UVC摄像头画面-曝光

    有时,我们需要在C#代码中对摄像头的曝光进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...

  7. 2019 草花手游java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.草花手游等公司offer,岗位是Java后端开发,因为发展原因最终选择去了草花手游,入职一年时间了,也成为了面 ...

  8. jQuery 前端复选框 全选 反选 下拉菜单联动

    jQuery 页面中复选框全选.反选.下拉联动(级联) <!DOCTYPE html> <html lang="en"> <head> < ...

  9. ES6 字符串&正则表达式

    目录 第二章 字符串和正则表达式UTF-16码位codePointAt()方法String.fromCodePoint()方法normalize()方法正则表达式u修饰符其他字符串变更字符串中的字串识 ...

  10. select用法 多并发处理

    select默认最大检查套接口数量是1024,有定义 #define __NFDBITS (8 * sizeof(unsigned long)) #define __FD_SETSIZE 1024 # ...