题面

传送门

分析

考虑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)的更多相关文章

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

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

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

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

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

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

  5. CodeForces - 1105D Kilani and the Game(多源BFS+暴力)

    题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...

  6. Arthur and Walls CodeForces - 525D (bfs)

    大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案. 一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉. 3 3 **. .** ... 正解是bfs ...

  7. Police Stations CodeForces - 796D (bfs)

    大意: 给定树, 有k个黑点, 初始满足条件:所有点到最近黑点距离不超过d, 求最多删除多少条边后, 使得原图仍满足条件. 所有黑点开始bfs, 贪心删边. #include <iostream ...

  8. Three Pieces CodeForces - 1065D (BFS)

    链接 大意: n*n棋盘, 每个格子写有数字, 各不相同, 范围[1,n*n], 初始在数字1的位置, 可以操纵knight,bishop,rook三种棋子, 每走一步花费1, 交换棋子花费1, 问按 ...

  9. Fair CodeForces - 987D (bfs)

    链接 大意:给定无向图边权均为1, 每个节点有一种货物, 对于每个节点, 求出拿到$s$种不同货物的最短距离 (每种货物独立计算,并且不用返回) 因为$s$较小, 直接枚举每种货物即可 所以问题就转化 ...

随机推荐

  1. Linux性能优化从入门到实战:09 内存篇:Buffer和Cache

      Buffer 是缓冲区,而 Cache 是缓存,两者都是数据在内存中的临时存储.   避免跟文中的"缓存"一词混淆,而文中的"缓存",则通指内存中的临时存储 ...

  2. kali Linux 入门(二)

    九.软件安装 1.apt install --软件名称-- -y 2.apt install packge_name----库安装 3.apt install kali-linux-all -y--- ...

  3. Spring的AOP和IoC及隔离级别

    Spring的AOP和IoC Spring AOP:代理机制.Spring提供的自动代理机制 Spring的IoC来实组件之间的依赖关系注入, 使控制层与业务实现分离,即客户通过调用业务委托接口来调用 ...

  4. git点滴

    git指定版本,SHA-1短的,长的都可以 git checkout c66a9be git checkout c66a9befsadf1sdf1s3fd21 git log ##查询本地log gi ...

  5. (3.1)狄泰软件学院C++课程学习剖析二

    深度剖析C++第二部分 1.通过对象名能够访问public成员变量.每个对象的成员变量都是专属的,成员变量不能够在对象之间共享. 2.需求:统计在程序运行期间某个类的对象数目,保证程序的安全性(不能使 ...

  6. centos7systemctl介绍

     Linux中如何启动.重启.停止.重载服务以及检查服务(如 httpd.service)状态  # systemctl start httpd.service # systemctl restart ...

  7. linux之rpm软件包管理

    1.RPM包的命名规则 例如:httpd-2.2.15-15.el6.centos.1.i686.rpm httpd ·        软件包名 2.2.15        软件版本 15       ...

  8. select选项

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Step1 - How to: Define a Windows Communication Foundation Service Contract

    https://msdn.microsoft.com/en-us/library/ms731835.aspx This is the first of six tasks required to cr ...

  10. Getting Started Tutorial from msdn

    Getting Started Tutorial The topics contained in this section are intended to give you quick exposur ...