马踏棋盘

0 篇文章 0 订阅

题目描述:中国象棋棋盘里,“马”的跳法为“斜日”,如图所示,有一个8X8的棋盘,按照要求编程

分析:8X8的棋盘,如果按照图中那样跳的是横竖线的交叉地方,那么实际上有9X9个位置



1.  a)判定“马”是否能够通过若干步从点P(x,y)跳到点Q(m,n);

     b)如果可达,输出一种“马”从P跳到Q经历的所有点

用递归实现查找路径,递归后,输出为反过来输出,所以将起点终点交换一下,输出点的顺序就是原来起点到终点的顺序

import java.util.Scanner;
public class Main {
	static int fx[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
	static int fy[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
	static int a[][] = new int[9][9];// 存储棋盘上马的遍历路径
	static int x, y, m, n;
	static boolean find = false;
	public static void main(String[] args) {
		// 象棋,递归是倒过来输出的,所以将起点终点交换一下,输出的顺序就是起点到终点的顺序
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		x = scan.nextInt();
		y = scan.nextInt();
		m = scan.nextInt();
		n = scan.nextInt();
		findPath(m, n);
		if (!find)
			System.out.println("P无法到达Q");
	}
	public static void findPath(int m, int n) {
		if (m < 0 || n < 0 || m > 8 || n > 8 || a[m][n] == 1)
			return;
		a[m][n] = 1;
		if (m == x && n == y) {
			find = true;
			System.out.println(m + "," + n);
		} else {
			for (int i = 0; i < 8; i++) {
				if (find) {
					System.out.println(m + "," + n);
					return;
				}
				findPath(m + fx[i], n + fy[i]);
			}
		}
	}
}


2. a)判定“马”是否能够通过若干步从点P(x,y)跳到点Q(m,n);

    b)如果可达,输出“马”从P跳到Q的最短路径上经历的所有点

在第1题的基础上,加上贪心算法,假设当前点为C,每次都从与C到P方向(因为是起点终点交换,所以是往P的方向找)最相似那个方向走。

import java.util.Scanner;
public class Main {
	static int fx[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
	static int fy[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
	static int a[][] = new int[9][9];// 存储棋盘上马的遍历路径
	static int x, y, m, n;
	static boolean find = false;
	public static void main(String[] args) {
		// 象棋,递归是倒过来输出的,所以将起点终点交换一下,输出的顺序就是起点到终点的顺序
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		x = scan.nextInt();
		y = scan.nextInt();
		m = scan.nextInt();
		n = scan.nextInt();
		findPath(m, n);
		if (!find)
			System.out.println("P无法到达Q");
	}
	public static void findPath(int m, int n) {
		if (m < 0 || n < 0 || m > 8 || n > 8 || a[m][n] == 1)
			return;
		a[m][n] = 1;
		if (m == x && n == y) {
			find = true;
			System.out.println(m + "," + n);
		} else {
			if (!find) {
				int dir = direction(m, n);
				findPath(m + fx[dir], n + fy[dir]);
			}
			System.out.println(m + "," + n);
		}
	}
	public static int direction(int m, int n) { // 寻找最优方向
		int dir = -1;
		double max = -1;
		for (int i = 0; i < 8; i++) {
			if (m + fx[i] < 0 || n + fy[i] < 0 || m + fx[i] > 8 || n + fy[i] > 8 || a[m + fx[i]][n + fy[i]] == 1)
				continue;
			double tmp = (fx[i] * (x - m) + fy[i] * (y - n)) / (Math.pow(fx[i] * fx[i] + fy[i] * fy[i], 0.5)
					* Math.pow((x - m) * (x - m) + (y - n) * (y - n), 0.5));
			if (tmp > max) {
				max = tmp;
				dir = i;
			}
		}
		return dir;
	}
}


3. a)判定“马”是否能够从P点开始遍历整个棋盘(每个位置只经过一次);

    b)如果可以,输出“马”从P跳到Q的最短路径上经历的所有点



其实博主暂时还没有找到P到Q到达不到的例子,还有从P点无法遍历整个棋盘的例子,如果有,请告知一下, 谢谢。






  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值