题意简述:矩阵中有的点不能走,你每次可从四个方向走,至少走一步,最多走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的更多相关文章

  1. CodeForces CF877D题解(BFS+STL-set)

    解法\(1:\) 正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...

随机推荐

  1. SVM(2)-模式识别课堂笔记

    三.非线性支持向量机 问题起源:1.对于一些非线性可分的问题,我们希望能通过一个映射问题将特征映射到新的空间中去(可能是更高维的空间),寄希望于在新的空间中样本能够线性可分:2.我们注意到在线性支持向 ...

  2. LUA提取免费迅雷账号

    --获取http://www.521xunlei.com/ 免费迅雷账号 function getPageid() local http = require("socket.http&quo ...

  3. java(list,set,map)链接

    http://blog.csdn.net/smileiam/article/details/49836865 http://blog.csdn.net/u013344815/article/detai ...

  4. python 2 计算字符串 余弦相似度

    def get_ord_list(str): return [ord(i) for i in str] def calcu_approx(str1,str2): def dot(A,B): retur ...

  5. HDU_1035_水

    http://acm.xidian.edu.cn/problem.php?id=1035 本来想用goto优化一下的,不知道什么情况,加了goto就wa了. #include<iostream& ...

  6. Spacemacs安装

    Spacemacs官网 为什么选择Spacemacs Spacemacs是一个已经配好的Emacs和Vim,正如官网所说的The best editor is neither Emacs nor Vi ...

  7. HDU 5558 Alice's Classified Message(后缀数组+二分+rmq(+线段树?))

    题意 大概就是给你一个串,对于每个\(i\),在\([1,i-1]\)中找到一个\(j\),使得\(lcp(i,j)\)最长,若有多个最大\(j\)选最小,求\(j\)和这个\(lcp\)长度 思路 ...

  8. num10---适配器模式

    1.类适配器 Adapter类,通过继承 被适配的类,实现目标类的接口,完成适配. 分析: java 单继承,所以适配器类 需要继承 被适配类,这就要求目标类必须是接口,有一定局限性. 被适配类的方法 ...

  9. 蓝桥杯2015年省赛C/C++大学B组

    1. 奖券数目 有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利.虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求.某抽奖活动的奖券号码是5位数(10000-99999),要求其 ...

  10. 一个"/"引发的惨案

    今天行云流水写了一个接口,正想着写完就睡觉了,结果访问的时候一直报错404,找不到路径,我反复检查了好久,确定路径名字没写错,百思不得其解,瞬间有想砸电脑的冲动,于是准备洗洗睡了,明天再搞 洗玩脚回到 ...