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 ...
随机推荐
- memwatch的使用
博主的新Blog地址:http://www.brantchen.com 欢迎訪问:) linux下的測试工具真是少之又少,还不好用,近期试用了memwatch,感觉网上的介绍不太好,所以放在这里跟大家 ...
- .idata数据的解析
每类Section代表不同的数据,不同的数据存储组织方式一定是有非常大区别的.代码段与资源段一定区别巨大,这意味着我需要一个一个的学习每个段的解析. idata段解析 这个段主要存储的是导入符号信息. ...
- 真实世界里的钢铁侠-特斯拉汽车创始人埃隆·马斯克(Elon Musk)
真实世界里的钢铁侠--特斯拉汽车公司和SpaceX公司总裁马斯克(31岁).当我们得意于「站在山上踢几块石头下去」或是「站在风口上的猪」的成功理论的时候,我们真的成功了吗?我们到底创造了什么?改变了什 ...
- jQuery选择器实现隔行变色和使用javaScript实现隔行变色
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <!--什么是选择器? jQuery选择器继承了 ...
- ASA虚墙配置
asa配置ASA Version 8.0(2) <system>!hostname ASA5520enable password 2KFQnbNIdI.2KYOU encryptedno ...
- CF 518D(概率dp)
传送门:Ilya and Escalator 题意:有n个人排队进车厢,每秒只能进一个人,而且第1个人进了后面的人才能进,第一个人每秒进入车厢的概率为p,不进的概率为1-p,求t秒后进入车厢总人数的数 ...
- Flume传输数据事务分析
Flume传输数据事务分析 本文基于ThriftSource,MemoryChannel,HdfsSink三个组件,对Flume传输数据的事务进行分析.假设使用的是其它组件.Flume事务详细的处理方 ...
- [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
XTools Pro 是一套为ArcGIS平台设计的矢量空间分析. 形状转换和表管理扩展工具,大大增强了 ArcGIS 的功能,使用该工具能够提高 ArcGIS 用户的效率和性能. XTools Pr ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- Django写的投票系统2(转)
在上一篇中 django实例:创建你的第一个应用投票系统(一) 已经介绍基本的功能,并已经启动服务了.这一节介绍数据库相关的东东. 首页打开mysite/settings.py配置文件, 设置数据库打 ...