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.
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.代码:

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<queue>
 4 using namespace std;
 5 typedef struct
 6 {
 7     int row;
 8     int col;
 9     //int step;
10 }data;
11 int a[300][300];
12 int colStep[8]={1,2,-1,-2,1,2,-1,-2};
13 int rowStep[8]={2,1,-2,-1,-2,-1,2,1};
14 bool in(int row,int col,int size)
15 {
16     if(row<0||row>=size) return false;
17     if(col<0||col>=size) return false;
18     return true;
19 }
20 void initArray(int size)
21 {
22     int i,j;
23     for(i=0;i<size;i++)
24     {
25         for(j=0;j<size;j++)
26         {
27             a[i][j]=-1;
28         }
29     }
30 }
31 int f(int i,int j,int m,int n,int size)
32 {
33     queue<data> q;
34     int k;
35     int newRow,newCol;
36     data start,aData;
37     start.row=i;
38     start.col=j;
39     initArray(size);
40     //start.step=1;
41     a[i][j]=0;
42     q.push(start);
43     while(!q.empty())
44     {
45         aData=q.front();
46         if(aData.row==m&&aData.col==n)
47         {
48             return a[aData.row][aData.col];
49         }
50         else
51         {
52             for(k=0;k<8;k++)
53             {
54                 newRow=aData.row+rowStep[k];
55                 newCol=aData.col+colStep[k];
56                 if(in(newRow,newCol,size))
57                 {
58                     if(a[newRow][newCol]==-1)
59                     {
60                         data newData;
61                         newData.col=newCol;
62                         newData.row=newRow;
63                         a[newRow][newCol]=a[aData.row][aData.col]+1;
64                         q.push(newData);
65                     }
66                 }
67             }
68         }
69         q.pop();
70     }
71     return 0;
72 }
73 int main()
74 {
75     int sum,k;
76     int i,j,m,n,size;
77     cin>>sum;
78     for(k=0;k<sum;k++)
79     {
80         cin>>size>>i>>j>>m>>n;
81         cout<<f(i,j,m,n,size)<<endl;
82     }
83     return 1;
84 }

转载于:https://www.cnblogs.com/mobileliker/p/3574303.html

OpenJudge/Poj 1915 Knight Moves相关推荐

  1. POJ 1915 Knight Moves

    Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29822 Accepted: 14013 Descri ...

  2. poj - 2243 Knight Moves

    这题和poj 1915一样,用bfs做走马步.现在再看当时的代码,真是好幼稚啊. 1 #include <stdio.h> 2 #include <string.h> 3 in ...

  3. POJ 2243:Knight Moves(双向BFS)

    http://poj.org/problem?id=2243 问题概述:一个8*8的棋盘,给定一个起点(列a-h,行1-8)和一个终点(列a-h,行1-8),按骑士的走法(走日字),从起点到 终点最少 ...

  4. 信息学奥赛一本通 1257:Knight Moves | OpenJudge NOI 2.5 917:Knight Moves

    [题目链接] ybt 1257:Knight Moves OpenJudge NOI 2.5 917:Knight Moves [题目考点] 1. 广搜 迷宫问题 [解题思路] 广搜,迷宫问题. 不同 ...

  5. POJ2243 Knight Moves —— A*算法

    题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  6. 英语题目翻译—— 917:Knight Moves

    题目:917:Knight Moves(OpenJudge - 917:Knight Moves) 翻译: 背景 传说中的象棋玩家Somurolov先生断言,除了他,没有人能像他一样迅速地将骑士从一个 ...

  7. 1 Knight Moves

    Problem F- Knight Moves 题目来源:https://vjudge.net/contest/207868#problem/F Problem description: 题意概括:中 ...

  8. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  9. hdu1372 Knight Moves BFS 搜索

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

最新文章

  1. 四层和七层负载均衡的区别
  2. 你知道“啥是佩奇”,却不一定了解佩奇排名算法
  3. PIC单片机入门_汇编/混编/C编比较
  4. C++线程池原理及创建(转)
  5. bzoj4443:[Scoi2015]小凸玩矩阵
  6. python数据结构算法 北京大学_北京大学公开课《数据结构与算法Python版》
  7. 搜索用计算机弹奏9277的数字,计算机基础知识参考试题(含答案)
  8. c语言中的switch语句中的break和continue的作用
  9. python调用opencv的速度_OpenCV-Python系列之OpenCV性能衡量与优化方法
  10. SAP License:未清项启用
  11. python new 干什么用_详解Python中的__new__()方法的使用
  12. Linux安装MySQL提示缺少libaio.so.1包问题
  13. SpringBoot中调用第三方接口的三种方式
  14. IMDB TOP250 更新于2015.3
  15. 清华学霸教你1小时入门 Python 爬虫,别说学长没帮你
  16. keil uvision5 cannot write project file 和 cannot read project file 解决建议
  17. 机器学习 深度学习技术区别_体育技术机器学习金钱和灵感的圣杯
  18. java 气泡_JAVA实现聊天气泡
  19. BIM+9大技术,你知多少?
  20. 区块链+慈善究竟帮助过谁?

热门文章

  1. 协同过滤算法评测python_元学习和推荐系统:协同过滤算法选择问题的文献综述和实证研究...
  2. android+5.0+小米手环,小米手环5和荣耀手环6哪个好-参数对比
  3. rf调用的python函数报错_Robot Framework(15)- 扩展关键字
  4. matlab向量的角标,【MATLAB】利用冒号表达式获得子矩阵
  5. mysql privileges_[转]mysql privileges
  6. c char*转int_C语言中的char类型也有signed和unsigned?字符也有正负之分吗?
  7. 如何使用计算机改进生产线,第四章 计算机生产管理.doc
  8. c语言 常量字符串数组,C语言常量以及字符串数组
  9. django下创建APP
  10. 动态规划之最长公共子串