解法\(1:\)

正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了的。\(n*m*4*k=4*10^9\)。

这里提供一个\(codeforces\)的官方解法:\(STL-set\)优化\(bfs\)。使用\(set\)主要是能够快速找到当前行和列有那个点没有被找到过,每行每列单独建立一个\(set\),\(set<int>\ line[N],col[N]\)。

在\(bfs\)的时候\(set<int>::iterator\ it=line[x].lower\_bound(y)\)找两次,分别处理左边和右边的答案。

再\(set<int>::iterator\ it=col[y].lower\_bound(x)\),分别处理上下方向的答案。

细节在代码里。

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
set<int> lin[N],col[N];
int n,m,k;
char mp[N][N];
int sx,sy,ex,ey;
queue<pair<int,pair<int,int> > > q;
int read()
{
int x=0,f=1;
char c=getchar();
while (c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while (c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void add_to_que(int x,int y,int dis)
{
q.push(make_pair(dis,make_pair(x,y)));
lin[x].erase(y);
col[y].erase(x);
}
void BFS()
{
//int x1=read(),y=read()
add_to_que(sx,sy,0);
while (!q.empty())
{
int curx=q.front().second.first;
int cury=q.front().second.second;
int curd=q.front().first;
q.pop(); if(curx==ex && cury==ey)
{
printf("%d\n",curd);
exit(0);
}
set<int>::iterator wsy=lin[curx].lower_bound(cury); while (wsy!=lin[curx].end() && *wsy-cury<=k && mp[curx][*wsy]!='#')
{
add_to_que(curx,*wsy,curd+1);
wsy=lin[curx].lower_bound(cury);
}
wsy=lin[curx].lower_bound(cury);
while (wsy!=lin[curx].begin() && cury-*(--wsy)<=k && mp[curx][*wsy]!='#')
{
add_to_que(curx,*wsy,curd+1);
wsy=lin[curx].lower_bound(cury);
} wsy=col[cury].lower_bound(curx);
while (wsy!=col[cury].end() && *wsy-curx<=k && mp[*wsy][cury]!='#')
{
add_to_que(*wsy,cury,curd+1);
wsy=col[cury].lower_bound(curx);
}
wsy=col[cury].lower_bound(curx);
while (wsy!=col[cury].begin() && curx-*(--wsy)<=k && mp[*wsy][cury]!='#')
{
add_to_que(*wsy,cury,curd+1);
wsy=col[cury].lower_bound(curx);
}
}
puts("-1");
}
int main()
{
n=read(),m=read(),k=read();
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>mp[i][j],lin[i].insert(j),col[j].insert(i);
sx=read(),sy=read(),ex=read(),ey=read();
BFS();
return 0;
}

解法\(2:\)

卢神发现的一个\(\Theta(nm)\)的算法\((\)我也不知道是不是这个复杂度\()\)。

每次找一个点的时候,如果在更新答案时发现当前这个新的答案不比之前的更优的话,就直接\(break\),是的,直接退出循环。

为什么这样是对的呢?如果答案是从同一条路上来的,那么显然将来的答案不会更优。如果是拐弯过来的,那么显然按照之前的走更优。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
int x=0,f=1;
char c=getchar();
while (c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while (c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
const int N=1005,inf=0x3f3f3f3f3f3f3f3f;
int n,m,k,sx,sy,ex,ey,dis[N][N],ans;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
char ch[N][N];
queue<pair<int,int> >q;
int dij()
{
memset(dis,0x3f,sizeof(dis));
q.push(make_pair(sx,sy));
dis[sx][sy]=0;
while(!q.empty())
{
int x=q.front().first,y=q.front().second; q.pop();
for(int i=0;i<4;i++)
{
for(int j=1;j<=k;j++)
{
int nx=x+dx[i]*j,ny=y+dy[i]*j;
if(nx<1 || nx>n || ny<1 || ny>m || ch[nx][ny]=='#' || dis[nx][ny]<dis[x][y]+1) break;
if(dis[nx][ny]==inf)
{
dis[nx][ny]=dis[x][y]+1;
q.push(make_pair(nx,ny));
}
}
}
}
return dis[ex][ey];
}
signed main()
{
n=read(),m=read(),k=read();
for(int i=1;i<=n;i++) scanf("%s",ch[i]+1);
sx=read(),sy=read(),ex=read(),ey=read();
if(sx==ex && sy==ey) puts("0"),exit(0);
ans=dij();
printf("%lld",ans==inf?-1:ans);
return 0;
}

CodeForces CF877D题解(BFS+STL-set)的更多相关文章

  1. [Codeforces 1246B] Power Products (STL+分解质因数)

    [Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...

  2. luogu题解P1032字串变换--BFS+STL:string骚操作

    题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...

  3. [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)

    题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...

  4. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

  5. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  6. codeforces 1093 题解

    12.18 update:补充了 $ F $ 题的题解 A 题: 题目保证一定有解,就可以考虑用 $ 2 $ 和 $ 3 $ 来凑出这个数 $ n $ 如果 $ n $ 是偶数,我们用 $ n / 2 ...

  7. Codeforces 799B - T-shirt buying(STL)

    题目链接:http://codeforces.com/problemset/problem/799/B 题目大意:有n件T恤,每件T体恤都分别有价格(每件衣服的价格不重复).前面的颜色.背部的颜色三种 ...

  8. CodeForces 907E Party(bfs+状压DP)

    Arseny likes to organize parties and invite people to it. However, not only friends come to his part ...

  9. Kilani and the Game CodeForces - 1105D (bfs)

    Kilani is playing a game with his friends. This game can be represented as a grid of size n×mn×m, wh ...

随机推荐

  1. UML online tools

    UML online tools UML https://www.diagrams.net/assets/svg/home-dia1.svg refs https://www.diagrams.net ...

  2. css & multi line words & ellipsis

    css & multi line words & ellipsis bug .news-card-content-title { width: 100%; height: 0.8rem ...

  3. c++ 去掉字符串首尾空格

    http://www.cplusplus.com/reference/regex/regex_replace/ #include <iostream> #include <regex ...

  4. 新手如何通过内存和NGK DeFi Baccarat进行组合投资?

    区块链市场在2020年迎来了大爆发,资本市场异常火热.无论是内存,还是DeFi,都无疑是这个火爆的区块链市场中的佼佼者.通过投资内存和DeFi,很多投资者都已经获取了非常可观的收益,尝到了资本市场带来 ...

  5. 教你玩转CSS Position(定位)

    CSS Position(定位) position 属性指定了元素的定位类型. position 属性的五个值: static relative fixed absolute sticky 元素可以使 ...

  6. 云原生系列6 基于springcloud架构风格的本地debug实现

    debug是程序员在日常开发中最常使用的操作, 那么,你是如何快速在微服务架构风格下快速debug后端服务呢? 开发现状 开发的理想状态 本地调测的使用步骤 登录智能网关 如果集成开发环境是在本地局域 ...

  7. 02_Mysql用户管理之Navicat下载及安装

    Navicat可以说是最好的Mysql客户端管理软件了,本博客将带你完成Navicat的下载与安装. 1.下载(https://www.navicat.com.cn/products)

  8. 腾讯云Centos7.6开放端口及配置腾讯云安全组

    1:防火墙的开启.关闭.状态查询.设置开机自启.开机禁用命令 检查状态(1):firewall-cmd --state 检查状态(2):systemctl status firewalld.servi ...

  9. 痞子衡嵌入式:盘点国内RISC-V内核MCU厂商(2020年发布产品)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是国内RISC-V内核MCU厂商(2020). 虽然RISC-V风潮已经吹了好几年,但2019年才是其真正进入主流市场的元年,最近国内大量 ...

  10. linux开启FTP服务

    目录 打开FTP服务 客户端链接时会出现的问题 打开FTP服务 先ping,查看网络是否联通 打开ssh服务 查看一些服务的状态 #查看ssh状态 service sshd status #防火墙的状 ...