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. [转] js async await 终极异步解决方案

    阅读目录 回顾 Promise async await 字面理解 async.await 如何执行 await 操作符 总结 既然有了promise 为什么还要有async await ? 当然是pr ...

  2. MethodInvoker委托,跨线程访问

    Invoke(new MethodInvoker(delegate { textBox1.Enabled = true; })); 上面是简单缩写,也可以写成 private void btnOK_C ...

  3. ABP 执行sql语句

    由于业务繁琐,用EF比较麻烦,需要用到sql语句,然后网上找了很久,找到的例子都是老版本的,新版本有先声明已经去掉,不能用了 在这里做个小记 首先注入实例 private readonly IDbCo ...

  4. C# 网络连接中异常断线的处理:ReceiveTimeout, SendTimeout 及 KeepAliveValues(设置心跳)

    C# 网络连接中异常断线的处理:ReceiveTimeout, SendTimeout 及 KeepAliveValues(设置心跳) 在使用 TcpClient 网络连接中常常会发生客户端连接异常断 ...

  5. Kafka分布式的消息顺序

    Kafka分布式的单位是partition,同一个partition用一个write ahead log组织,所以可以保证FIFO的顺序.不同partition之间不能保证顺序. 但是绝大多数用户都可 ...

  6. Geany——Python配置

    Geany是一个很不错的编辑器,操作很简单,这里记录一下Geany的入手设置(在下是一个Python程序猿,就以Python为例): 1:新建:选择 下拉菜单中的  main.py  ,然后就能生成P ...

  7. Java自学-类和对象 属性初始化

    如何进行Java的属性初始化 步骤 1 : 对象属性初始化 对象属性初始化有3种 声明该属性的时候初始化 构造方法中初始化 初始化块 . public class Hero { public Stri ...

  8. 基于OpenGL的三维曲面动态显示实现

    在使用Visual C++的MFC AppWizard建立应用程序框架后,生成了多个类,与OpenGL编程相关的类是视图类,主要的显示任务都在其中完成. 1.基于OpenGL绘图的基本设置 1.1 设 ...

  9. iOS - 适配 iOS 13 之工兵连扫雷

    iOS 13 支持适配的机型 目前最新 iPhone 11.iPhone 11 Pro和iPhone 11 Pro Max iPhone X.iPhone XR.iPhone XS.iPhone XS ...

  10. 【转载】 C#中常见的泛型集合类有哪些

    在C#语言编程过程中,List集合类是最常见的泛型集合类,其实除了List集合,还有其他一些常用的泛型集合类,如字典类型Dictionary泛型集合类.先进先出的队列类型Queue泛型集合类.后进先出 ...