博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
75: libreoj #10028 双向宽搜
阅读量:4308 次
发布时间:2019-06-06

本文共 1510 字,大约阅读时间需要 5 分钟。

$des$

实现一个bfs

$sol$

写了一个双向bfs

#include 
using namespace std;#define Rep(i, a, b) for(int i = a; i <= b; i ++)#define gc getchar()inline int read() { int x = 0; char c = gc; while(c < '0' || c > '9') c = gc; while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc; return x;}const int N = 305;const int xd[] = {-1, -2, -2, -1, 1, 2, 2, 1};const int yd[] = {-2, -1, 1, 2, 2, 1, -1, -2};int Lim = 300;int vis[N][N], bel[N][N];int n;int Bfs_time;queue
> Q;inline int Work(int sx, int sy, int tx, int ty) { if(sx == tx && sy == ty) return 0; memset(vis, 0, sizeof vis); memset(bel, 0, sizeof bel); while(!Q.empty()) Q.pop(); Q.push(make_pair(sx, sy)); Q.push(make_pair(tx, ty)); bel[sx][sy] = 1, bel[tx][ty] = 2; vis[tx][ty] = 1; while(!Q.empty()) { pair
tp = Q.front(); Q.pop(); int px = tp.first, py = tp.second; Rep(i, 0, 7) { int nx = px + xd[i], ny = py + yd[i]; if(bel[nx][ny] == bel[px][py] || nx < 0 || nx > Lim || ny < 0 || ny > Lim) continue; if(vis[nx][ny]) return vis[nx][ny] + vis[px][py]; vis[nx][ny] = vis[px][py] + 1; bel[nx][ny] = bel[px][py]; Q.push(make_pair(nx, ny)); } }}int main() { n = read(); while(n --) { Lim = read(); cout << Work(read(), read(), read(), read()) << "\n"; } return 0;}

 

转载于:https://www.cnblogs.com/shandongs1/p/9873579.html

你可能感兴趣的文章
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>
必学高级SQL语句
查看>>
经典SQL语句大全
查看>>
log日志记录是什么
查看>>
<rich:modelPanel>标签的使用
查看>>
<h:commandLink>和<h:inputLink>的区别
查看>>
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>
suse创建的虚拟机,修改ip地址
查看>>
linux的挂载的问题,重启后就挂载就没有了
查看>>
docker原始镜像启动容器并创建Apache服务器实现反向代理
查看>>
docker容器秒死的解决办法
查看>>
管理网&业务网的一些笔记
查看>>
openstack报错解决一
查看>>
openstack报错解决二
查看>>
linux source命令
查看>>