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 ...
随机推荐
- 关于Page翻页效果, PageViewConrtoller
Page View Controllers你使用一个page view controller用page by page的方式来展示内容.一个page view controller管理一个self-c ...
- iperf工具学习记录
源码下载地址:http://sourceforge.net/projects/iperf/ 编译命令: tar -zxvf iperf-2.0.5.tar.gz cd iperf-2.0.5 ./co ...
- CH 6021 走廊泼水节
描述 [简化版题意]给定一棵N个节点的树,要求增加若干条边,把这棵树扩充为完全图,并满足图的唯一最小生成树仍然是这棵树.求增加的边的权值总和最小是多少. 我们一共有N个OIER打算参加这个泼水节,同时 ...
- April Fools Contest 2017 E
Description Input The input consists of four lines, each line containing a single digit 0 or 1. Outp ...
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
- 1-9方法的重写(override)
什么是重写? 重写,也叫做覆盖,当父类中的方法无法满足子类需求时,子类可以将父类的方法进行重写编写来满足需求.比如孩子继承了父亲的房子,可以将房子重新装修. 方法重写的条件: 两个类必须是继承关系 必 ...
- 1-10super和this关键字
什么是super? super代表的是当前子类对象中的父类型特征. 什么时候使用super? 子类和父类中都有某个数据,例如,子类和父类中都有name这个属性.如果要再子类中访问父类中的name属性, ...
- Redis java操作客服端——jedis
1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...
- java将一个List赋值给另一个List的4种方法
编辑 删除 声明:ArrayList a, 仅仅只是声明了一个list变量,其未来作用相当于C++中的引用变量,亦或者相当于一个对象块的索引,但并未为其分配具体的完整的对象所需要的内存空间,其所分配的 ...
- WEB前端JS与UI框架
前端Js框架汇总 概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人 ...