博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
双向BFS
阅读量:4537 次
发布时间:2019-06-08

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

1 /* 2 转自http://blog.csdn.net/custqi/article/details/6455425 3 感觉对双向广搜写得挺清楚的 4 */ 5 #include
6 #include
7 #include
8 using namespace std; 9 const int maxn = 301;10 int n;11 int used[maxn][maxn];12 int g[maxn][maxn];13 int d[8][2] = { { -2, -1 }, { -2, 1 }, { -1, -2 }, { -1, 2 }, { 1, -2 }, { 1, 2 }, { 2, -1 }, { 2, 1 } };14 15 struct Point16 {17 int x, y;18 }st, ed;19 int Check(int x, int y)20 {21 return (x >= 0 && x
= 0 && y
Q;27 Point curNode, nextNode;28 memset(used, 0, sizeof(used));29 //放入起始结点30 g[st.x][st.y] = 0;31 used[st.x][st.y] = 1;//由起始结点方向扩展的结点记录值为 1 32 Q.push(st);33 //放入终止结点34 g[ed.x][ed.y] = 0;35 used[ed.x][ed.y] = 2;//由终止结点方向扩展的结点记录值为 2 36 Q.push(ed);37 //同时放入起始结点和终止结点 进行双向广搜38 while (!Q.empty())39 {40 curNode = Q.front();41 Q.pop();42 sx = curNode.x;43 sy = curNode.y;44 for (int i = 0; i<8; i++)45 {46 tx = sx + d[i][0];47 ty = sy + d[i][1];48 if (!Check(tx, ty)) continue;49 if (used[tx][ty] == 0) //如果used[tx][ty]未访问过 50 {51 g[tx][ty] = g[sx][sy] + 1; //更新在tx ty的值 52 nextNode.x = tx;53 nextNode.y = ty;54 used[tx][ty] = used[sx][sy]; // 55 Q.push(nextNode);56 }57 else if (used[sx][sy] != used[tx][ty]) //起始方向与终止方向同时进行后,此时的结点为俩个方向相遇的点,得到结果 58 {59 return g[sx][sy] + g[tx][ty] + 1;60 }61 }62 }63 return 0;64 }65 int main()66 {67 int TestCases;68 scanf("%d", &TestCases);69 while (TestCases--)70 {71 scanf("%d%d%d%d%d", &n, &st.x, &st.y, &ed.x, &ed.y);72 printf("%d/n", solve());73 }74 return 0;75 }

 

转载于:https://www.cnblogs.com/usedrosee/p/4293417.html

你可能感兴趣的文章
JQuery怎样返回前一页
查看>>
Best Time to Buy and Sell Stock
查看>>
Web服务器的原理
查看>>
记录ok6410 jlink 命令行调试uboot
查看>>
ASP.net 内置对象
查看>>
QT使用mysql
查看>>
判断有无网
查看>>
php开发环境搭建
查看>>
HTML5表单那些事
查看>>
Spring MVC 学习总结(五)——校验与文件上传
查看>>
Spring 4 官方文档学习 Spring与Java EE技术的集成
查看>>
cocos+kbe问题记录
查看>>
自动化测试框架selenium+java+TestNG——配置篇
查看>>
测量标准体重
查看>>
(转)关于Android中为什么主线程不会因为Looper.loop()里的死循环卡死?引发的思考,事实可能不是一个 epoll 那么 简单。...
查看>>
SQL*Plus 系统变量之32 - NEWP[AGE]
查看>>
Spring配置文件总结
查看>>
4.三角形面积
查看>>
基础-事务
查看>>
MAC下安装与配置MySQL [转]
查看>>