题意:

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

    Redis docker run -itd --name myredis -v /dockerdata/redis/config/redis.conf:/etc/redis/redis.conf  - ...

  2. AcWing 243. 一个简单的整数问题2 | 树状数组

    传送门 题目描述 给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“C l r d”,表示把 A[l],A[l+1],…,A[r] 都加上 d. 2.“Q l r”,表示询问 ...

  3. Python保存时提示“SyntaxError: Non-ASCII character '\xe8' in file”

    原因 Python 默认的是ASCII 编码方式,如果出现中文会出现问题,所哟必须在代码的第二行或第一行 显示的声明编码方式(已注释的方式,即"#"开始) 解决方法 在文件头部加上 ...

  4. vue 路由模块化

    第一. 在 router 文件夹下 新建个个模块的文件夹,存放对应的路由js文件 如图1: 第二.修改router文件夹下的index.js  如图2 三.在main.js 修改如下代码 图3

  5. hadoop搭建

    一.前期准备 1.1 静态ip,请查看虚拟机安装不放呢 1.2 hostname 以及 hosts文件修改 cat /etc/hostname 不同的机器设置不同的名字 cat /etc/hosts ...

  6. 在ubuntu 18上安装MongoDB

    本文介绍 MongoDB Community Edition 在 Ubuntu 下的安装和常见配置方法.文中操作基于 MongoDB Community Edition 4.2.2 和 Ubuntu ...

  7. Milking Cows 挤牛奶 USACO 排序 模拟

    1005: 1.2.1 Milking Cows 挤牛奶 时间限制: 1 Sec  内存限制: 128 MB提交: 15  解决: 9[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 1 ...

  8. BigDecimal的加减乘除,比较,小数保留

    关于BigDecimal的一些常用基本操作记录 1        BigDecimal b1 = new BigDecimal("1.124"); 2        BigDeci ...

  9. dp - 逆序数序列

    对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样自然数数 ...

  10. EasyExcel 自定义单元格式的问题。

    最近在做一个关于性能测试管理系统,一个新的需求,需要导出测试报告,直接使用了ali的封装的EasyExcel,但是在复杂头与一些样式,就缺少了自定义的灵活性,在官方demo中没有找到很好的解决方法. ...