主题链接: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 &amp;&amp; 双向BFS 比)的更多相关文章

  1. POJ 1915 Knight Moves

    POJ 1915 Knight Moves Knight Moves   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29 ...

  2. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  3. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  4. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  5. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

  6. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  7. BFS:HDU3085-Nightmare Ⅱ(双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  9. HDU 2243 Knight Moves

    题目: A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find th ...

随机推荐

  1. 模块化手机project ara之我见

    组装电脑,已被大部分人所熟知,只是像玩具一样组装手机,应该还仅仅是停留在想象.谷歌Project Ara将这一想象一步一步拉进现实,她希望提供一块框架,使用者能够自由地替换摄像头.显示屏.处理器.电池 ...

  2. 千万别用模板给的list.size()巨坑

    要用!list.empty()取代i<list.size();否则就不会AC了.

  3. java中clone的深入理解

    Java中Clone的概念大家应该都很熟悉了,它可以让我们很方便的“制造”出一个对象的副本来,下面来具体看看java中的Clone机制是如何工作的?      1. Clone和Copy      假 ...

  4. 委托、Lambda和事件

    委托 委托相当于C语言当中的函数指针,不过委托是类型安全的类,它定义了返回类型和参数的类型. 声明委托 在C#中使用一个类,分为两个阶段.首先,需要定义这个类,告诉编译器这个类由什么字段和方法组成,然 ...

  5. TMG 2010 VPN配置

    微软的ISA 到2006以后就叫TMG了,上周在公司的服务器上安装测试了下,虽然增加了很多功能,但是主要功能上和ISA 2004差不多,最近在部署L2TP VPN,由于防火墙带的远程访问VPN为纯的L ...

  6. httl开源JAVA模板引擎,动态HTML页面输出

    HTTL(Hyper-Text Template Language)是一个适用于HTML输出的开源JAVA模板引擎,适用于动态HTML页面输出,可用于替代JSP页面,它的指令类似于Velocity. ...

  7. httpcomponents-client-4.4.x

    Chapter 1. Fundamentals Prev     Next Chapter 1. Fundamentals 1.1. Request execution The most essent ...

  8. 基于.net开发chrome核心浏览器【三】

    原文:基于.net开发chrome核心浏览器[三] 本篇我们讲解怎么用CefGlue开发一个最简单的浏览器 一: CefGlue是建立在Cef项目之上的,Cef项目是C/C++的项目:CefGlue只 ...

  9. 询问任意区间的min,max,gcd,lcm,sum,xor,or,and

    给我们n个数,然后有m个询问,每个询问为L,R,询问区间[L,R]的最大最小值,最小公约数,最大公约数,和,异或,或,且 这些问题通通可以用RMQ的思想来解决. 以下用xor来作为例子 设dp[i][ ...

  10. altKey,ctrlKey,shiftKey

    <1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>< ...