#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. Codevs 1003 电话连线

    时间限制: 1 s   空间限制: 128000 K   题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当 ...

  2. Xcode中为代码添加特殊标记

    有时候,我们需要在代码中搜索特殊的符号或者代码段,根据符号或使用搜索功能导航代码段效率并不算高.为了使用普通的英语标识重要的代码片段,可在代码中插入特殊格式的注释.这些注释不会在应用程序中添加任何特殊 ...

  3. C# 操作.ini文件

    1.声明变量 #region "声明变量" /// <summary> /// 写入INI文件 /// </summary> /// <param n ...

  4. jasonTree多选多级树控件

    jasonTree1.0 jasonTree多选多级树控件(名字是自己取),用于友好的展示树形结构的数据,并可以多选,传统的做法是在一个select的下拉框中显示一个可折叠的树结构,公司的需求人员这种 ...

  5. gulp插件

    gulp是趋势 gulp完全开发指南 => 快来换掉你的Grunt吧 gulp的工作流程:文件流--文件流--文件流......因为grunt操作会创建临时文件,会有频繁的IO操作,而gulp使 ...

  6. curl 报错记录,mark

    今天在做接口开发的时候,使用curl post ,请求返回数据为 null ,很纳闷,然后使用 curl_errno 打印出来的错误代码为 28 ,curl_error($ch) 打印出来的是Oper ...

  7. cookie文件在电脑的保存位置

    在Windows系统上(这里以Win7为例)浏览器的Cookie IE浏览器Cookie数据位于:%APPDATA%\Microsoft\Windows\Cookies\ 目录中的xxx.txt文件 ...

  8. return *this和return this的区别

    别跟我说, return *this返回当前对象, return this返回当前对象的地址(指向当前对象的指针). 正确答案为:return *this返回的是当前对象的克隆(当然, 这里仅考虑返回 ...

  9. 微软职位内部推荐-Senior SDE for Win Shell Exp

    微软近期Open的职位: Job posting title: Senior Software Development Engineer Location: China, Beijing Divisi ...

  10. redhat或centos关闭防火墙并开启sshd服务

    使用putty连接虚拟机的redhat连不上时处理方案: 这里使用的是VMware Workstation,  将宿主机与虚拟机之间的网络使用 ‘桥接方式’: 1.关闭宿主机与虚拟机的防火墙, 在re ...