#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. 萌新的IDEA_web开发笔记(未完)

    萌新IDEA_web开发笔记 按兴趣自己搞的网页: http://47.94.140.98:8080/ow_web/my_web/web/ 暂时还没做完. 部署在租的服务器上面,背景视频加载可能有点慢 ...

  2. powerdesigner添加mysql的字符集支持

    一般建模可能都会用到:powerdesigner 但是,在建表的时候,我一直没有找到: DEFAULT CHARACTER SET COLLATE 两个选项.因此,想了个方法,点击:工具栏->d ...

  3. 【原创】大数据基础之ElasticSearch(3)升级

    elasticsearch版本升级方案 常用的滚动升级过程(Rolling Upgrade)如下: $ curl -XPUT '$es_server:9200/_cluster/settings?pr ...

  4. 【原创】大叔问题定位分享(18)beeline连接spark thrift有时会卡住

    spark 2.1.1 beeline连接spark thrift之后,执行use database有时会卡住,而use database 在server端对应的是 setCurrentDatabas ...

  5. 移动端遮罩及阻止页面滑动,实用!!! 我们经常做一个fixed定位的遮罩和一个提示弹框,这时就要用到。记录--

    document.body.style.height = '100%'; document.body.style.overflow = 'hidden'; document.getElementByI ...

  6. fs.inotify.max_user_watches默认值太小,导致too many open files

    运行环境:centos7.5 linux 打开文件数 too many open files 解决方法fs.inotify.max_user_watches默认值太小,导致too many open ...

  7. Apache:SSLCertificateFile:文件不存在或为空(操作系统RHEL7)

    配置参数及报错信息(配置了TLS加密后httpd服务无法启动) 我在httpd.conf文件中引用了上面创建的文件.但在那之后,当我尝试RESTART Apache时,我遇到以下错误: 错误:SSLC ...

  8. sass—使用自定义function和@each实现栅格布局

    /*使用自定义function和@each实现栅格布局*/ @function buildLayout($num: 5){ $map: (defaultValue: 0); //不能直接生成col,需 ...

  9. .net core2.x - Identity - 简介

  10. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-10项目各种全局帮助类

    本文目录 1.  前沿2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装3.XmlHelper快速操作xml文档4.SerializationHe ...