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$较小, 直接枚举每种货物即可 所以问题就转化 ...
随机推荐
- qt05 音乐播放器
这些步骤可实现音乐播放,但是列表不能显示 music = new QMediaPlayer(this); playlist = new QMediaPlaylist(this); playlist-& ...
- ps:新建Photoshop图像
从现在起我们开始正式地接触Photoshop,为了保证大家的快捷键设置与教程内容一致.请确认Photoshop的快捷键设置是默认值.可从菜单[编辑 键盘快捷键]打开快捷键设置,在组选项里面选择“Pho ...
- git 日常 常用命令
初始化git git init 第一次拉代码: 方式1:git clone git clone https://git.oschina.net/*****.git (https远程仓库地址) 方式2: ...
- 阿里云ECS VSFTP上传本地文件
开始终端 购买云服务,获得公网IP,内网IP 控制台首页获得 获得终端连接密码 连接终端,输入连接密码 获得终端界面,进入终端 上传文件 ## .安装VSFTP服务器程序 yum install vs ...
- Linux下python pip手动安装笔记
今天查问题, 从redis集群中模糊查询某个key, 用一些重复的命令, 链接不同的node, redis-cli 去查, 感觉不舒服. 考虑写一些shell或py来简化一下. 一查环境, 安装了py ...
- sublime text3 Package Control 插件安装及推荐(MAC)
参考: https://www.zhihu.com/question/36233553 https://www.cnblogs.com/zuoan-oopp/p/6692797.html 插件推荐 因 ...
- 15:解决IntelliJ IDEA的乱码问题
1. -Dfile-encodings=UTF-8 ,全局:
- linux文件软链接操作
cd /etc/alternatives ll php* lrwxrwxrwx 1 root root 15 Oct 23 15:24 php -> /usr/bin/php7.3* lrwxr ...
- 解决Windows2003 Server终端服务120天限制
用过windows server 2003做服务器的人都知道,windows2003的性能安全性比以前的windows版本高出很多,但是也带来很多麻烦.其中服务器最重要的远程管理“终端服务”居然要求授 ...
- 夯实Java基础系列7:Java 代码块和执行顺序
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...