Knight Moves
Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the “difficult” part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying “To get from xx to yy takes n knight moves.”.
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
题目大意:给你一个起点,和一个终点,问骑士从起点终点最少走多少步?
国际象棋图解
首先 BFS 一定是可以做的,但有一种更有效的方法A*,A*=bfs+贪心
A*详情见罗勇军老师的博客,说的很详细。
我觉得这个题需要注意的点:
(1)g和h应该用同样的计算方法。例如h是曼哈顿距离,g也应该是曼哈顿距离。如 果计算方法不同,f = g + h就没有意义了。
(2)h(x) 的曼哈顿距离为当前点到终点的曼哈顿距离,而当前节点的g(x)为上一个点g(x)+23(根号5 向上取整 再乘10)。
(3)优先队列的排列顺序依据为 启发式的代价函数F(x) = g(x) +h(x)。
(4)关于优先队列的的创建 参考Oracle官网 .PriorityQueue(int initialCapacity, Comparator<? super E> comparator) 需要指定比较容器的大小,不指定的话,只给出Comparator比较器虽然没有这个方法eclipse也是可以运行出结果的,但是Poj会报Compile Error
(5)比较器Comparator 的compare 方法 public int compare(num o1, num o2);o1.f>o2.f return 1, 表示需要修改,使得o1.f<o2.f 。队列按num.f从小到大的顺序排序。
package lanqiaobei7;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class bfs_chess {
static boolean[][] aa;
//static int[] r = new int[1005];
static int[] dirx = {-2,-2,-1,-1,1,1,2,2};
static int[] diry = {1,-1,2,-2,2,-2,1,-1};
static PriorityQueue<num> q;
static int h,g,endx,endy,startx,starty;
static num num1;
static char[] c;
static String s,ss;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
s = sc.next();
c = s.toCharArray();
int startx = c[1]-48;
int starty = c[0]-96;
ss = sc.next();
c = ss.toCharArray();
endx = c[1]-48;
endy = c[0]-96;
h = Math.abs(startx-endx)+Math.abs(starty-endy);
//q = new LinkedList();
q= new PriorityQueue<num>(1,new Comparator<num>() {
@Override
public int compare(num o1, num o2) {
if(o1.f>o2.f) {
return 1;
}else if(o1.f<o2.f) {
return -1;
}else {
return 0;
}
//return o1.f>o2.f?1:-1;
}
});
q.add(new num(startx,starty,0,0,h*10));
aa = new boolean[9][9];
aa[startx][starty] = true;
BFS();
}
}
private static void BFS() {
int front=1;
while(q.size()>0) {
num1 = q.poll();
front = num1.step+1;
if(num1.x==endx&&num1.y==endy) {
System.out.println("To get from "+s+" to "+ss+" takes "+num1.step+" knight moves.");
break;
}else {
for (int i = 0; i <dirx.length; i++) {
int newx = num1.x+dirx[i];
int newy = num1.y+diry[i];
g = num1.g+23;
h = Math.abs(newx-endx)+Math.abs(newy-endy);
if(judge(newx,newy)) {
q.add(new num(newx,newy,front,g,h*10));
}
}
}
}
}
private static boolean judge(int newx,int newy) {
//&&(Math.abs(newx-endx)+Math.abs(newy-endy))<=h
if(newx>=1&&newx<=8&&newy>=1&&newy<=8&&!aa[newx][newy]) {
aa[newx][newy]=true;
return true;
}
return false;
}
static class num{
private int x;
private int y;
private int step;
private int g;
private int f;
public num(int x, int y,int step,int g,int f) {
super();
this.x = x;
this.y = y;
this.step = step;
this.g=g;
this.f=f+g;
}
}
}