kotlin 解數(shù)獨(dú),“容易”、“初級(jí)”均已解開,“高級(jí)”尚未測(cè)試,“高級(jí)+”沒解開
數(shù)獨(dú)鏈接:
https://www.sudoku-cn.com/
class NineTablePlay {
companion object {
val defaultNum = mutableListOf<Int>(
6, 1, 9, 3, 0, 0, 0, 0, 0,
0, 7, 0, 9, 0, 0, 0, 0, 5,
0, 0, 0, 4, 0, 0, 6, 1, 0,
0, 3, 0, 0, 0, 9, 5, 0, 0,
1, 0, 0, 7, 0, 0, 2, 0, 0,
7, 0, 0, 0, 8, 5, 0, 3, 0,
5, 2, 7, 0, 6, 0, 0, 0, 0,
0, 0, 0, 0, 9, 4, 0, 0, 7,
0, 0, 1, 0, 0, 0, 0, 2, 6
)
//key是值 ,value是index
var rowHashList = mutableListOf<HashMap<Int, Int>>()
var volHashList = mutableListOf<HashMap<Int, Int>>()
var centerHashList = mutableListOf<HashMap<Int, Int>>()
// key 是 index,value是 第幾個(gè)hashMap
var centerDefaultHashMap = HashMap<Int, Int>()
var possibleHashMap = HashMap<Int, MutableList<Int>>()
var impossibleHashMap = HashMap<Int, MutableList<Int>>()
@JvmStatic
fun main(args: Array<String>) {
printList()
//數(shù)據(jù)確定沒問題,下面開始推算
//1.算出每一位可能的數(shù)字
//2.算出每一位不肯能的數(shù)字
//3.做交集
//4.判斷結(jié)果長(zhǎng)度是否為1
//5.不斷循環(huán),直至默認(rèn)數(shù)組都不為0或者循環(huán)了10000次也沒得到結(jié)果
var i = 10000
while (defaultNum.contains(0) && i > 1) {
i--
rowHashList.clear()
volHashList.clear()
centerHashList.clear()
centerDefaultHashMap.clear()
possibleHashMap.clear()
impossibleHashMap.clear()
//每一行都有1-9
addRow()
//每一列都有1-9
addVol()
//每3*3 都有1-9
addCenter()
setImpossibleList()
// 得到第一步的 可能結(jié)果
removeImpossible()
// 將可能的結(jié)果 與每行、每列、每9方格做并集
checkImpossible()
}
print("\n\n\n======**********=========")
printList()
}
/**
* 假設(shè)可能是3、6,那就檢查 每一行里面是不是除了它,其他的都不可能為3、6
*/
private fun checkImpossible() {
doCheckImpossible(rowHashList)
doCheckImpossible(volHashList)
doCheckImpossible(centerHashList)
}
private fun doCheckImpossible(hashList: MutableList<HashMap<Int, Int>>) {
for ((i, list) in hashList.withIndex()) {
list.forEach { t, u ->
var pList = possibleHashMap.get(t)
if (pList!!.size > 1) {//遍歷所有的可能結(jié)果 ,與所有的不可能對(duì)比
for ((k, p) in pList.withIndex()) {
var count = 0
list.forEach { innerT, innerU ->
var impList = impossibleHashMap.get(innerT)
if (impList!!.contains(p)) {
count++
}
}
if (count == 8) {
possibleHashMap.put(t, mutableListOf(p))
defaultNum[t] = p
}
}
}
}
}
}
/**
* 將不可能去掉,留下可能與不可能作比較
*/
private fun removeImpossible() {
for ((index, num) in defaultNum.withIndex()) {
var defaultHashSet = getDefaultHashSet()
var imList = impossibleHashMap.get(index)?.toMutableSet()!!
defaultHashSet.removeAll(imList)
possibleHashMap.put(index, defaultHashSet.toMutableList())
if (defaultHashSet.size == 1) {
defaultNum[index] = defaultHashSet.first()
}
}
}
private fun setImpossibleList() {
for ((index, num) in defaultNum.withIndex()) {
//先判斷是否為定值
if (num > 0) {
var defaultHashSet = getDefaultHashSet()
defaultHashSet.remove(num)
impossibleHashMap.put(index, defaultHashSet.toMutableList())
} else {
//當(dāng)前的數(shù)字 與之相關(guān)的行、列、9格子等
var aimList = relatedRowImpossible(index).toMutableSet()
aimList.addAll(relatedVolImpossible(index).toMutableSet())
aimList.addAll(relatedCenterImpossible(index).toMutableSet())
impossibleHashMap.put(index, aimList.toMutableList())
}
}
}
//=============================================================================================
//凡是存在的值,就是不可能的值
private fun relatedRowImpossible(index: Int): MutableSet<Int> {
var oneRowList = rowHashList.get(index / 9)
return getImpossibleSet(oneRowList)
}
private fun relatedVolImpossible(index: Int): MutableSet<Int> {
var oneVolList = volHashList.get(index % 9)
return getImpossibleSet(oneVolList)
}
private fun relatedCenterImpossible(index: Int): MutableSet<Int> {
var oneCenterList = centerHashList.get(centerDefaultHashMap.get(index)!!)
return getImpossibleSet(oneCenterList)
}
private fun getImpossibleSet(oneRowList: HashMap<Int, Int>): MutableSet<Int> {
var mutableSet = mutableSetOf<Int>()
oneRowList.forEach { t, u ->
mutableSet.add(u)
}
return mutableSet
}
//===========================================================================================
//==========================列、行、九格子單獨(dú)拿出來===============================================
private fun addRow() {
addHashList(rowHashList)
}
private fun addVol() {
addHashList(volHashList)
}
private fun addCenter() {
for (i in 0..8) {
var hashMap = HashMap<Int, Int>()
centerHashList.add(hashMap)
}
var L = -1
for (m in 0..54 step 27) {//0--27--54
for (n in 0..6 step 3) {//0--3--6
L++
for (i in 0..18 step 9) {//0--9--18
for (k in 0..2) {//0--1--2
centerDefaultHashMap.put(m + n + i + k, L)
centerHashList[L].put(m + n + i + k, defaultNum[m + n + i + k])
}
}
}
}
}
private fun addHashList(hashList: MutableList<HashMap<Int, Int>>) {
for (i in 0..8) {
var hashMap = HashMap<Int, Int>()
hashList.add(hashMap)
}
for ((index, num) in defaultNum.withIndex()) {
hashList[index / 9].put(index, num)
}
}
//===============================打印============================================================
private fun printHashList(hashList: MutableList<HashMap<Int, Int>>) {
for ((index, hashMap) in hashList.withIndex()) {
println()
hashMap.keys.forEach {
print(" ${hashMap[it]} ")
}
}
}
private fun printList() {
for ((index, elm) in defaultNum.withIndex()) {
if (index % 9 == 0) {
println()
}
if (index % 27 == 0) {
println()
}
if (index % 3 == 0) {
print(" ")
}
print("${elm},")
}
}
//=========================獲取默認(rèn)的1-9 用于去除impossible========================================
private fun getDefaultHashSet(): MutableSet<Int> {
var defaultHashSet = mutableSetOf<Int>()
for (i in 1..9) {
defaultHashSet.add(i)
}
return defaultHashSet
}
}
//(1,1)(2,2)
fun setData(row: Int, col: Int, num: Int) {
defaultNum[(row - 1) * 9 + col - 1] = num
}
}