目录
KNIGHT

** KNIGHT:** <Excerpt in index | 首页摘要>

<The rest of contents | 余下全文>

Knight
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 106(37 users) Total Accepted: 54(35 users) Rating: Special Judge: No
Description
The knight in the chessboard wonders the least number of moves to go to square t from square s. The knight has a unique L-shaped move: two squares in one direction either horizontally or vertically, and one square in another direction perpendicular to the first.

Help him to solve this problem!

Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases. Then T test cases follow.

For each test case:

Line 1. This line contains the chessboard coordinates of position s indicating the square the knight is currently in.

Line 2. This line contains the chessboard coordinates of position t indicating the square the knight wants to go to.

The coordinate consists of a lowercase letter from a to h and a digit from 1 to 8.

Output
For each test case:

Line 1. Output the least number of moves.

Sample Input
1

a1

a3

Sample Output
2

Source
哈理工2012春季校赛 - 现场赛
Author
齐达拉图@HRBUST

原来,马是走日的、、原谅我的英语水平、、、。。

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
int a[10][10];
int visit[10][10];
int direct[8][2]={{1,2},{2,1},{-1,2},{2,-1},{1,-2},{-2,1},{-1,-2},{-2,-1}};
int b[200],x;
int c[10]={0,8,7,6,5,4,3,2,1};
char y;
struct node
{
int x;
char y;
} p,q[1000];
int bfs()
{
p.x=x;
int j,i;
p.y=y;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
visit[i][j]=-1;
}
}
visit[c[x]][b[y]]=0;
int front=0,rear=0;
q[0]=p;
while(rear<=front)
{
p=q[rear++];
int x=p.x;
int y=p.y;
for(i=0;i<8;i++)
{

int x1=x+direct[i][0];
int y1=y+direct[i][1];
if(x1<=8&&x1>0&&y1>=97&&y1<=104&&visit[c[x1]][b[y1]]==-1)
{
if(a[c[x1]][b[y1]]==1)
{ visit[c[x1]][b[y1]]=visit[c[x]][b[y]]+1;
return visit[c[x]][b[y]]+1;
}
else
{
visit[c[x1]][b[y1]]=visit[c[x]][b[y]]+1;
q[++front].x=x1;
q[front].y=y1;
}
}

}
}
return -1;
}
int main()
{
int i,j;
int k=0;
for(i='a';i<='h';i++)
{
b[i]=k++;
}
int T;
while(~scanf("%d",&T))
{
while(T--)
{ getchar
(); memset
(a,0,sizeof(a));
char y1;
int x1; scanf
("%c%d",&y,&x); a
[c[x]][b[y]]=2; getchar
(); scanf
("%c%d",&y1,&x1);
if(x==x1&&y==y1)
{ cout
<<0<<endl;
continue;
} a
[c[x1]][b[y1]]=1; cout
<<bfs()<<endl;
/* for(i=1;i<=8;i++) { for(j=0;j<8;j++) { cout<<visit[i][j]<<" "; } cout<<endl; }*/







}
}
return 0;
}
文章作者: 爱笑的k11
文章链接: http://1315402725.github.io/posts/f4f16136/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 爱笑的k11
打赏
  • 微信
  • 支付寶

评论