题意:

给以一个网格图,有起点终点和一些怪兽,可以上下左右走,不能走到距离怪兽曼哈顿距离为d以内的地方,问到终点最短路径

n*m<=2e5,d<=2e5

思路:

因为n*m的范围,不能直接建2e5*2e5的图,所以要vector.resize()

如果对每个怪兽都预处理的话,复杂度将是O(d2)

所以我们可以让所有怪兽同时走,这样预处理只有O(nm),也可以证明不会漏情况

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e6+;
const int maxm = 1e6+;
//const int inf = 0x3f3f3f3f;
const int inf = 1e9+;
const db pi = acos(-1.0); vector<char>a[maxn];
vector<int>v[maxn],vis[maxn],wall[maxn],tp[maxn];
int n,m,d;
int dx[] = {,-,,};
int dy[] = {,,-,};
bool ck(int x, int y){
if(x>=&&x<=n&&y>=&&y<=m)return true;
return false;
}
queue<pair<PI, int>>seetq;
void seet(){ while(!seetq.empty()){
auto top = seetq.front();
seetq.pop();
int x = top.fst.fst;
int y = top.fst.sc;
int z = top.sc;
//f(v[x][y])continue;
//v[x][y]=1;
if(z==d)continue;
for(int i = ; i < ; i++){
if(ck(x+dx[i],y+dy[i])&&v[x+dx[i]][y+dy[i]]==){
seetq.push({{x+dx[i],y+dy[i]},z+});
v[x+dx[i]][y+dy[i]]=;
}
}
}
return;
}
PI s,t;
char str[maxn]; int main() {
scanf("%d %d %d", &n, &m, &d);
for(int i = ; i <= n; i++){
v[i].resize(m+);
a[i].resize(m+);
vis[i].resize(m+);
wall[i].resize(m+);
tp[i].resize(m+);
} //getchar();
for(int i = ; i <= n; i++){
scanf("%s",str+);
for(int j = ; j <= m; j++){
v[i][j]=vis[i][j]=wall[i][j]=;
tp[i][j]=inf;
char c = str[j];
a[i][j] = c;
if(c=='S'){
s = {i,j};
}
else if(c=='F'){
t = {i,j};
}
else if(c=='M'){
wall[i][j]=d+;
seetq.push({{i,j},});
v[i][j]=;
}
}
}
seet();
queue<pair<PI,int>>q;
q.push({{s.fst,s.sc},});
int ans = -;
if(v[s.fst][s.sc]==)return printf("-1"),;
q.push({{s.fst,s.sc},});
while(!q.empty()){
auto top = q.front();
q.pop();
int x = top.fst.fst;
int y = top.fst.sc;
int z = top.sc;
if(x==t.fst&&y==t.sc){
ans=z;
break;
}
if(vis[x][y])continue;
vis[x][y]=;
for(int i = ; i < ; i++){
int nx = x+dx[i];
int ny = y+dy[i];
if(ck(nx,ny)&&vis[nx][ny]==&&v[nx][ny]!=){
q.push({{nx,ny},z+});
}
}
}
printf("%d",ans);
return ;
}
/*
4
2 2 3
2 3 4
1 4
0
*/

Codeforces gym101755H Safe Path(bfs)的更多相关文章

  1. codeforces 1072D Minimum path bfs+剪枝 好题

    题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) ...

  2. Safe Path(bfs+一维数组存图)

    题目链接:http://codeforces.com/gym/101755/problem/H 题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点: 很显然读到  2&l ...

  3. POJ2126——Prime Path(BFS)

    Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...

  4. POJ3126 Prime Path (bfs+素数判断)

    POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...

  5. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  6. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...

  7. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  8. Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)

    D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. POJ3126 Prime Path —— BFS + 素数表

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. OpenLayers4 隐藏(hide)Feature

    需求: 需要将同一图层的要素进行分类显示和隐藏(类似于图层控制) 方法: 使用setStyle方法将Feature的样式设置为null. 环境: win10.google chrome.OL 4.3 ...

  2. echarts更改轴线颜色

    xAxis : [ { type : 'category', data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月 ...

  3. XSS基础学习

    XSS基础学习 By:Mirror王宇阳 什么是XSS XSS攻击是指在网页中嵌入一段恶意的客户端Js脚本代码片段,JS脚本恶意代码可以获取用户的Cookie.URL跳转.内容篡改.会话劫持--等. ...

  4. python文档字符串(函数使用说明)

    关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的 ...

  5. 配置IDEA默认作者@author

    IDEA安装目录下,使用文本编辑器打开~/bin/idea64.exe.vmoptions文件 在最后添加:-Duser.name=Your name 保存重启IDEA,Done

  6. SpringMVC启动流程源码解密

    我们知道,SpringMVC最后是通过Tomcat来进行部署的.当在Servlet中进行进行应用部署时,主要步骤为(引用来自http://download.oracle.com/otn-pub/jcp ...

  7. Ubuntu 设置中文输入法

    环境:新安装的Ubuntu 16.04 LTS 本来打算使用sogou拼音,但是在输入后的提示框中内容为乱码,因此尝试重新安装. 1.sudo apt-get remove sogoupinyin & ...

  8. 让现有vue前端项目快速支持多语言 - 用.net core程序快速替换中文为资源Key,咱不干体力活

    前言 最近应公司上层要求,需要将现有项目尽快支持多语言,而中文内容可以找专业人员翻译.那么咱们说干就干,首先我们项目的前端是用vue写的spa程序且组件方面用的element ui,那么自然而然想到用 ...

  9. Java并发关键字Volatile 详解

    Java并发关键字Volatile 详解 问题引出: 1.Volatile是什么? 2.Volatile有哪些特性? 3.Volatile每个特性的底层实现原理是什么? 相关内容补充: 缓存一致性协议 ...

  10. 团队第一次作业(By七个小矮人)

    一.团队简介 1.团队名称:七个小矮人 2.团队成员列表 201731024137 马驰(队长) 201731021227 于丁 201731024114 杨汶桐 201731024125 李朋珂 2 ...