cf877D
题意简述:矩阵中有的点不能走,你每次可从四个方向走,至少走一步,最多走k步(不能横跨那些不能走的格子),问从(sx,sy)走到(tx,ty)的最短时间是多少?
题意:利用set来加速bfs的过程,原理是一个点一旦走过就不需要再去走了,具体看代码
#include <bits/stdc++.h>
using namespace std; const int N = 1002;
bool G[N][N];
int n, m, k, d[N][N];
set<int> xs[N], ys[N]; void clear_rem(vector<pair<int,int> > &rem){
for(auto p : rem){
xs[p.first].erase(p.second);
ys[p.second].erase(p.first);
}
rem.clear();
} void bfs(int sx, int sy){
xs[sx].erase(sy); ys[sy].erase(sx);
queue<pair<int,int> > q; q.push({sx,sy});
d[sx][sy]=0;
while(!q.empty()){
pair<int,int> u=q.front(); q.pop();
int x = u.first, y=u.second;
vector<pair<int,int> > rem;
for(auto it=xs[x].lower_bound(y); it!=xs[x].end(); it++){
if(*it-k>y || !G[x][*it])break;
rem.push_back({x,*it});
q.push({x,*it}); d[x][*it]=d[x][y]+1;
}
clear_rem(rem);
for(auto it=ys[y].lower_bound(x); it!=ys[y].end(); it++){
if(*it-k>x || !G[*it][y])break;
rem.push_back({*it,y});
q.push({*it,y}); d[*it][y]=d[x][y]+1;
}
clear_rem(rem);
auto it=xs[x].lower_bound(y);
if(it!=xs[x].begin()){
it--;
while(1){
if(y-*it>k || !G[x][*it])break;
rem.push_back({x,*it});
q.push({x,*it}); d[x][*it]=d[x][y]+1;
if(it==xs[x].begin())break;
it--;
}
}
clear_rem(rem); it=ys[y].lower_bound(x);
if(it!=ys[y].begin()){
it--;
while(1){
if(x-*it>k || !G[*it][y])break;
rem.push_back({*it,y});
q.push({*it,y}); d[*it][y]=d[x][y]+1;
if(it==ys[y].begin())break;
it--;
}
}
clear_rem(rem);
}
} int main(){ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(d,-1,sizeof(d));
cin >> n >> m >> k;
for(int x=1; x<=n; x++)
for(int y=1; y<=m; y++){
char c; cin >> c;
G[x][y]=c=='.';
xs[x].insert(y);
ys[y].insert(x);
}
int X, Y; cin >> X >> Y;
bfs(X,Y);
cin >> X >> Y;
cout << d[X][Y] << "\n";
}
cf877D的更多相关文章
- CodeForces CF877D题解(BFS+STL-set)
解法\(1:\) 正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...
随机推荐
- php---> xhprof安装及使用
xhprof 简介 xhprof是一款网站的性能工具 安装(lnmp) php --ri xhprof #检查php是否有这个扩展 cd xhprof-0.9.4/xhprof-0.9.4/exten ...
- Shell常用命令之sort
sort命令 sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出.sort命令既可以从特定的文件,也可以从stdin中获取输入. 语法 sort (选项) (参数) 选项 - ...
- Leetcode 题目整理-1
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- Intent传递实现Parcelable接口的对象
Intent可以传递基本数据类型,在对象实现了Parcelable接口后,Intent也可以传递对象. 1. 使类ListVideo实现了Parcelable接口. package com.examp ...
- 页面置换算法之Clock算法
1.前言 缓冲池是数据库最终的概念,数据库可以将一部分数据页放在内存中形成缓冲池,当需要一个数据页时,首先检查内存中的缓冲池是否有这个页面,如果有则直接命中返回,没有则从磁盘中读取这一页,然后缓存到内 ...
- Standby Redo Logs的前世今生与最佳实践
编辑手记:使用过Data Guard的人应该对于Standby Redo Logs都不陌生,在配置了 Standby Redo Logs的standby中,能够进行日志的实时应用,同时Standby ...
- Linux命令行与Shell脚本编程大全
快来参加<Linux命令行与Shell脚本编程大全>学习吧,提升技能,展示自我. 点击链接即可进入学习:https://s.imooc.com/WTmCO6H 课程亮点适合零基础读者,从零 ...
- 实例探究Aspectj,解析SentinelResourceAspect
为了学习SentinelResourceAspect,这篇文章里我用Aspectj实现一个AOP实例,一起来看下. Sentinel 提供了 @SentinelResource 注解用于定义资源,支持 ...
- Yandex Big Data Essentials Week1 Unix Command Line Interface Processes managing
free displays the total amount of free and used memory free [options] top provides a dynamic real-ti ...
- Hapi+MySql项目实战配置插件-加载文件渲染母版(三)
加载插件 一般在其它node框架下,我们安装好插件直接require('插件')就能正常使用了,但是在Hapi下我们必须要Server.register()方法,才能正常使用插件.举个例子: serv ...