Sudoku backtracking with one dimension array

11 篇文章 0 订阅
package hello.test;

import java.io.*;
import java.util.*;

public class Sudoku {

	private static final int N = 9;  // the size of the board
	private int[] board;  // the one dimension array that stores board data
	
	// constructor
	public Sudoku() {
		try {
			init();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// init
	private void init() throws FileNotFoundException {
		// TODO Auto-generated method stub
		board = new int[N * N];
	
		Scanner fin = new Scanner(new File("/home/will/myworld/workspace/HelloProject/src/sudokuin.txt"));
		int numCases = fin.nextInt();
		
		for (int c=1; c<=numCases; c++) {
			for(int i = 0; i < N * N; i++) {
				board[i] = fin.nextInt();
			}
		}
		
	}
	
	// solve the problem using backtracking method
	public void solve(int cell) {
		if (cell == N * N) { this.show(); return; }
		
		// skip over cell n since it has fixed value
		if (board[cell] != 0) { solve(cell + 1); return; }
		
		// try all 9 possibilities
		for (int n = 1; n <= N; n++) {
			if (isConsistent(cell, n)) {
				board[cell] = n;
				solve(cell + 1);
			}
		}
		board[cell] = 0; // clean up after itself
	}

	// if the cell puts number n, does it conflict in row, column and the small square
	private boolean isConsistent(int cell, int n) {
		//if (board[cell] != 0) return false;
		
		int R = cell / N;  // the row of the two dimensional array
		int C = cell % N;  // the column of the two dimensional array
		int r = R / 3;	   // the small 3 * 3 array's row
		int c = C / 3;     // the small 3 * 3 array's column
		
		// is the row consistent
		for(int i = 0; i < N; i++) {
			if(board[R * N + i] == n) return false;  // the row of the cell is equal to n
		}
		
		// is the column consistent
		for(int j = 0; j < N; j++) {
			if(board[j * N + C] == n) return false;  // the column of the cell is equal to n
		}
		
		// is the square consistent
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				// (3 * r + i) * N + 3 * c + j is the small square's position in one dimensional array
				if(board[(3 * r + i) * N + 3 * c + j] == n) return false;  
			}
		}
		
		// otherwise, the cell is consistent in row, column and square, return true
		return true;
	}

	// show board
	public void show() {
		for(int i = 1; i <= N * N; i++) {
			System.out.print(board[i - 1] + " ");
			if(i % N == 0) {
				System.out.println();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Sudoku s = new Sudoku();
		s.solve(0);
	}

}


Below is a test case:

 

1
0 8 0 4 0 2 0 6 0
0 3 4 0 0 0 9 1 0
9 6 0 0 0 0 0 8 4
0 0 0 2 1 6 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 3 5 7 0 0 0
8 4 0 0 0 0 0 7 5
0 2 6 0 0 0 1 3 0
0 9 0 7 0 1 0 4 0

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值