题目:

给出一张游戏地图和每个玩家的位置,每次能移动的步数。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+暴力)的更多相关文章

  1. 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 ...

  2. Codeforces 1105D Kilani and the Game【BFS】

    <题目链接> 题目大意: 每个玩家控制一个颜色去扩张,每个颜色的扩张有自己的速度,一个颜色跑完再跑下一种颜色.在所有颜色不能在继续扩张的时候停止游戏.询问此时各种颜色的数量. 解题分析: ...

  3. CF 986A Fair——多源bfs

    题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...

  4. 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 ...

  5. 牛客网 牛客练习赛7 D. 珂朵莉的无向图(多源BFS)

    题目链接  Problem D 比赛的时候完全想不到 直接对给定的这些点做多源$BFS$,把给定的这些点全都压到队列里,然后一个个做. 最后统计被访问的点的个数即可. #include <bit ...

  6. bzoj 2252 [ 2010 Beijing wc ] 矩阵距离 —— 多源bfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2252 又没能自己想出来... 一直在想如何从每个1开始广搜更新答案,再剪剪枝,什么遇到1就不 ...

  7. 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 ...

  8. D. Kilani and the Game 解析(裸BFS、實作)

    Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...

  9. 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 ...

随机推荐

  1. XTU1267:Highway(LCA+树的直径)

    传送门 题意 有n个小镇,Bobo想要建造n-1条边,并且如果在u到v建边,那么花费是u到v的最短路长度(原图),问你最大的花费. 分析 比赛的时候没做出来,QAQ 我们首先要找到树的直径起点和终点, ...

  2. noi.ac 邀请赛1 By cellur925

    A. array 考场:上来就想暴力,首先第一个子任务肯定没问题,怎么搞都行.然后第二个子任务用个数组记下新修的值就行了.第三个子任务用一下等差数列求和公式帮助求解,每次都重新算(因为每次改变全部元素 ...

  3. spring boot :error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required

    配置多个数据源启动报错,error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSo ...

  4. python之类的相关名词-继承-

    继承:父类有的功能,子类继承后也都有 继承是直接把父类方法写入子类的object里 如果定义的类有很多重复的功能,可以把重复的类定义成父类 静态方法:不需要实例化就可以调用,不可以调用类里面的变量和方 ...

  5. Minimal string CodeForces - 797C

    Minimal string CodeForces - 797C 题意:有一个字符串s和空串t和u,每次操作可以将s的第一个字符取出并删除然后放到t的最后,或者将t的最后一个字符取出并删除然后放到u的 ...

  6. centOS 部署服务器(一)

    接下来我所写的博客仅仅是为了记录我的学习过程,与其他无关. 由于公司换用了亚马逊服务器,用的是它的RDS数据库,所以就没有像以前的项目部署的时候使用mysql,不过要下载安装mysql-proxy,字 ...

  7. 166 Fraction to Recurring Decimal 分数到小数

    给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如,    给出 分子 = 1, 分母 = 2,返回 "0.5".  ...

  8. 155 Min Stack 最小栈

    设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈.    push(x) -- 将元素x推入栈中.    pop() -- 删除栈顶的元素.    top() -- 获取 ...

  9. cacti支持中文办法

    1.yum groupinstall "chinese support" 2. 登陆Cacti,在主页的左边点击setting,选择paths页(console>>se ...

  10. configure: error: The LBL Packet Capture Library, libpcap, was not found!

    configure: error:  The LBL Packet Capture Library, libpcap, was not found! yum install libpcap*