#include<iostream>
using namespace std;
#define SIZE 305 int vis[SIZE][SIZE];
int sx,sy,ex,ey;
int dx[]={-,-,-,-,,,,};
int dy[]={-,-,,,,,-,-};
int n,case_num;
typedef struct node
{
int x;
int y;
int step;
}node;
node queue[SIZE*SIZE]; void bfs()
{
int head,tail;
head=tail=;
node startnode;
startnode.x=sx;
startnode.y=sy;
startnode.step=;
queue[tail++]=startnode;
vis[startnode.x][startnode.y]=;
node cur,next;
while(head!=tail)
{ int nx,ny;
cur=queue[head++];
if(cur.x==ex&&cur.y==ey)
{
cout<<cur.step<<endl;
return;
}
for(int i=;i<;i++)
{
nx=cur.x+dx[i];
ny=cur.y+dy[i];
if(nx>=&&nx<n&&ny>=&&ny<n&&vis[nx][ny]==)
{
next.x=nx;
next.y=ny;
next.step=cur.step+;
queue[tail++]=next;
vis[next.x][next.y]=;
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
cin>>case_num;
while(case_num--)
{
cin>>n;
cin>>sx>>sy>>ex>>ey;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
vis[i][j]=;
bfs();
}
return ;
}

poj1915的更多相关文章

  1. poj1915 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...

  2. poj1915(双向bfs)

    题目链接:https://vjudge.net/problem/POJ-1915 题意:求棋盘上起点到终点最少的步数. 思路:双向广搜模板题,但玄学的是我的代码G++会wa,C++过了,没找到原因QA ...

  3. poj1915 BFS

    D - 广搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bi ...

  4. 超超超简单的bfs——POJ-1915

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26102   Accepted: 12305 De ...

  5. POJ-1915 Knight Moves (BFS)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26952   Accepted: 12721 De ...

  6. BFS广度优先搜索 poj1915

    Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...

  7. poj2243+poj1915骑士问题

    2243是骑士问题,八个格子的,BFS,因为要最短路经,所以没有用A*,A*跑不出来,太慢了,因为要搜索到所有解啊!一直更新最优,而BFS,一层一层搜索,第一次得到的便是最短的了!300格子,标记的话 ...

  8. POJ1915 BFS&双向BFS

    俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...

  9. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

随机推荐

  1. MS SQL Server 时间函数

    日期和时间数据类型 数据类型 存储(字节) 日期范围 精确度 格式示例 DateTime 8 1753年1月1日 - 9999年12月31日 3 1/3毫秒 yyyy-MM-dd hh:mm:ss.n ...

  2. 【原创】大数据基础之Ambari(2)通过Ambari部署ElasticSearch(ELK)

    ambari2.7.3(hdp3.1) 安装 elasticsearch6.3.2 ambari的hdp中原生不支持elasticsearch安装,下面介绍如何通过mpack方式使ambari支持el ...

  3. 【原创】大叔经验分享(27)linux服务器升级glibc故障恢复

    redhat6系统默认安装的glibc-2.12,有的软件依赖的是glibc-2.14,这时需要升级glibc,下载安装 http://ftp.gnu.org/gnu/glibc/glibc-2.14 ...

  4. 点击页面上的元素,页面删除removeChild()

    简单描述:最近做了一个图片上传,上传完成回显图片的时候,需要用到点击图片,从页面删除的效果,然后就找到了removeChild()方法,说实话,我刚看到的时候,就觉得这个问题已经解决了,但是却发现这个 ...

  5. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  6. 斐波那契求第n项

    摘自  https://blog.csdn.net/lpjishu/article/details/51323116 斐波那契求第n项是常见的算法题 方法1  递归法 //斐波那契 0,1,1,2,3 ...

  7. 一步一步安装SQL Server 2017

    快速开始: 下载安装文件:https://www.microsoft.com/en-us/sql-server/sql-server-downloads-free-trial 应该选择哪个版本? Th ...

  8. [NewLife.XCode]高级增删改

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netstandard,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示 ...

  9. [C#] 動的にアセンブリをロードする

    アプリケーション ドメインにおいて起動時に読み込まれない別のアセンブリ (.dll や .exe) を読み込む場合.System.Reflection.Assemby クラスの Load メソッドを使 ...

  10. Windows应用程序组成及编程步骤

    Windows应用程序组成及编程步骤: 1.应用程序的组成:一个完整的Windows应用程序通常由五种类型的文件组成 1.C语言源程序文件 2.头文件 3.模块定义文件 4.资源描述文件 5.项目文件 ...