CodeForces - 1105D Kilani and the Game(多源BFS+暴力)
题目:
给出一张游戏地图和每个玩家的位置,每次能移动的步数。p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束。
问在游戏结束后,每个玩家占领的格子的数目。
思路:
当时做的时候,并没有思路,借鉴了大佬的……Orz
对每一个玩家分配一个队列,这个队列中只保存移动时最后一步的位置。如果在一个循环中p个玩家都没有能够成功移动的,就说明游戏结束了。
然后遍历一下地图,统计一下每个玩家占领的格子就ok了。
代码:
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <iomanip>
#define MOD 998244353
#define MAX 1000000000
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = ;
int n,m,p;
int vis[maxn][maxn],s[maxn],ans[maxn];
int dx[]={,,,-};
int dy[]={,-,,};
char mp[maxn][maxn];
struct Node
{
int x,y,t;
};
queue<Node> q1[maxn],q2[maxn]; bool isin(int x,int y)
{
return x>= && x<n && y>= && y<m;
} int MultipleBfs(int id)
{
while(!q2[id].empty())
{
Node now = q2[id].front();
q2[id].pop();
now.t = ;//开始移动时步数都是0的,好好想想……
q1[id].push(now);
} int flag = ;
while(!q1[id].empty())
{
Node now = q1[id].front();
q1[id].pop();
if(now.t == s[id])//记录最外围的格子的位置
{
q2[id].push(now);
continue;
} for(int i=; i<; i++)
{
int tx = now.x+dx[i],ty = now.y+dy[i];
if(isin(tx,ty) && !vis[tx][ty] && mp[tx][ty]!='#')
{
vis[tx][ty] = id;
q1[id].push(Node{tx,ty,now.t+});
flag++;//标记这个玩家有没有移动
}
}
}
if(flag>) return ;
else return ;
} int main()
{
//FRE();
while(scanf("%d%d%d",&n,&m,&p) != EOF)
{
memset(ans,,sizeof(ans));
memset(vis,,sizeof(vis));
for(int i=; i<=p; i++) scanf("%d",&s[i]);
for(int i=; i<n; i++)
{
scanf("%s",mp[i]);
for(int j=; j<m; j++)
{
if(isdigit(mp[i][j]))
{
int x = mp[i][j] - '';
vis[i][j] = x;//对每个玩家的位置进行预先标记
q2[x].push(Node{i,j,});//压入队列
}
}
} while(true)
{
int flag = ;
for(int i=; i<=p; i++)
{
flag += MultipleBfs(i);
}
if(!flag) break;//如果没有玩家能够移动就退出
} for(int i=; i<n; i++)//统计每个玩家的格子
{
for(int j=; j<m; j++)
{
//printf("%d ",vis[i][j]);
ans[vis[i][j]]++;
}
//printf("\n");
} for(int i=; i<=p; i++)
{
if(i!=) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return ;
}
CodeForces - 1105D Kilani and the Game(多源BFS+暴力)的更多相关文章
- 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】
<题目链接> 题目大意: 每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色.在所有颜色不能在继续扩张的时候停止游戏.询问此时各种颜色的数量. 解题分析: ...
- CF 986A Fair——多源bfs
题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...
- Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
D. Igor In the Museum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598 ...
- 牛客网 牛客练习赛7 D. 珂朵莉的无向图(多源BFS)
题目链接 Problem D 比赛的时候完全想不到 直接对给定的这些点做多源$BFS$,把给定的这些点全都压到队列里,然后一个个做. 最后统计被访问的点的个数即可. #include <bit ...
- bzoj 2252 [ 2010 Beijing wc ] 矩阵距离 —— 多源bfs
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2252 又没能自己想出来... 一直在想如何从每个1开始广搜更新答案,再剪剪枝,什么遇到1就不 ...
- Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)
Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...
- 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 ...
随机推荐
- MFC类别概述
MFC 类别主要可分为下列数大群组: ■ General Purpose classes - 提供字符串类别.数据处理类别(如数组与串行),异 常情况处理类别.文件类别...等等. ■ Windows ...
- hdu2767(图的强连通)
//题意:问需要添加几条边使得这张图成为每个点都等价(强连通图) 我们先把图中的强连通分量缩点 可能他本身就是满足条件,那么直接输出0 经过缩点后,就可以把强连通分量看成一个个独立的点,在这张图上搞一 ...
- Ecliplse 指定JRE
http://blog.csdn.net/hongweigg/article/details/9987649 在Eclipse启动的过程中,它会去找系统环境变量设置的JRE_HOME或JDK_HOME ...
- Codeforces732E Sockets
首先检测有木有和Computer匹配的Socket,如果有则将其匹配. 然后将所有没有匹配的Socket连上Adapter,再去检测有木有Computer与Socket匹配. 重复这个操作31次,所有 ...
- Luogu P1141 01迷宫【搜索/dfs】By cellur925
题目传送门 我tm到现在还需要刷这种水搜索...我退役吧. 但就是搜索弱嘛 补一补嘛qwq 题目大意:给你一张地图与许多询问,每次询问求这个点所在联通块的点的个数. 所以这个题目的本质就是在求联通块. ...
- kibana 操作
插入时不指明id,不会自动生成id,和视频中的不一样啊 解决: 其实是可以的,不过put不可以 POST才可以 中文输入有问题: 待解决: 基本操作记录 GET _search { "que ...
- Eclipse新建Maven webapp项目错误的解决方法
新建webapp项目时出现如下错误: 解决步骤如下: 1. 右键点击项目,选择Properties,点击Java Build Path,将默认的JRE移除,点击右侧add Library,选择JRE ...
- django接受表单
from django.shortcuts import render from django.shortcuts import HttpResponse import os # Create you ...
- git for mac
Git 使用 1.下载完成后打开终端,使用git --version或者which git命令查看安装版本,有就是安装成功了. 2.创建一个全球用户名.全球邮箱 git config --global ...
- loj125 除数函数求和 2
https://loj.ac/problem/125 $原式=2\sum_{i=1}^n(i^2*{\lfloor}{\frac{n}{i}}{\rfloor})+3\sum_{i=1}^n(i*{\ ...