题意:

给以一个网格图,有起点终点和一些怪兽,可以上下左右走,不能走到距离怪兽曼哈顿距离为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. js判断各种类型

    js的六种基本类型:Object,Boolean,Number,String,Undefined,Null; Object中又有:Function,Array,Date... 如何判断数据类型? Ob ...

  2. .net core 3.0 搭建 IdentityServer4 验证服务器

    叙述 最近在搞 IdentityServer4  API接口认证部分,由于之前没有接触过 IdentityServer4 于是在网上一顿搜搜搜,由于自己技术水平也有限,看了好几篇文章才搞懂,想通过博客 ...

  3. 通过httpClient设置代理Ip

    背景: 我们有个车管系统,需要定期的去查询车辆的违章,之前一直是调第三方接口去查,后面发现数据不准确(和深圳交警查的对不上),问题比较多.于是想干脆直接从深圳交警上查,那不就不会出问题了吗,但是问题又 ...

  4. springboot2 整合mongodb

    在springboot2中使用MongoDB 1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId ...

  5. Java小白集合源码的学习系列:LinkedList

    目录 LinkedList 源码学习 LinkedList继承体系 LinkedList核心源码 Deque相关操作 总结 LinkedList 源码学习 前文传送门:Java小白集合源码的学习系列: ...

  6. LGV - 求多条不相交路径的方案数

    推荐博客 :https://blog.csdn.net/qq_25576697/article/details/81138213 链接:https://www.nowcoder.com/acm/con ...

  7. map + 前缀和

    B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. 从网上下载DLL

    1,微软官网 2:https://www.zhaodll.com/ 3:http://www.dllzj.com/

  9. golang判断目录项中是目录还是文件。

    package main import ( "fmt" "os") func main() { //目录的操作 fmt.Println("请输入文件目 ...

  10. python应用airtest库的环境搭建

    参考https://blog.csdn.net/ywyxb/article/details/64126927 注意:无论是在线还是离线安装,最好在管理员权限下执行命令 1.安装Python36(32位 ...