数独算法实现(递归方式)

package com.sudoku;

public class sodu {
	private int[][] sodu = null;
	private int[] tai = null;

	/**
	 * 构造方法 因为每执行一次递归都会把数组tai元素全置为-1. 所以在执行一次递归后需要重新给tai赋值。
	 */
	public sodu() {

		do {
			this.init();

			tai = getMixArray();
			generateNum(0, 0, 0, 0); // upleft

			tai = getMixArray();
			generateNum(0, 3, 0, 3); // up

			tai = getMixArray();
			generateNum(0, 6, 0, 6); // upright

			tai = getMixArray();
			generateNum(3, 0, 3, 0); // left

			tai = getMixArray();
			generateNum(3, 6, 3, 6); // right

			tai = getMixArray();
			generateNum(6, 0, 6, 0); // downleft

			tai = getMixArray();
			generateNum(6, 3, 6, 3); // down

			tai = getMixArray();
			generateNum(6, 6, 6, 6); // downright
		} while (this.checkZERO());

	}

	/**
	 * 得到一个随机数组(元素1到9无重复)
	 * 
	 * @param array
	 */
	private int[] getMixArray() {
		int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		for (int i = 0; i < array.length; i++) {
			int rand = (int) (Math.random() * 9);
			int middle = array[i];
			array[i] = array[rand];
			array[rand] = middle;
		}
		return array;
	}

	/**
	 * 初始化数独数组 会初始化正中间的3*3矩阵
	 */
	private void init() {
		sodu = new int[9][9];

		int[] array = this.getMixArray();
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				sodu[3 + i][3 + j] = array[i * 3 + j];
			}
		}
	}

	/**
	 * 打印
	 */
	public void print() {
		String flag = "~";
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				System.out.print("[" + sodu[i][j] + "]");
				if (j % 3 == 2 && j < 8) {
					System.out.print(flag);
				}
			}
			System.out.println();
			if (i % 3 == 2 && i < 8) {
				for (int k = 0; k < 14; k++) {
					System.out.print(" ");
					System.out.print(flag);
				}
				System.out.println();
			}

		}
	}

	/**
	 * 递归方法
	 * 
	 * 生成(row,erect)处的值
	 * 
	 * xi和xj 分别是初始调用该方法时的row和erect
	 * 
	 * @return
	 */
	private boolean generateNum(int row, int erect, int xi, int xj) {
		if (row - xi == 3)
			return true;
		for (int i = 0; i < tai.length; i++) {
			if (tai[i] != -1 && isGood(row, erect, tai[i])) {
				sodu[row][erect] = tai[i];
				tai[i] = -1;
				if (erect - xj < 2) {
					if (generateNum(row, erect + 1, xi, xj)) {
						return true;
					} else {
						tai[i] = sodu[row][erect];
						sodu[row][erect] = 0;
						continue;
					}
				} else {
					if (generateNum(row + 1, xj, xi, xj)) {
						return true;
					} else {
						tai[i] = sodu[row][erect];
						sodu[row][erect] = 0;
						continue;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 判断数字number是否可以用在坐标(x,y)处
	 */
	private boolean isGood(int i, int j, int number) {
		for (int m = 0; m < 9; m++) {
			if (sodu[i][m] == number) {
				return false;
			}
		}
		for (int n = 0; n < 9; n++) {
			if (sodu[n][j] == number) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断数独数组中是否有0
	 */
	private boolean checkZERO() {
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				if (sodu[i][j] == 0) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * 程序入口
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new sodu().print();
	}
}

你可能感兴趣的:(算法)