题面

传送门

分析

考虑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. python函数传参和返回值注意事项

    函数传参 空参数 定义函数时括号里面没有形参,调用时不用传参. def func(): print('null para.') # 调用 func() 位置传参 规定形参的数量,调用时必须传递相同数量 ...

  2. 14DBCP连接池

    实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程,为了解决此类性能问题,通常情况我们采用连接池技术,来共享连接Connection.这样我们就不需要每次都创建连接.释放连接了,这些操作 ...

  3. window 批处理脚本获取上级目录

    1 SET CurrDir=%CD% CD.. SET InstPath=%CD% CD %CurrDir% 2 pushd.. set parent=%cd% popd 参考: https://ms ...

  4. maven 配置阿里云镜像

    编辑%maven_home%/conf/settings.xml文件,添加 <mirror> <id>aliyun-maven</id> <mirrorOf& ...

  5. DDL DML DCL的理解

    DDL的操作对象是表,不会对具体的数据进行操作. DML的操作对象是记录, DCL的操作对象是数据库对象的权限.

  6. ZOJ 3329 One Person Game(概率DP,求期望)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题目大意: 有三个骰子,分别有K1,K2,K3个面,一次投掷可以得到三个 ...

  7. MongoDB性能分析

    # mongostat --host=100.150.2.12 --port=27017 --authenticationDatabase=admin --username=root --passwo ...

  8. Java-package import关键字

    package包关键字,在java中,有包的概念,主要是用来归类 分类作用: 便于项目的开发和维护: 通过分包,可以很清晰的来管理类: 上述 一个类Animal: 在开头有个 package com. ...

  9. Js DOM 修改 css Style

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. LinkedList 源码解读

    LinkedList 源码解读 基于jdk1.7.0_80 public class LinkedList<E> extends AbstractSequentialList<E&g ...