骑士走棋盘 java

对于骑士走棋盘,算法的讨论以及比较多了,本文就是简单的使用经典的走难走的方向的算法。

对于一个位置,有八个方向的走法


我们对于每个位置都去试探其还有几个能走的方向,然后选择最少的方向去走,一旦没有走的方向,就退回来。

	static int M = 8;
	static int N = 8;
	static int[][] aaa = new int[M][N];
	static int x[] = { -2, -2, -1, 1, 2, 2, 1, -1 };
	static int y[] = { -1, 1, 2, 2, 1, -1, -2, -2 };

棋盘用一个8*8的二维矩阵表示,方向为两个对应数组。


	static int getCount(int i, int j) {
		int count = 0;
		for (int m = 0; m < 8; m++) {
			int nowX = i + x[m];
			int nowY = j + y[m];
			if (nowX >= 0 && nowX <= M - 1 && nowY >= 0 && nowY <= N - 1
					&& aaa[nowX][nowY] == 0) {
				count++;
			}
		}
		if (count == 0) {
			count = 99;
		}
		return count;
	}

通过getCount()函数知道对应八个位置能走的方向数。

而对于不能走的方向,我设定其值为99,方便在后面剔除。


然后对于不同方向根据方向数排序

	static int[] sort(int[] a) {
		int[][] b = new int[8][2];
		int[] count = new int[8];
		for (int i = 0; i < 8; i++) {
			b[i][0] = a[i];
			b[i][1] = i;
		}
		for (int m = 0; m < 7; m++) {
			for (int n = 0; n < 7 - m; n++) {
				if (b[n][0] >= b[n + 1][0]) {
					int temp1 = 0;
					int temp2 = 0;
					temp1 = b[n][0];
					temp2 = b[n][1];
					b[n][0] = b[n + 1][0];
					b[n][1] = b[n + 1][1];
					b[n + 1][0] = temp1;
					b[n + 1][1] = temp2;
				}
			}
		}
		for (int i = 0; i < 8; i++) {
			count[i] = b[i][1];
			if (b[i][0] == 99) {
				count[i] = 99;
			}
			// System.out.print(b[i][1] + " ");
		}
		return count;
	}

这样我们就可以知道不同方向下一步能走的方向的个数进行的排序,count数组为方向号的排序。

static int go(int i, int j, int index) {
		aaa[i][j] = index;
		int[] count = new int[8];
		int[] count_i = new int[8];
		if (index == M * N) {
			show();
			return 1;
		}
		for (int m = 0; m < 8; m++) {
			int nowX = i + x[m];
			int nowY = j + y[m];
			if (nowX >= 0 && nowX <= M - 1 && nowY >= 0 && nowY <= N - 1
					&& aaa[nowX][nowY] == 0) {
				// show();
				count[m] = getCount(nowX, nowY);
				if (index == N * M - 1) {
					go(nowX, nowY, index + 1);
					return 1;
				}
			} else {
				count[m] = 99;
			}
		}
		showInt(count);
		count_i = sort(count);
		showInt(count_i);
		for (int i1 = 0; i1 < 8; i1++) {
			if (count_i[i1] == 99) {
				break;
			}
			int nowX = i + x[count_i[i1]];
			int nowY = j + y[count_i[i1]];
			if (nowX >= 0 && nowX <= M - 1 && nowY >= 0 && nowY <= N - 1
					&& aaa[nowX][nowY] == 0) {
				if (go(nowX, nowY, index + 1) == 1) {
					return 1;
				}
			}
		}
		aaa[i][j] = 0;
		return 0;
	}

travel为移动函数

package szy;

import java.util.concurrent.CountDownLatch;

import javax.xml.transform.Templates;

/*
 * 加上了经典的算法
 * 下一步先走出路较少的地方
 * 
 * 
 */
public class Qishi2 {
	static int M = 5;
	static int N = 5;
	static int[][] aaa = new int[M][N];
	static int x[] = { -2, -2, -1, 1, 2, 2, 1, -1 };
	static int y[] = { -1, 1, 2, 2, 1, -1, -2, -2 };
	static int zz[] = { 1, 2, 4, 3, 5, 6, 8, 7 };

	public static void main(String[] args) {
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				aaa[i][j] = 0;
			}
		}
		show();
		// System.out.println(getCount(4, 4));
		go(0, 0, 1);
	}

	static int go(int i, int j, int index) {
		aaa[i][j] = index;
		int[] count = new int[8];
		int[] count_i = new int[8];
		if (index == M * N) {
			show();
			return 1;
		}
		for (int m = 0; m < M; m++) {
			int nowX = i + x[m];
			int nowY = j + y[m];
			if (nowX >= 0 && nowX <= M - 1 && nowY >= 0 && nowY <= N - 1
					&& aaa[nowX][nowY] == 0) {
				// show();
				count[m] = getCount(nowX, nowY);
				if (index == N * M - 1) {
					go(nowX, nowY, index + 1);
					return 1;
				}
			} else {
				count[m] = 99;
			}
		}
		showInt(count);
		count_i = sort(count);
		showInt(count_i);
		for (int i1 = 0; i1 < 8; i1++) {
			if (count_i[i1] == 99) {
				break;
			}
			int nowX = i + x[count_i[i1]];
			int nowY = j + y[count_i[i1]];
			if (nowX >= 0 && nowX <= M - 1 && nowY >= 0 && nowY <= N - 1
					&& aaa[nowX][nowY] == 0) {
				if (go(nowX, nowY, index + 1) == 1) {
					return 1;
				}
			}
		}
		aaa[i][j] = 0;
		return 0;
	}

	static void show() {
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				System.out.print(aaa[i][j] + " ");
			}
			System.out.println();
		}
	}

	static int getCount(int i, int j) {
		int count = 0;
		for (int m = 0; m < 8; m++) {
			int nowX = i + x[m];
			int nowY = j + y[m];
			if (nowX >= 0 && nowX <= M - 1 && nowY >= 0 && nowY <= N - 1
					&& aaa[nowX][nowY] == 0) {
				count++;
			}
		}
		if (count == 0) {
			count = 99;
		}
		return count;
	}

	static int[] sort(int[] a) {
		int[][] b = new int[8][2];
		int[] count = new int[8];
		for (int i = 0; i < 8; i++) {
			b[i][0] = a[i];
			b[i][1] = i;
		}
		for (int m = 0; m < 7; m++) {
			for (int n = 0; n < 7 - m; n++) {
				if (b[n][0] >= b[n + 1][0]) {
					int temp1 = 0;
					int temp2 = 0;
					temp1 = b[n][0];
					temp2 = b[n][1];
					b[n][0] = b[n + 1][0];
					b[n][1] = b[n + 1][1];
					b[n + 1][0] = temp1;
					b[n + 1][1] = temp2;
				}
			}
		}
		for (int i = 0; i < 8; i++) {
			count[i] = b[i][1];
			if (b[i][0] == 99) {
				count[i] = 99;
			}
			// System.out.print(b[i][1] + " ");
		}
		return count;
	}

	static void showInt(int[] a) {
		for (int i = 0; i < a.length; i++) {
			// System.out.print(a[i] + " ");
		}
		// System.out.println();
		// System.out.println();
	}

}

可能写的不好,只能算完成功能了 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值