Codeforces 1105D (BFS)
题面
分析
考虑BFS
while(棋盘没有满){
for 玩家 p{
对p进行BFS,走s[p]步
}
}
对于每个玩家p
BFS的时候如果到了格子(x,y),就把\(vis[x][y]\)标记为p
最后把vis扫一遍就统计出了每个玩家占领的个数
每次BFS时要把最外层的节点存下来,下一次BFS时直接从那些节点开始搜索
具体实现中对每个玩家维护两个队列q1,q2,队列中的每个元素(x,y,t)表示当前时间为t,位置(x,y)
初始化时向q2插入起点
function expand(p){
while(q2非空) 把q2的元素插入q1中,并将时间t设为0
while(q1非空){
x=q1.front()
q1.pop()
if(x的时间为s){
q2.push(x)
continue
}
从x向四周BFS
}
}
那么,如何记录BFS是否能停止呢
在BFS中记录每次新增的节点数量
如果所有玩家的新增节点数量都为0就结束
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 1005
#define maxp 15
using namespace std;
int n,m,t;
struct node {
int x;
int y;
int t;
node() {
}
node(int xx,int yy,int d) {
x=xx;
y=yy;
t=d;
}
};
const int walkx[4]= {1,-1,0,0},walky[4]= {0,0,1,-1};
int s[maxp];
queue<node>q1[maxp];
queue<node>q2[maxp];
char graph[maxn][maxn];
int vis[maxn][maxn];
int expand(int p) {
int newx=0;
while(!q2[p].empty()) {
node x=q2[p].front();
q2[p].pop();
x.t=0;
q1[p].push(x);
}
while(!q1[p].empty()) {
node x=q1[p].front();
q1[p].pop();
if(x.t==s[p]) {
q2[p].push(x);
continue;
}
for(int i=0; i<4; i++) {
int xx=x.x+walkx[i];
int yy=x.y+walky[i];
if(xx<1||yy<1||xx>n||yy>m||graph[xx][yy]=='#'||vis[xx][yy]!=0||x.t+1>s[p]) continue;
newx++;
q1[p].push(node(xx,yy,x.t+1));
vis[xx][yy]=p;
}
}
if(newx>=1) return 1;
else return 0;
}
int count[maxp];
char tmp[maxn];
int main() {
scanf("%d %d %d",&n,&m,&t);
for(int i=1; i<=t; i++) scanf("%d",&s[i]);
for(int i=1; i<=n; i++) {
scanf("%s",tmp+1);
for(int j=1; j<=m; j++) {
graph[i][j]=tmp[j];
if(graph[i][j]>='0'&&graph[i][j]<='9') {
vis[i][j]=graph[i][j]-'0';
q2[graph[i][j]-'0'].push(node(i,j,0));
}
}
}
while(1) {
int flag=0;
for(int i=1; i<=t; i++) {
flag+=expand(i);
}
if(flag==0) break;
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
count[vis[i][j]]++;
}
}
for(int i=1; i<=t; i++) {
printf("%d ",count[i]);
}
}
Codeforces 1105D (BFS)的更多相关文章
- Kilani and the Game CodeForces - 1105D (bfs)
沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms #include <iostream> #include <algorithm> # ...
- Codeforces 1105D Kilani and the Game【BFS】
<题目链接> 题目大意: 每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色.在所有颜色不能在继续扩张的时候停止游戏.询问此时各种颜色的数量. 解题分析: ...
- 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 1105D(Kilani and the Game,双队列bfs)
AC代码: #include<bits/stdc++.h> #define ll long long #define endl '\n' #define mem(a,b) memset(a ...
- CodeForces - 1105D Kilani and the Game(多源BFS+暴力)
题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...
- Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...
- Police Stations CodeForces - 796D (bfs)
大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...
- Three Pieces CodeForces - 1065D (BFS)
链接 大意: n*n棋盘, 每个格子写有数字, 各不相同, 范围[1,n*n], 初始在数字1的位置, 可以操纵knight,bishop,rook三种棋子, 每走一步花费1, 交换棋子花费1, 问按 ...
- Fair CodeForces - 987D (bfs)
链接 大意:给定无向图边权均为1, 每个节点有一种货物, 对于每个节点, 求出拿到$s$种不同货物的最短距离 (每种货物独立计算,并且不用返回) 因为$s$较小, 直接枚举每种货物即可 所以问题就转化 ...
随机推荐
- vue-router的hash和history模式的区别
一.概念 为了构建 SPA(单页面应用),需要引入前端路由系统,这也就是 Vue-Router 存在的意义. 前端路由的核心,就在于:改变视图的同时不会向后端发出请求. 为了达到这种目的,浏览器当前提 ...
- 用C#实现获取文件夹大小的源代码
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- 基于Xilinx Kintex-7 XC7K325T 的FMC USB3.0 SATA 四路光纤数据转发卡
基于Xilinx Kintex-7 XC7K325T 的FMC USB3.0 SATA 四路光纤数据转发卡 1. 板卡概述 本板卡基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片, ...
- python基础--2 字符串
整型 int python3里,不管数字多大都是int类型 python2里面有长整型long 将整型字符串转换为数字 # a='123' # print(type(a),a) # b=int(a) ...
- No module named flask 导包失败,Python3重新安装Flask模块
在部署环境过程中,通过pip install -r requirements.txt安装包,结果启动项目时总是报错,显示没有flask模块,通过pip install flask还是不行,于是下载fl ...
- maven构建docker镜像异常
由于没有配置ip+2375端口,导致每次跑的时候,都是连接本地的,一直会报错 [ERROR] Failed to execute goal com.spotify:docker-maven-plugi ...
- UE4-PS4开发渲染线程优化方法及记录
先说方法: Launch 到 PS4 Devkit上,在PS4上输入Stat unit 看瓶颈在哪里.我们发现Frame 和Draw数值几乎一样,其余两项相对较小,这表明瓶颈在渲染线程上. 关于渲染线 ...
- 2019 上海网络赛 J stone name (01背包)
题目:https://nanti.jisuanke.com/t/41420 题意:给你一个集合,然后让你拆成两个集合 x,y 求满足 x>y && x-(x集合中最小 ...
- 牛客提高D2t2 幸运数字考试
分析 预处理出所有合法数字 然后直接lower_bound查询即可 代码 #include<iostream> #include<cstdio> #include<cst ...
- leetcode 215. 数组中的第K个最大元素(python)
在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2输出: 5示 ...