Knight | ||||||
|
||||||
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 |
原来,马是走日的、、原谅我的英语水平、、、。。
#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=