早教吧作业答案频道 -->其他-->
谁帮我编几道题啊用C++语言MrSomurolov,fabulouschess-gamerindeed,assertsthatnooneelsebuthimcanmoveknightsfromonepositiontoanothersofast.Canyoubeathim?TheProblemYourtaskistowriteaprogramtocalculatetheminimum
题目详情
谁帮我编几道题啊 用C++语言
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
我明天需要交 2分钟就ok了
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
我明天需要交 2分钟就ok了
▼优质解答
答案和解析
最容易想到的方法是bfs
#include
#include
using namespace std;
#define INGRID(x,y) (x>=0&&x=0&&y>size>>start.x>>start.y>>end.x>>end.y;
if(start.x==end.x&&start.y==end.y)
{
cout
#include
#include
using namespace std;
#define INGRID(x,y) (x>=0&&x=0&&y>size>>start.x>>start.y>>end.x>>end.y;
if(start.x==end.x&&start.y==end.y)
{
cout
看了谁帮我编几道题啊用C++语言M...的网友还看了以下:
正数的立方是什么数,负数的立方是什么数.谁<3次根号10<几,谁小于3次根号100<几 2020-04-11 …
A、B、C三个圆柱体,A的底面半径是B的12,是C的2倍;C的高是A的2倍,是B的4倍.(1)B的 2020-04-27 …
从生物学角度来看,下列古诗z表现为竞争关系的是()A.黄梅时节家家雨,青草池塘处处蛙B.种豆南山下 2020-05-17 …
人教版六年级数学96页第11题九十六页十一题急谁占谁的百分之几谁比谁多百分之几谁比谁少百分之几 2020-05-20 …
从小明家步行去奶奶家,爸爸需要15分钟,妈妈需要18分钟.两人每分钟个行全程的几分之几?从小明家步 2020-05-24 …
A、B、C三个圆柱体,A的底面半径是B的12,是C的2倍;C的高是A的2倍,是B的4倍.(1)B的 2020-06-07 …
A、B、C、D四人在争论今天是星期几:A说:明天是星期五.B说:明天是星期天.C说:你们俩说的都不 2020-08-03 …
按“谁的几分之几=谁”的格式写出关系式.1、一本书已经看了3分之1.2、一条绳子剪短后还剩8分之3. 2020-11-18 …
小孩刚上一年级20天,学习“比一比”这个,a比b多3,b比c多5,那么a比c多几个呢?怎么讲都不懂! 2020-12-27 …
工程队修一条4千米长的铁路,12天修完.平均每天修多少千米?平均每天修这条公路的几分之几?谁回答了谁 2020-12-29 …