chapter05-Knight Moves(Poj 1915)

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;
		}
	}

}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章