#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. JavaScript 中undefined,null,NaN的区别

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型.var a1;var a2 = true;va ...

  2. Virtualizing WrapPanel VS toolkit:WrapPanel

    用toolkit:WrapPanel的时候,LIST太大,内存不行,等下我试试 Virtualizing WrapPanel这个 http://www.codeproject.com/Articles ...

  3. UBUNTU12.4 安装磊科无线网卡驱动

    UBUNTU12.4 安装磊科无线网卡驱动 在淘宝低价买了一个网卡,回来发现不能用 ,擦 无语了. 无赖只能在网上各种找驱动,编译 安装 .今天在终于安装好了WIFI驱动了: 下载地址:https:/ ...

  4. zedboard 中SDK 修改串口设置(波特率。。。。)

    其实在zedboard   SDK中不用初始化串口的也就是platform()可以不写 ,初始化在EDK导入SDK中就写好了  具体看bsp文件夹下面的汇编.但是如果我们想要在SDK中改变串口设置的话 ...

  5. Android String format 通过value 下的string.xml 文件

    <string name="format_coordinate" formatted="false">%s %d° %d\' %d\" % ...

  6. 【个人】IIS Express 配置

    <!-- 查看URL访问控制列表: netsh http show urlacl 添加URL访问控制: netsh http add urlacl url=http://myhostname:8 ...

  7. C#简单实现发送手机短信

    偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2) ...

  8. UE4 将本地图片转成UTexture2D 在runtime显示

    UFUNCTION(BlueprintCallable, Category = "TextureFromDisk") static class UTexture2D* GetTex ...

  9. Oracle varchar2 4000

    关于oracle varchar2 官方文档的描述 VARCHAR2 Data Type The VARCHAR2 data type specifies a variable-length char ...

  10. platform

    作者yuanlulu httpblogcsdnnetyuanlulu版权没有但是转载请保留此段声明 第1章platform驱动管理机制 platform_device 数据结构 注册流程 platfo ...