CF1105D-Kilani and the Game-(多向bfs)
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)的更多相关文章
- Codeforces 1105D Kilani and the Game【BFS】
<题目链接> 题目大意: 每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色.在所有颜色不能在继续扩张的时候停止游戏.询问此时各种颜色的数量. 解题分析: ...
- [CF1105D]Kilani and the Game
题目大意:给出一个$n\times m(n,m\leqslant10^3)$的地图,有$k(k\leqslant9)$个玩家,第$i$个玩家速度为$s_i$.地图中$\#$代表障碍:$.$ 代表空地: ...
- Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)
题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...
- 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 ...
- D. Kilani and the Game 解析(裸BFS、實作)
Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...
- 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 ...
- 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 ...
- D. Kilani and the Game(多源BFS)
题目来源:http://codeforces.com/contest/1105/problem/D 题意:编号为1-k的点在一张n*m的表格中依次扩散,每个结点有各自的扩散速度且只可以往上下左右四个方 ...
- Kilani and the Game CodeForces - 1105D (bfs)
沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...
随机推荐
- ZYNQ笔记(4):PL触发中断
一.ZYNQ中断框图 PL到PS部分的中断经过ICD控制器分发器后同时进入CPU1 和CPU0.从下面的表格中可以看到中断向量的具体值.PL到PS部分一共有20个中断可以使用.其中4个是快速中断.剩余 ...
- groovy常用语法及实战
groovy语言简介 一种基于JVM的敏捷开发语言,作为编程语言可编译成java字节码,也可以作为脚本语言解释执行. 结合了Python.Ruby和Smalltalk的许多强大的特性 支持面向对象编程 ...
- [Atcoder AGC032C]Three Circuits
题目大意:有一张$n$个点$m$条边的无向连通图,判断是否可以从中分出$3$个环,满足三个环覆盖整张图并且没有重复的边.$n,m\leqslant10^5$ 题解:分类讨论.有度数为奇肯定不行,因为连 ...
- 可落地的DDD(3)-如何利用DDD进行微服务的划分
摘要 前面两篇介绍了DDD的目标管理.DDD的工程结构调整.这篇讨论微服务的划分.微服务是目前后端比较流行的架构体系了,那么如何做好一个微服务的划分?一个微服务的粒度应该是多大呢?这篇主要介绍如何结合 ...
- 批量修改Ms SqlServer 的default(默认值)
原文:批量修改Ms SqlServer 的default(默认值) --1.取得数据库所有表的默认值: select t3.name as 表名,t1.name as 字段名,t2.text as 默 ...
- .net core 引入SwaggerUI教程
Swagger Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.方便前后端接口对接. 1.打开NuGet程序包,搜索“Swashbuckle ...
- asp.net core 系列之Reponse caching 之 Response Caching Middleware(4)
这篇文章介绍 Response Caching Middleware . Response Caching Middleware in ASP.NET Core 通过在ASP.NET Core应用中 ...
- Web API 授权筛选器
方式一.全局认证 public static class WebApiConfig { public static void Register(HttpConfiguration config) { ...
- 【开发笔记】- 将MySQL数据库表中自增ID从0开始
命令: 用于清空某表的数据 且让自增的id重新从0开始 truncate table 你的表名
- 坑人的Mysql5.7 (默认不支持Group By语句)(转)
部署项目时,项目启动完毕.点击有group by 查询语句时出现错误,界面中没有该有的数据.查询log日志发现错误 Expression #1 of SELECT list is not in GRO ...