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 ...
随机推荐
- PTA QQ Account Manageme【map的巧妙应有】
5-27 QQ Account Management (25分) You are supposed to implement the functions of account "Log in ...
- 洛谷P2564 [SCOI2009]生日礼物(单调队列)
传送门 准确的来说这个应该叫尺取法? 先对所有的点按$x$坐标进行排序 我们维护两个指针$l,r$,每一次令$r$不断右移直到所有颜色齐全,再不断右移$l$直到颜色数不足,那么此时$[l-1,r]$这 ...
- 第一篇 .NET高级技术之索引器
基础知识补充 索引 器 没有名字 ,索引器的内部本质 (ILSpy的IL模式下看)类型 this[参数]{get;set;} 可以是只读或者只写(在get或者set前加上private) 字符串是只读 ...
- WIN7 64位操作系统 无法找到Access驱动
为了更充分的利用硬件资源,我想很多人都开使用64位操作系统了,同时你可以也发现了在64位操作系统上ODBC的驱动找不到了,所以ODBC的东西都没法用了. 因为2007以前版本的Office只有32位版 ...
- Charles对移动APP抓包(https)
1.下载安装Charles 2.设置代理 (1)查看默认端口:Proxy->Proxy Settings 在这个页面会看到HTTP Proxy的默认端口是8888 (2)查看当前电脑的IP:H ...
- req.getParameter()无法获取参数(附前端json序列化)
问题:前端用Ajax的post方式想servlet传递参数,servlet的getParameter()方法无法获取参数. 前端代码: $.ajax({ url: '/TestWeb/addBook' ...
- img 标签访问图片返回403forbidden
做百度编辑器时,从秀米复制过来的文档,图片不无法加载,返回403的错 解决办法 解决这个问题只需要在头部添加一个meta <meta name="referrer" cont ...
- 批量部署Hadoop集群环境(1)
批量部署Hadoop集群环境(1) 1. 项目简介: 前言:云火的一塌糊涂,加上自大二就跟随一位教授做大数据项目,所以很早就产生了兴趣,随着知识的积累,虚拟机已经不能满足了,这次在服务器上以生产环境来 ...
- JSP自定义标签开发步骤
自定义的标签库一.基本概念: 1.标签(Tag): 标签,通常也成为动作,是一组按照XML语法格式编写的代码片段,在JSP中,用来封装在页面中可重复利用的逻辑,通过标签可以使JSP网页变得简洁并且易于 ...
- iOS Getter 和Setter 注册xibcell
// 初始化cell的xib的方式 [tableView registerNib:[UINib nibWithNibName:@"LXmiddleCell" bundle:nil] ...