Description
Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him canmove 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 neededfor a knight to reach one point from another, so that you have the chance to befaster than Somurolov.
For people not familiar with chess, the possible knight moves are shown inFigure 1.
Input
The input begins with thenumber n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containinginteger numbers. The first line specifies the length l of a side of the chessboard (4 <= l <= 300). The entire board has size l * l. The second andthird line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying thestarting and ending position of the knight on the board. The integers areseparated by a single blank. You can assume that the positions are validpositions on the chess board of that scenario.
Output
For each scenario of the inputyou have to calculate the minimal amount of knight moves which are necessary tomove from the starting point to the ending point. If starting point and endingpoint are equal,distance is zero. The distance must be written on a singleline.
Sample Input
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
Sample Output
5
28
0
Source
TUDProgramming Contest 2001, Darmstadt, Germany
题目的大意:
就像中国象棋一样,骑士(类似于马的走法,如图所示)在棋盘上走,要求输入棋盘的大小,出发点的坐标以及目的地的坐标,要求输出从出发点到目的地所花费的最小的步数。
分析:
其实从图中可以看出,对于每一点我们有8种选择,从起点开始,每一步相当于分别走了8步,然后接下来64部,64*8不断的遍历;当然还要判断边界的情况,还要判断该点是否已经走过。。总体上来讲,这道题不难哈。。。
代码如下:
package ACM;
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
private int[][] g;
private static int[][] go = {{2,1},{-2,1},{2,-1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};
public Main(int n) {
g = new int[n][n];
for (int i = 0; i < g.length; i++) {
Arrays.fill(g[i], 0);
}
}
public int getMinTime(Node startnode,Node endnode) {
//处于效率考虑;
if(startnode.equals(endnode)){
return 0;
}
int count = 0;
LinkedList<Node> llisttemp = new LinkedList<Node>();
llisttemp.add(startnode);
Node nodetemp;
Node newNode;
while(llisttemp.size()>0){
nodetemp = llisttemp.remove();
for(int i=0;i<go.length;i++){
newNode = new Node(nodetemp.x +go[i][0],nodetemp.y +go[i][1]);
if(newNode.isLegal(g.length)){
if(g[newNode.x][newNode.y] != 0){
continue;
}
llisttemp.add(newNode);
g[newNode.x][newNode.y] = g[nodetemp.x][nodetemp.y]+1;
}
if(newNode.equals(endnode)){
return g[newNode.x][newNode.y];
}
}
}
return -1;
}
public static void main(String[] args) {
int n;
int QPsize;
Node startnode;
Node endnode;
Main ma;
// TODO Auto-generated method stub
Scanner cin = new Scanner(new BufferedInputStream(System.in));
n = cin.nextInt();
for (int i = 0; i < n; i++) {
QPsize = cin.nextInt();
ma = new Main(QPsize);
startnode = new Node(cin.nextInt(),cin.nextInt());
endnode = new Node(cin.nextInt(),cin.nextInt());
System.out.println(ma.getMinTime(startnode, endnode));
}
}
static class Node{
public int x;
public int y;
public int count = 0 ;
public Node(int x,int y){
this.x =x;
this.y = y;
}
public boolean equals(Node n){
if(this.x == n.x && this.y == n.y){
return true;
}
return false;
}
public boolean isLegal(int len){
if(x >= len || y>=len ||x<0||y<0){
return false;
}
return true;
}
}
}