package com.feng.testproject.algorithm;
import java.util.*;
public class L36 {
public static void main(String[] args) {
char[][] board = {{'.','8','7','6','5','4','3','2','1'},
{'2','.','.','.','.','.','.','.','.'},
{'3','.','.','.','.','.','.','.','.'},
{'4','.','.','.','.','.','.','.','.'},
{'5','.','.','.','.','.','.','.','.'},
{'6','.','.','.','.','.','.','.','.'},
{'7','.','.','.','.','.','.','.','.'},
{'8','.','.','.','.','.','.','.','.'},
{'9','.','.','.','.','.','.','.','.'}};
boolean validSudoku = isValidSudoku(board);
System.out.println(validSudoku);
}
/**
* 思路:用空间换时间,定义每行每列每格每个3*3方格 还可以填写的数值
* 要定义变量太多,暂时放弃
* 暴力破解试试,回溯
*
* @param board
* @return
*/
public static boolean isValidSudoku(char[][] board) {
int length = board.length;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < length; j++) {
if (board[i][j] != '.') {
Set<Character> hang = new HashSet<>();
Set<Character> lie = new HashSet<>();
Set<Character> fangge = new HashSet<>();
for (int k = 0; k < length; k++) {
if (board[i][k] != '.') {
boolean add = hang.add(board[i][k]);
if (!add) {
return false;
}
}
if (board[k][j] != '.') {
boolean add = lie.add(board[k][j]);
if (!add) {
return false;
}
}
}
for (int k = i/3*3; k < i/3*3+3; k++) {
for (int l = j/3*3; l < j/3*3+3; l++) {
if (board[k][l] != '.') {
boolean add = fangge.add(board[k][l]);
if (!add) {
return false;
}
}
}
}
}
}
}
return true;
}
/**
* 思路:用空间换时间,定义每行每列每格每个3*3方格 还可以填写的数值
* 要定义变量太多,暂时放弃
* 暴力破解试试,回溯
*
* @param board
* @return
*/
public static boolean isValidSudokujieda(char[][] board) {
int length = board.length;
Table[][] table = new Table[length][length];
PriorityQueue<Table> queue = new PriorityQueue<>();
boolean flag = true;
while (flag) {
flag = false;
queue.clear();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < length; j++) {
if (board[i][j] != '.') {
char c = board[i][j];
table[i][j] = new Table(c);
} else {
Set<Character> all = getAll();
findValues(board, length, i, j, all);
if (all.size() == 0) {
sout(board, length);
return false;
} else if (all.size() == 1) {
board[i][j] = all.iterator().next();
flag = true;
} else {
table[i][j] = new Table(all);
queue.add(table[i][j]);
}
}
}
}
}
return true;
}
private static void sout(char[][] board, int length) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println("");
}
}
private static void findValues(char[][] board, int length, int i, int j, Set<Character> all) {
for (int k = 0; k < length; k++) {
if (board[i][k] != '.') {
all.remove(board[i][k]);
}
if (board[k][j] != '.') {
all.remove(board[k][j]);
}
}
for (int k = i/3*3; k < i/3*3+3; k++) {
for (int l = j/3*3; l < j/3*3+3; l++) {
if (board[k][l] != '.') {
all.remove(board[k][l]);
}
}
}
}
/**
* 存入九个数
* @return
*/
private static Set<Character> getAll() {
Set<Character> all = new HashSet<>();
all.add('1');
all.add('2');
all.add('3');
all.add('4');
all.add('5');
all.add('6');
all.add('7');
all.add('8');
all.add('9');
return all;
}
static class Table implements Comparable<Table>{
private char value;
private Set<Character> set = new HashSet<>();
private int length;
public Table(Set<Character> set) {
this.set = set;
this.length = set.size();
}
public Table(char value) {
this.value = value;
}
public void add(char c) {
set.add(c);
length = set.size();
}
public void remove(char c) {
set.remove(c);
length = set.size();
}
public char getValue() {
return value;
}
public void setValue(char value) {
this.value = value;
}
public Set<Character> getSet() {
return set;
}
public void setSet(Set<Character> set) {
this.set = set;
this.length = set.size();
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
@Override
public int compareTo(Table o) {
return this.getLength() - o.getLength();
}
}
}
总结
题目没看明白就开始做题,花了很长时间取消求解的问题,结果只需要判断题目给出的数独是否有效,这个问题需要注意。
刚开始在做求解数独时一直在想如何才能表示每一个待求解的位置可能的数字,给每一个位置定义一个set,结果变量名不好起也比较麻烦不好定位。
之后确定对象也可以定义成数组,建立内部类,用内部类方式表示每隔位置可能数字,才方便做
优先队列数据结构的使用也是一种成长,每次找可能数字最小的位置,以他为起点来试可能的值,是时间复杂度更小一点。
做题时采用了三层嵌套循环,时间复杂度还是很高的,相对于官方解答来说,换一种方式来求每一行每一列重复的数据,也是一个新的思路。
要熟悉数组这种数据结构。