POJ 1915-Knight Moves (单向BFS && 双向BFS 比)
主题链接:Knight Moves
题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路
研究了一下双向BFS,不是非常难,和普通的BFS一样。双向BFS只是是从 起点和终点同一时候開始搜索,可降低搜索时间
当两条搜索路线相遇时,结束。
貌似有一年百度的招聘 笔试,就是双向BFS。
。。。
以下,比較一下BFS 和 双向BFS的用时。
BFS
STL的queue可能会浪费一点时间
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
const int N = 310;
using namespace std;
int mapp[N][N];
bool vis[N][N];
struct node{
int x,y,ans;
};
int n;
int mv[8][2] = {{1,2},{1,-2},{2,1},{2,-1},{-2,1},{-2,-1},{-1,2},{-1,-2}};
void BFS(int sx,int sy,int ex,int ey)
{
queue<node>q;
node t,f;
memset(vis,0,sizeof(vis));
f.x = sx; f.y = sy; f.ans = 0;
vis[sx][sy] = true;
q.push(f);
while(!q.empty())
{
t = q.front();
q.pop();
if(t.x==ex && t.y==ey)
{
printf("%d\n",t.ans);
return ;
} for(int i = 0;i<8;i++)
{
f.x = t.x + mv[i][0];
f.y = t.y + mv[i][1];
if(!vis[f.x][f.y]&& 0<=f.x && f.x <n && 0<=f.y && f.y<n)
{
f.ans = t.ans + 1;
q.push(f);
vis[f.x][f.y] = true;
}
}
}
}
int main()
{
int t,sx,sy,ex,ey;
std::ios::sync_with_stdio(false);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%d%d",&sx,&sy);
scanf("%d%d",&ex,&ey);
BFS(sx,sy,ex,ey);
}
return 0;
}
双向BFS
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2p3MDEzMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
时间差的不是非常大,可是理论上,时间复杂度会降低若干倍。假设数据大的话,时间差会非常明显
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
const int N = 310;
using namespace std;
int mapp[N][N];
int vis[N][N];
struct node{
int x,y;
};
int n;
int mv[8][2] = {{1,2},{1,-2},{2,1},{2,-1},{-2,1},{-2,-1},{-1,2},{-1,-2}};
int dis[N][N];
void BFS(int sx,int sy,int ex,int ey)
{
if(sx==ex && sy == ey)
{
printf("0\n");
return ;
}
queue<node>q;
node t,f;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
f.x = sx; f.y = sy;
t.x = ex; t.y = ey;
vis[sx][sy] = 1; //从起点開始搜索的路线 标记为1
vis[ex][ey] = 2; //从终点開始搜索的路线 标记为2
q.push(f);
q.push(t);
while(!q.empty())
{
t = q.front();
q.pop();
for(int i = 0;i<8;i++)
{
f.x = t.x + mv[i][0];
f.y = t.y + mv[i][1];
if(0<=f.x && f.x <n && 0<=f.y && f.y<n)
{
if(!vis[f.x][f.y])
{
dis[f.x][f.y] = dis[t.x][t.y] + 1;
q.push(f);
vis[f.x][f.y] = vis[t.x][t.y];
}
else if(vis[f.x][f.y]!=vis[t.x][t.y]) //两者相遇
{
printf("%d\n",dis[f.x][f.y]+dis[t.x][t.y] + 1); //会漏掉相遇的那个点,所以要加1
return ;
} }
}
}
}
int main()
{
int t,sx,sy,ex,ey;
std::ios::sync_with_stdio(false);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%d%d",&sx,&sy);
scanf("%d%d",&ex,&ey);
BFS(sx,sy,ex,ey);
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
POJ 1915-Knight Moves (单向BFS && 双向BFS 比)的更多相关文章
- POJ 1915 Knight Moves
POJ 1915 Knight Moves Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29 ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- OpenJudge/Poj 1915 Knight Moves
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- POJ 2243 Knight Moves
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13222 Accepted: 7418 Des ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- BFS:HDU3085-Nightmare Ⅱ(双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- HDU 2243 Knight Moves
题目: A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find th ...
随机推荐
- 【剑指offer】q34:丑数
题目要求第n个丑数.所以对于中间结果不须要保存. def Humble(index): curHum = 1 M2 = 2; M3 = 3; M5 = 5 while index > 1: cu ...
- Fast portable non-blocking network programming with Libevent
Fast portable non-blocking network programming with Libevent Fast portable non-blocking network prog ...
- 自顶向下分析Binder【1】—— Binder实例篇
欢迎转载,转载请注明:http://blog.csdn.net/zhgxhuaa 一个Binder实例 我们Binder的学习将从以下的一个实例開始.依据Android文档中的描写叙述,创建一个Bin ...
- SQLServer 复制中移除和加入公布而不初始化全部项目
-- 若提前"禁止架构更改".新增的列不会自己主动加入大公布.此时应使用 sp_articlecolumn 加入列 EXEC sp_changepublication @publi ...
- php获取分类以下的全部子类方法
获取分类以下的全部子类方法: static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True) { static $ ...
- cocos2dx A* + tiledMap
本文转自:http://blog.csdn.net/w18767104183/article/category/1757765 前面一章讲了cocos2dx 中使用A星算法 这章中讲 A*结合tile ...
- jps查看java进程中哪个线程在消耗系统资源
jps或ps -ef|grep java可以看到有哪些java进程,这个不用说了.但值得一提的是jps命令是依赖于/tmp下的某些文件 的. 而某些操作系统,定期会清理掉/tmp下的文件,导致jps无 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- python几道简单的算法题
最近看了python的语法,但是总感觉不知道怎么使用它,还是先来敲敲一些简单的程序吧. 1.题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十 ...
- core graphics path
当UIKit无法满足画图需求的时候.就须要用到Core Graphics API.当中最普遍的就是path. 一些重要的概念 graphics context 能够理解成canvas.在ios里相应C ...