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> # ...
随机推荐
- Java的多路分支代码,感觉有点意思
/** * @Author hty * @Date 2019-12-16 16:39 * @Version 1.0 */ import java.util.Random; // 比赛结果 enum O ...
- 关于 Windows to go
1. 在宿主计算器的操作系统中访问 Windows to go 的磁盘 如题,如果需要在宿主计算器的操作系统中访问 Windows to go 的U盘(移动硬盘)中的文件,只需要打开磁盘管理,“更改驱 ...
- In-Memory:哈希索引
SQL Server 2016支持哈希查找,用户可以在内存优化表(Memory-Optimized Table)上创建哈希索引(Hash Index),使用Hash 查找算法,实现数据的极速查找.在使 ...
- 在ASP.NET Core中获取客户端和服务器端的IP地址(转载)
随着ASP.NET的发展,有不同的方式从请求中访问客户端IP地址.WebForms和MVC Web应用程序只是访问当前HTTP上下文的请求. var ip = HttpContext.Current. ...
- vue日历/日程提醒/html5本地缓存
先上图 功能: 1.上拉日历折叠,展示周 2.左右滑动切换月 2.“今天”回到今天:“+”添加日程 3.localStorage存储日程 index,html <body> <div ...
- 三分钟掌握,使用Quqrtz.Net实现定时发送邮件
在实际的项目中,常遇到延时触发工作以及定时触发工作 这里所讲的是借助第三方的组件 Quartz.Net 来实现(源码位置:https://github.com/quartznet/quartznet) ...
- 如何在.Net Mvc中让Get,Post请求访问同一个Action的方法
[HttpPost] [ActionName("Index")] public ActionResult Post(Models.WeChatRequestModel model) ...
- Dubbo(三):框架设计
整体设计 图例说明: 图中左边淡蓝背景的为服务消费方使用的接口,右边淡绿色背景的为服务提供方使用的接口,位于中轴线上的为双方都用到的接口. 图中从下至上分为十层,各层均为单向依赖,右边的黑色箭头代表层 ...
- 《JavaScript高级程序设计》笔记:附录A ECMAScript Harmony
一般性变化 常量 用const关键字声明常量,声明的变量在初始赋值后,就不能进行修改了,如下代码: const MAX_SIZE = 25; MAX_SIZE = 10; //报错 块级作用域及其他作 ...
- JavaScript 数据类型(基本数据类型)
JavaScript 数据类型分为简单数据类型和复杂数据类型. 简单数据类别包括 Number.String.Boolean.Undefined 和 Null 共5种. 复杂数据类型只有一个 Obje ...