主题链接: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. 连载:面向对象葵花宝典:思想、技巧与实践(28) - 设计原则:内聚&amp;耦合

    前面通过实例解说了一个一环扣一环的面向对象的开发流程:用例模型 -> 领域模型 -> 设计模型(类模型 + 动态模型),解答了面向对象怎样做的问题.接下来我们就要讲"怎样做好面向 ...

  2. 编程算法 - 萨鲁曼的军队(Saruman&#39;s Army) 代码(C)

    萨鲁曼的军队(Saruman's Army) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 直线上有N个点, 每个点, 其距离为R以内的区域里 ...

  3. 什么是Java “实例化”

    实例化:对象也是引用数据类型,只能使用new运算符从堆中分配内存: 使用已经定义好的类,创建该类对象的过程称为“实例化”. 只有先实例化类的对象,才可以访问到类中的成员(属性和方法). 使用成员运算符 ...

  4. cmake编译时遇到的问题解决

    编译cmake首先须要gcc环境,能够运行 gcc --version命令看看. 假设没有,能够使用yum或从cd中进行安装,此处是在虚拟机中从cd中进行安装.将cd链接到虚拟机都会吧,此处略去,.. ...

  5. Nginx 负载均衡-加权轮询策略剖析

    本文介绍的是客户端请求在多个后端服务器之间的均衡,注意与客户端请求在多个nginx进程之间的均衡相区别(Nginx根据每个工作进程的当前压力调整它们获取监听套接口的几率,那些当前比较空闲的工作进程有更 ...

  6. Do you master on array in C ?

    Do you master on array in C ? 因为新标准C99的支持变长数组, 差点儿C的标准特性就是看着gcc来的(Linux 内核严重依赖GCC) int mani() { cons ...

  7. configure: error: zlib not installed

    export LDFLAGS="-L/usr/local/zlib/lib" export CPPFLAGS="-I/usr/local/zlib/include&quo ...

  8. zoj3640(概率dp)

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4808 题意: 一个吸血鬼,每次可以随机的选择n个洞中的任意一个,如果 ...

  9. Effective C++ -- 构造析构赋值运算

    05.了解C++默默编写并调用哪些函数 编译产生的析构函数时non-virtual,除非这个类的基类析构函数为virtual 成员变量中有引用和const成员时,无法自己主动生成copy assign ...

  10. Android开源项目pulltorefresh分析与简单使用

    在Android开发中有时我们须要訪问网络实时刷新数据.比方QQ好友在线状态最新信息,QQ空间须要显示很多其它的好友动态信息,EOE论坛client显示很多其它的文章帖子信息等.android-pul ...