题意:

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

最新文章

  1. JavaScript进阶之this
  2. tinymce整合struts2使用
  3. kvm虚拟机时间修改
  4. SQL SERVER with递归示例一则
  5. seafile
  6. Spark RDD概念学习系列之为什么会引入RDD?(一)
  7. IOS动画隐式,显式,翻页
  8. HTML5 + SOCKET视频传输
  9. 给定一个字符串,仅由a,b,c 3种小写字母组成。
  10. 王灏:光音网络致力打造Wi-Fi大生态圈
  11. [Design Pattern] Observer Pattern 简单案例
  12. jquery之多重判断
  13. Javascript数组操作方法
  14. psl/sql本地与远程连接配置
  15. MVC Json 回报
  16. Problem E: 可变长数组
  17. C++ STL之min_element()与max_element()(取容器中的最大最小值)
  18. .Net MVC5异步请求Entity Framework 无限循环解决方法
  19. ueditorUE 去掉本地保存成功的提示框!
  20. Linux运维面试题之--网页打开缓慢如何优化

热门文章

  1. Linux下扫描服务器IP地址是否冲突(arp-scan)
  2. VS从标准输入读入文件
  3. 计算n的阶乘
  4. 【转】分布式服务框架 Zookeeper -- 管理分布式环境中的数据
  5. OpenStack Identity API v3 extensions (CURRENT)
  6. php部署后错误排查流程
  7. 数据结构与算法 Python语言实现 第一章练习
  8. canal 基于Mysql数据库增量日志解析
  9. IDEA不编译空文件夹
  10. 【LC_Lesson1】--字符串反转练习