Knight Moves
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 20913   Accepted: 9702

Description

Background

Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?

The Problem

Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.

For people not familiar with chess, the possible knight moves are shown in Figure 1.

                      

Input

The input begins with the number n of scenarios on a single line by itself.

Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers
{0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The
distance must be written on a single line.

Sample Input

3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1

Sample Output

5
28
0

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 350
using namespace std; int n;
int x1,x2,y1,y2; struct node{
int x;
int y;
int sum;
node(int a,int b,int c)
{
x=a;
y=b;
sum=c;
} }; int go[8][2]={{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,1},{-2,-1}};
int vis[310][310]; int BFS()
{
queue<node> que;
memset(vis,0,sizeof vis);
que.push(node(x1,y1,0));
vis[x1][y1]=1;
while(!que.empty())
{
node temp=que.front();
for(int i=0;i<8;i++)
{
int x0=temp.x+go[i][0];
int y0=temp.y+go[i][1];
int sum0=temp.sum+1;
if(x0>=0&&x0<n&&y0>=0&&y0<n)
{
if(x0==x2&&y0==y2) return sum0;
else if(!vis[x0][y0])
{
que.push(node(x0,y0,sum0));
vis[x0][y0]=1;
}
}
}
que.pop();
} return 0; } int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d",&n,&x1,&y1,&x2,&y2); if(x1==x2&&y1==y2)
printf("0\n");
else
printf("%d\n",BFS());
} return 0; }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 1915 Knight Moves(BFS+STL)的更多相关文章

  1. POJ 1915 Knight Moves

    POJ 1915 Knight Moves Knight Moves   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29 ...

  2. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  3. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  4. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

  5. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

  6. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  7. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  8. HDU1372:Knight Moves(BFS)

    Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  9. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...

随机推荐

  1. JavaScript 基础优化(读书笔记)

    1.带有 src 属性的<script>元素不应该在其<script>和</script>标签之间再包含额外的 JavaScript 代码.如果包含了嵌入的代码,则 ...

  2. HDU 5009 Paint Pearls (动态规划)

    Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...

  3. HADOOP2.6

    LINUX下HADOOP2.6.0集群环境的搭建 本文旨在提供最基本的,可以用于在生产环境进行Hadoop.HDFS分布式环境的搭建,对自己是个总结和整理,也能方便新人学习使用. 基础环境 JDK的安 ...

  4. 基于CefGlue的桌面应用开发

    原文地址:http://johnnyfee.github.io/csharp/2013/12/21/cef-glue/ 前言 如果你想使用WEB技术来开发桌面客户端,并且是想使用的语言也是C#时,那请 ...

  5. 利用objc的runtime来定位次线程中unrecognized selector sent to instance的问题

    昨天遇到一个仅仅有一行错误信息的问题: -[NSNull objectForKey:]: unrecognized selector sent to instance 0x537e068 因为这个问题 ...

  6. linux--档案权限与目录配置

    下面是最近学习档案权限与目录配置的一些知识点总结***博客园-邦邦酱好*** Linux最优秀的地方之一,就在于他的多人多任务环境.而为了让各个使用者具有较保密的档案数据,因此档案的权限管理就变的很重 ...

  7. awk使用的实例

    1.使用split函数 name.url的内容: 上海    http://trip.elong.com/shanghai/jingdian elong   destination 云南    htt ...

  8. JS CSS 网页 简单 右侧 悬浮

    <!--右侧效果--> <script> $().ready(function() { $(".orm").hover(function() { $(thi ...

  9. HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram

    HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram ...

  10. Android-Universal-Image-Loader学习笔记(一个)

    Android-Universal-Image-Loader它是一个开源项目,处理图像加载和缓存.闲暇的时候,读一些源.特别记录. 所述图像文件(磁盘)高速缓存,我们需要考虑的因素,如以下 1)  定 ...