#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. 杭电ACM2092--整数解

    杭电ACM2092--整数解    分析 http://acm.hdu.edu.cn/showproblem.php?pid=2092 一个YES,一个Yes.试了10几次..我也是无语了..哪里都不 ...

  2. Google面试题:计算从1到n的正数中1出现的次数

    题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. 找工作,准备看写题目,题目说是Goo ...

  3. 简单版解决IE兼容性问题

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 在访问页面时, 如 ...

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

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

  5. JQuery在iframe中实现 点击后选中当前栏目的样式

    二级或者三级折叠菜单参考http://www.cnblogs.com/qigege/p/5178947.html <script type="text/javascript" ...

  6. referenceerror wx is not defined 微信JsSdk开发

    如果你和我一样遇到了“referenceerror wx is not defined”错误,很有可能是jweixin-1.0.0.js与你其它某js冲突. 解决办法: <script type ...

  7. [转]WPF 依赖项属性

    from:http://blog.csdn.net/datoumimi/article/details/8033682 ps:环境限制,发的东西一长就会被拦截,所以删了一部分 UI软件中经常会用到大量 ...

  8. 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  9. mysql 配置主从

    1.选择2个ip,1个为主,1个为从:例:主:192.168.12.76 从:192.168.12.772.在192.168.12.76的my.cnf 配置master,添加如下:(红色为添加的内容) ...

  10. [Java][RCP] 引入第三方jar包时出错: XXXcannot be found XXX

    为什么会这样? 下面的博客有介绍,不在累赘 http://dengmin.iteye.com/blog/260585 这些博客貌似忘掉了一点,或者是我本地的Eclipse新建的项目Version不够高 ...