OpenJudge/Poj 1915 Knight Moves

时间:2022-09-26 11:34:49

1.链接地址:

http://bailian.openjudge.cn/practice/1915

http://poj.org/problem?id=1915

2.题目:

总Time Limit:
1000ms
Memory Limit:
65536kB
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.
OpenJudge/Poj 1915 Knight Moves
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
Source
TUD Programming Contest 2001, Darmstadt, Germany

3.思路:

4.代码:

 #include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
typedef struct
{
int row;
int col;
//int step;
}data;
int a[][];
int colStep[]={,,-,-,,,-,-};
int rowStep[]={,,-,-,-,-,,};
bool in(int row,int col,int size)
{
if(row<||row>=size) return false;
if(col<||col>=size) return false;
return true;
}
void initArray(int size)
{
int i,j;
for(i=;i<size;i++)
{
for(j=;j<size;j++)
{
a[i][j]=-;
}
}
}
int f(int i,int j,int m,int n,int size)
{
queue<data> q;
int k;
int newRow,newCol;
data start,aData;
start.row=i;
start.col=j;
initArray(size);
//start.step=1;
a[i][j]=;
q.push(start);
while(!q.empty())
{
aData=q.front();
if(aData.row==m&&aData.col==n)
{
return a[aData.row][aData.col];
}
else
{
for(k=;k<;k++)
{
newRow=aData.row+rowStep[k];
newCol=aData.col+colStep[k];
if(in(newRow,newCol,size))
{
if(a[newRow][newCol]==-)
{
data newData;
newData.col=newCol;
newData.row=newRow;
a[newRow][newCol]=a[aData.row][aData.col]+;
q.push(newData);
}
}
}
}
q.pop();
}
return ;
}
int main()
{
int sum,k;
int i,j,m,n,size;
cin>>sum;
for(k=;k<sum;k++)
{
cin>>size>>i>>j>>m>>n;
cout<<f(i,j,m,n,size)<<endl;
}
return ;
}

OpenJudge/Poj 1915 Knight Moves的更多相关文章

  1. POJ 1915 Knight Moves

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

  2. POJ 1915 Knight Moves&lpar;BFS&plus;STL&rpar;

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

  3. POJ 2243 Knight Moves&lpar;BFS&rpar;

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

  4. POJ 2243 Knight Moves

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

  5. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  6. POJ Knight Moves 2243 x

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13974   Accepted: 7797 Des ...

  7. POJ---2243 Knight Moves 使用A&ast;算法的广度优先搜索

    题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...

  8. 917&colon;Knight Moves

    题目链接:http://noi.openjudge.cn/ch0205/917/ 原题应该是hdu 1372 总时间限制: 1000ms  内存限制: 65536kB 描述 BackgroundMr ...

  9. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

随机推荐

  1. Android系统启动顺序

    Android是一个基于Linux的开源操作系统.x86(x86是一系列的基于intel 8086 CPU的计算机微处理器指令集架构)是linux内核部署最常见的系统.然而,所有的Android设备都 ...

  2. 用c&num;读取并分析sql2005日志

    用过logExplorer的朋友都会被他强悍的功能吸引,我写过一篇详细的操作文档可以参考http://blog.csdn.net/jinjazz/archive/2008/05/19/2459692. ...

  3. 水leetcode 爬楼梯

    public class Solution { public int climbStairs(int n) { if(n==1) return 1; if(n==2) return 2; int pr ...

  4. Linux grep 命令中的正则表达式详解

    在 Linux .类 Unix 系统中我该如何使用 Grep 命令的正则表达式呢? Linux 附带有 GNU grep 命令工具,它支持扩展正则表达式(extended regular expres ...

  5. 常用类:String,StringBuffer,StringBuilder

    String String类被final修饰符修饰,所以不能将其进行继承,所有对它的改变都要重新创建一个新的地址 1.String的构造器 String() String(byte []bytes)/ ...

  6. ASP&period;NET Core中的OWASP Top 10 十大风险-失效的访问控制与Session管理

    不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: https://dotnetcoretutorials.com/201 ...

  7. HDU 4549 M斐波那契数列(矩阵快速幂)

    题目链接:M斐波那契数列 题意:$F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]$.给定$a,b,n$,求$F[n]$. 题解:暴力打表后发现$ F[n]=a^{fib(n-1)} ...

  8. webpack打包遇到过的问题

    1.打包后html文件打开是空白页面,报错信息如图所示: 解决办法:这里主要是将assetsPublicPath的路径从'/'改为'./'就好了. ('/'表示根目录:'./'表示当前目录) 2.运行 ...

  9. 理解活在Iphone中的那些App (四)

    App生存环境之宿主环境 终于开始说一些技术性的话题了,从这里开始的一些技术细节的东西,以前我也没有太刻意的注意过.为了写这个也是刚刚看了一点资料,如果有纰漏,恳请指出. 一个App生存的宿主环境主要 ...

  10. XMPP客户端

    1. Strophe.js 2. Converse.js