题意:

给以一个网格图,有起点终点和一些怪兽,可以上下左右走,不能走到距离怪兽曼哈顿距离为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. kubelet--help-v1.15.4

    kubelet --help 官方文档   The kubelet is the primary "node agent" that runs on each node. It c ...

  2. C++中重载、重写(覆盖)和隐藏的区别

    转载自:https://blog.csdn.net/zx3517288/article/details/48976097 基本概念: 重载:是指同一可访问区内被声明的几个具有不同参数列(参数的类型,个 ...

  3. 《图解机器学习-杉山将著》读书笔记---CH3

    CH3 最小二乘学习法 重点提炼 提出最小二乘学习法的缘故: 最小二乘学习法公式 对不同模型进行最小二乘法学习,得到最小二乘公式中的参数theta: 1.线性模型   代入3.1公式,对参数求偏导,偏 ...

  4. 关于Mach-O类型文件那点事

    Mach-O文件简介   Mach-O是一种文件格式,是Mach Object文件格式的缩写. 它通常应用于可执行文件,目标代码,动态库,内核转储等中.   Mach-O作为大部分基于Mach核心的操 ...

  5. js删除数组的某个元素

    最近在刷算法题,我是用js语言去写的,其中一题需要删除数组的某个元素,查了一下资料,总结一下 使用splice()方法 array.splice(start[, deleteCount[, item1 ...

  6. stars-one原创工具——m3u8视频下载合并器(kotlin)

    一款可以下载m3u8.解密ts文件及合并ts文件的视频下载工具 蓝奏云下载地址 github地址 软件对你有帮助的话,不妨赞赏一波!感谢! 程序说明 采用多线程下载,可有效的提高下载速度 内置解密程序 ...

  7. springboot2 整合redis

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  8. 用Django自动生成表遇到问题

    因为以前在数据库中已经生成过Django 叫App01下的表,所以无法生成,在数据库中执行这个命令 DELETE FROM django_migrations WHERE app='App01';然后 ...

  9. github 关掉邮件通知

  10. jenkins 配置ssh密钥登录

    1.找到一台服务器执行 ssh-keygen -t rsa 会在目录/root/.ssh生成id_rsa私钥.id_rsa.pub公钥,将公钥的内容写入到同目录下的authorized_keys文件( ...