#include<iostream>
#include<stdio.h>
#define MAXN 350
#include"queue"
using namespace std;
bool mark[MAXN][MAXN];
struct point
{
int x;
int y;
int step;
point()
{
step = ;
}
};
bool bfs(point p);
point b;
int num;
point e;
point tem;
queue<point> coll;
int main()
{
//freopen("acm.acm","r",stdin);
int test;
int ans;
cin>>test;
while(test --)
{
//memset(road,0,sizeof(road));
memset(mark,false,sizeof(mark));
cin>>num;
cin>>b.x>>b.y;
cin>>e.x>>e.y;
if(b.x == e.x && b.y == e.y)
{
cout<<<<endl;
continue;
}
coll.push(b);
mark[b.x][b.y] = true;
// cout<<"----------"<<endl;
while(!coll.empty() && !bfs(coll.front()) )
{
//cout<<"-================"<<endl;
// cout<<coll.front().x<<endl;
// cout<<coll.front().y<<endl;
//break;
coll.pop();
}
while(!coll.empty())
{
coll.pop();
}
}
} bool bfs(point p)
{
if(p.x - >= )
{
if(p.y - >= && !mark[p.x - ][p.y - ])
{
tem.x = p.x - ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.y + < num && !mark[p.x - ][p.y + ])
{
tem.x = p.x - ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
}
////////////////
if(p.x + < num)
{
if(p.y - >= && !mark[p.x + ][p.y - ])
{
tem.x = p.x + ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.y + < num && !mark[p.x + ][p.y + ])
{
tem.x = p.x + ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
} ///////////////////
if(p.y - >= )
{
if(p.x - >= && !mark[p.x - ][p.y - ])
{
tem.x = p.x - ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.x + < num && !mark[p.x + ][p.y - ])
{
tem.x = p.x + ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
}
//////////////////////
if(p.y + < num)
{
if(p.x - >= && !mark[p.x - ][p.y + ])
{
tem.x = p.x - ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.x + < num && !mark[p.x + ][p.y + ])
{
tem.x = p.x + ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
}
return false;
}

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

技术网站地址: vmfor.com

POJ 1915的更多相关文章

  1. POJ 1915 Knight Moves

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

  2. OpenJudge/Poj 1915 Knight Moves

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

  3. poj 1915 http://poj.org/problem?id=1915

    /**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...

  4. POJ 1915 Knight Moves(BFS+STL)

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

  5. POJ 1915 经典马步 双向bfs

    拿这个经典题目开刀...........可是双向时间优势在这题上的效果不太明显 #include <iostream> #include <algorithm> #includ ...

  6. poj 1915 KnightMoves(bfs)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24094   Accepted: 11364 De ...

  7. 论深度优先(DFS)和广度优先搜索(BF)的优点及不足(更新ing)

    例题: POJ 1915 Knight Moves 骑士遍历问题(跳马问题) 在一个m*m的棋盘上,从任意一个给定的位置(sx , sy)出发,为象棋中的马找一条路通过最少的步数到达另一位置(ex , ...

  8. BFS(三):双向广度优先搜索

    所谓双向广度搜索指的是搜索沿两个方向同时进行:(1)正向搜索:从初始结点向目标结点方向搜索:(2)逆向搜索:从目标结点向初始结点方向搜索:当两个方向的搜索生成同一子结点时终止此搜索过程. 广度双向搜索 ...

  9. POJ 3488 &amp; HDU 1915 Arne Saknussemm(模拟)

    题目链接: POJ:http://poj.org/problem? id=3488 HDU:pid=1915">http://acm.hdu.edu.cn/showproblem.ph ...

随机推荐

  1. 将string转换成char*

    string 是c++标准库里面其中一个,封装了对字符串的操作  把string转换为char* 有3中方法:  1.data  如:  string str="abc";  ch ...

  2. IE Problem : inetcpl.cpl

    从windows8升级windows8.1后每次关闭ie11浏览器总是跳出出现一个问题导致关闭,我都快疯了,以前windows8一点问题都没有.这是怎么回事?还有能回到ie10吗?(张浩228) 小昕 ...

  3. jQuery 手风琴侧边菜单

    动手做了一个简单手风琴菜单,上图: 点击 B 可收缩 C 列表,点击 C 改变自身和父节点 B 的样式,悬浮时均有不同的样式改变. 先看页面代码,列表的嵌套: <div id="men ...

  4. sql中更新数据库用到declare @a in

    declare @a in update TB_Class set @a=1,name='李小龙' where ID=1 这样就可以像更新哪个就更新哪个了 例如ibatisnet中需要更新的时候: & ...

  5. 9种jQuery和css3图片动画特效代码演示

    1.自由旋转的jQuery图片 演示和下载地址 2.css3阴影动画效果 演示和下载地址 3.拉窗帘特效图片 演示和下载地址 4.css3文字特效动画 演示和下载地址 5.css3时钟代码 演示和下载 ...

  6. window store app 附件读取

    public static async Task<bool> DisplayApplicationPicker(string folderName, string fileName) { ...

  7. DataGridview动态添加列

    1.获取数据源(select * from table名称) 2.动态绑定数据源 private void GetTableInfo(DataTable dt) { listBh = new List ...

  8. Python开发【第一篇】Python基础之自定义模块和内置模块

    为什么要有模块,将代码归类.模块,用一砣代码实现了某个功能的代码集合. Python中叫模块,其他语言叫类库. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代 ...

  9. Jquery LigerUI框架学习(一)

    ligerUI框架是一个很丰富的后台框架模板,具有简洁大方的后台样式框架,还有很多灵活的控件,方便开发人员使用: 把昨天学习的成功拿出来供大家学习学习: 首先我们要去ligerUI官网下载Jquery ...

  10. hdu 5720 BestCoder 2nd Anniversary Wool 推理+一维区间的并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5720 题意:有n(n <= 105)个数 ,每个数小于等于 1018:问在给定的[L,R]区间中 ...