#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. 递归 - Leetcode 110 判断二叉树是否为平衡二叉树

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  2. linux文件系统初始化过程(4)---加载initrd(中)

    一.目的 上文详细介绍了CPIO格式的initrd文件,本文从源代码角度分析加载并解析initrd文件的过程. initrd文件和linux内核一般存储在磁盘空间中,在系统启动阶段由bootload负 ...

  3. html-webpack-template, 一个更好的html web service插件

    源代码名称:html-webpack-template 源代码网址:http://www.github.com/jaketrent/html-webpack-template html-webpack ...

  4. ActiveMQ简单介绍及安装

    消息中间件 我们简单的介绍一下消息中间件,对它有一个基本认识就好,消息中间件(MOM:Message Orient middleware). 消息中间件有很多的用途和优点: 1. 将数据从一个应用程序 ...

  5. GitHub界面初识

      现在很多 HR 在招聘程序员的需求都会提到「有 Github 项目者优先」,大部分求职者也会在简历中附上 Github 链接. 作为一个专业的 HR,即便不懂代码,也不能被一个链接唬住.今天我就手 ...

  6. python学习第20天

    python中的继承 单继承 多继承

  7. 通过语法设置DNS解析

    通过语法设置DNS解析 # 来自 https://dns.he.net/?action=logout # 语法 http://[你的域名]:[你的密码]@dyn.dns.he.net/nic/upda ...

  8. js分析 天_眼_查 字体文件

    0. 参考 js分析 猫_眼_电_影 字体文件 @font-face 1. 分析 1.1 定位目标元素 1.2 查看网页源代码 1.3 requests 请求提取得到大量错误信息 对比猫_眼_电_影抓 ...

  9. HDU4864:Task(贪心)

    Problem DescriptionToday the company has m tasks to complete. The ith task need xi minutes to comple ...

  10. arp_spoof脚本的编写

    协议包分析 ARP原理: 在PC1 访问 Server 的流量 要经过网关  也就是 192.168.1.254 欺骗原理:pc1  与 网关 建立 MAC  ARP 缓存 我们将KALI 攻击机的 ...