917:Knight Moves

简介: 题目链接:http://noi.openjudge.cn/ch0205/917/原题应该是hdu 1372总时间限制: 1000ms  内存限制: 65536kB描述BackgroundMr Somurolov, fabulous chess-gamer indeed, asserts ...

题目链接:http://noi.openjudge.cn/ch0205/917/

原题应该是hdu 1372

总时间限制: 1000ms  内存限制: 65536kB
描述
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.

输入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.输出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.样例输入

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

样例输出

5
28
0

来源TUD Programming Contest 2001, Darmstadt, Germany

题目大意:

输入n表示有一个n*n的棋盘,输入开始坐标和结束坐标,问一个骑士朝着棋盘的8个方向走马字步,从起点到终点最少需要多少步。

首先输入T表示有T个测试样例,然后输入n表示棋盘规模,然后输出起点和终点的坐标(坐标从0开始到n-1)

输出马移动的最少步数,起点和终点相同则输出0.

算法分析:

这个题明显用广搜效率比较高。深搜的话,要搜索完所有路径然后才知道最优解。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 struct obj
 8 {
 9     int xx,yy,step;
10 };
11 
12 int T,n;
13 queue<struct obj> q;
14 struct obj S,E;
15 int used[303][303];
16 
17 int dx[8]={-2,-1,1,2,2,1,-1,-2};//???????????????????8???? 
18 int dy[8]={1,2,2,1,-1,-2,-2,-1};
19 void BFS();
20 
21 int main(int argc, char *argv[])
22 {
23     freopen("917.in","r",stdin);
24     scanf("%d",&T);
25     while(T)
26     {
27         T--;
28         scanf("%d",&n);
29         scanf("%d%d%d%d",&S.xx,&S.yy,&E.xx,&E.yy);
30         S.step=0;
31         E.step=-1;
32         
33         if(S.xx==E.xx&&S.yy==E.yy) printf("0\n");
34         else 
35         {
36             memset(used,0,sizeof(used));
37             BFS();
38             if(E.step==-1) printf("no way!\n");
39             else printf("%d\n",E.step);
40         }
41     }
42     return 0;
43 }
44 
45 void BFS()
46 {
47     int i,txx,tyy;
48     struct obj temp;
49     
50     while(!q.empty()) q.pop();
51     
52     used[S.xx][S.yy]=1;
53     q.push(S);
54     while(!q.empty())
55     {
56         for(i=0;i<8;i++)
57         {
58             txx=q.front().xx+dx[i];
59             tyy=q.front().yy+dy[i];
60             if(txx>=0&&txx<n&&tyy>=0&&tyy<n&&used[txx][tyy]==0)
61             {
62                 temp.xx=txx;
63                 temp.yy=tyy;
64                 temp.step=q.front().step+1;
65                 q.push(temp);
66                 used[txx][tyy]=1;
67                 if(temp.xx==E.xx&&temp.yy==E.yy)
68                 {
69                     E.step=temp.step;
70                     return;
71                 }
72             }
73         }
74         q.pop();
75     }
76 }

 

相关文章
|
7月前
Knight Moves(POJ2243)
Knight Moves(POJ2243)
|
机器学习/深度学习
1257:Knight Moves 2021-01-09
1257:Knight Moves 2021-01-09
UVA699 下落的树叶 The Falling Leaves
UVA699 下落的树叶 The Falling Leaves
UVA699 下落的树叶 The Falling Leaves
UVA439-骑士的移动 Knight Moves(BFS)
UVA439-骑士的移动 Knight Moves(BFS)
UVA439-骑士的移动 Knight Moves(BFS)
|
开发者
牛客第六场-Combination of Physics and Maths
题意:选出一个子矩阵,使得所求的压强最大,压强是指这个子矩阵中每个元素之和 / 这个子矩阵最下面一行的元素之和
61 0
牛客第六场-Combination of Physics and Maths
|
Go
HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
88 0