中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-09 16:59
47,811 topics / 349,896 posts / today 1 new / 48,254 members
DOS批处理 & 脚本技术(批处理室) » [Quiz][Discussion][Sudoku] Three Steps to Solve
Printable Version  3,903 / 20
Floor1 flyinspace Posted 2008-08-20 11:41
银牌会员 Posts 517 Credits 1,206
Please have those who understand Sudoku rules start solving directly from the red text part.

1. Requirement: No third-party tools are allowed to be used
2. The script can be vbs, bat:
3. All commands built into Windows XP can be used.

Initially, this topic was proposed by
moniuming
in the forum:
Discussion addresses are as follows:
------------------------------------------------------------------------------------------------
http://www.cn-dos.net/forum/viewthread.php?tid=42108&fpage=3
http://www.cn-dos.net/forum/viewthread.php?tid=42157&fpage=1
------------------------------------------------------------------------------------------------
Due to work time constraints, I have not had time to study this topic.

Now, putting aside the discussion area (the data in the discussion area does not meet the requirements), let's start our Sudoku trilogy.

Sudoku rules are as follows:
********************************************************
1. All data in each row is 1-9, no duplicates, and no unused numbers
2. All data in each column is 1-9, no duplicates, and no unused numbers.
3. In a 9*9 area, it is evenly divided into 9 3*3 small areas. In any 3*3 small area, the data is 1-9, no duplicates, and no unused numbers.
**********************************************************
For example, the following is divided into 3 3*3 areas
┏━┳━┳━┳━┳━┳━┳━┳━┳━┓
┃4 ┃7 ┃2 ┃3 ┃9 ┃6 ┃5 ┃1 ┃8 ┃
┣━╋━╋━╋━╋━╋━╋━╋━╋━┫
┃6 ┃8 ┃1 ┃5 ┃4 ┃7 ┃9 ┃2 ┃3 ┃
┣━╋━╋━╋━╋━╋━╋━╋━╋━┫
┃3 ┃5 ┃9 ┃2 ┃8 ┃1 ┃7 ┃4 ┃6 ┃
┗━┻━┻━┻━┻━┻━┻━┻━┻━┛
I generated a 3*9 digital area, and there are no duplicate phenomena in this 3*9 digital area.

After understanding the above prerequisites, the problem comes out.

First, use the Sudoku rules to generate a 9*9 Sudoku area, and the requirements are
1. The data in all rows is 1-9, no duplicates, and no unused numbers

2. The data in all 3*3 areas is 1-9, no duplicates, and no unused numbers

3. No requirements for columns
Those who can complete this question, +1 point for completing item 1.
+3 points for completing item 2.
+5 points for completing both items 1 and 2
----------------------------------------------------------------------
Second, use the Sudoku rules to generate a 9*9 area, and the requirements are
1. The data in all rows is 1-9, no duplicates, and no unused numbers
2. The data in all columns is 1-9, no duplicates, and no unused numbers
3. No requirements for 3*3 areas
Those who can complete this question, +1 point for completing item 1.
+2 points for completing item 2.
+4 points for completing both items 1 and 2.
-----------------------------------------------------------------------
Third, use the Sudoku rules to generate a 9*9 area, and the requirements are
1. The data in all rows and columns is 1-9, no duplicates, and no unused numbers.
2. The data in all 3*3 areas is 1-9, no duplicates, and no unused numbers.
3. A good conflict detection environment (that is, the problem of computer operation overhead)
Those who complete this question, +15 points... (At this time, the Sudoku list is completed)
-------------------------------------------------------------------------

I have completed question three, but each time it takes a long time, the longest is more than 2 hours. If there were no screen prompts to generate the number sequence, I almost thought I was in an infinite loop.

Thank you for correction, and the typo in the question has been corrected.

[ Last edited by flyinspace on 2008-8-21 at 03:17 PM ]
Floor2 flyinspace Posted 2008-08-20 11:43
银牌会员 Posts 517 Credits 1,206
People who complete the questions, please note: which project number it is. The code is enclosed in
Floor3 slore Posted 2008-08-20 16:46
铂金会员 Posts 2,478 Credits 5,212
The Dancing Links algorithm seems to be the best search algorithm. But it's really hard to implement in scripts... Still have to use the most traditional methods...
Floor4 523066680 Posted 2008-08-20 16:59
银牌会员 Posts 1,133 Credits 2,362
Request slore to join the QQ group of hat (or I don't know if you are in the group). The group number is in hat's signature
Floor5 moniuming Posted 2008-08-20 23:55
银牌会员 Posts 574 Credits 1,335 From 广西
Question 3, not much testing has been done yet, no problems found so far, everyone is welcome to find faults...
Floor6 moniuming Posted 2008-08-21 00:03
银牌会员 Posts 574 Credits 1,335 From 广西
Just now, when testing, a problem was found. When the following situation occurs, it will fall into an infinite loop. The reason is that the first three digits of the sixth line can only take 1, 3, 9, but these three numbers have already appeared in the second column of the first three lines, ==> The infinite loop occurs...
Floor7 moniuming Posted 2008-08-21 00:11
银牌会员 Posts 574 Credits 1,335 From 广西
This problem has occurred again. To solve this problem, we need to reset everything completely, and a lot of code will have to be added. Alas...


[ Last edited by moniuming on 2008-8-21 at 12:15 AM ]
Floor8 terse Posted 2008-08-21 03:10
银牌会员 Posts 946 Credits 2,404
### Problem 3
Floor9 523066680 Posted 2008-08-21 07:12
银牌会员 Posts 1,133 Credits 2,362
Ask, the number range mentioned in the subsequent question is 0-9. There are a total of 10 numbers here. Is this confirmed?
Floor10 slore Posted 2008-08-21 09:25
铂金会员 Posts 2,478 Credits 5,212
Floor11 moniuming Posted 2008-08-21 11:31
银牌会员 Posts 574 Credits 1,335 From 广西
Correct the code on floor 6, no more infinite loops, heh...
Floor12 flyinspace Posted 2008-08-21 15:08
银牌会员 Posts 517 Credits 1,206
Statistics through the following code
--------------------------
@echo off
set "GetCurTime=00:00:08.17"
set "GetOldTime=23:59:09.19"
for /f "delims=:. tokens=1,2,3,4" %%i in ('echo %GetCurTime%') do (
set "Curhour=%%i"
set "CurMin=%%j"
set "CurSec=%%k"
set "CurBit=%%l"
)
if "%Curhour:~0,1%"=="0" set "Curhour=%Curhour:~1,1%"
if "%CurMin:~0,1%"=="0" set "CurMin=%CurMin:~1,1%"
if "%CurSec:~0,1%"=="0" set "CurSec=%CurSec:~1,1%"
if "%CurBit:~0,1%"=="0" set "CurBit=%CurBit:~1,1%"
for /f "delims=:. tokens=1,2,3,4" %%i in ('echo %GetOldTime%') do (
set "Oldhour=%%i"
set "OldMin=%%j"
set "OldSec=%%k"
set "OldBit=%%l"
)
if "%Oldhour:~0,1%"=="0" set "Oldhour=%Oldhour:~1,1%"
if "%OldMin:~0,1%"=="0" set "OldMin=%OldMin:~1,1%"
if "%OldSec:~0,1%"=="0" set "OldSec=%OldSec:~1,1%"
if "%OldBit:~0,1%"=="0" set "OldBit=%OldBit:~1,1%"
if "%Curhour%" LSS "%Oldhour%" set "Curhour=24"
set /a "TotalTime=(%Curhour%-%Oldhour%)*60*60*100 + (%CurMin%-%OldMin%)*60*100+(%CurSec%-%OldSec%)*100+%CurBit%-%OldBit%"
echo Total time used: %TotalTime% microseconds
pause
--------------------------------------------
Code on floor 9
In 1000 calculations, the longest one is 7953 microseconds
On floor 12
In 1000 calculations, the longest one is 10513 microseconds

If you have any questions, contact flyinspace.

I modified the code program to do the statistics automatically
Floor13 flyinspace Posted 2008-08-21 15:12
银牌会员 Posts 517 Credits 1,206
Where did you get the algorithm from floor 9? It's amazing.
When my code is almost as long as the code from floor 9, the statistics take a long time.
After adding multi - level judgments, the running time is reduced.
Floor14 523066680 Posted 2008-08-21 15:41
银牌会员 Posts 1,133 Credits 2,362
Maybe it's original.

Floor 12 really surprised me. I didn't even dare to look at the title. I will definitely explore your code when I have time!
Remember to mark the original author, I'm saving it.

[ Last edited by 523066680 on 2008-8-21 at 03:46 PM ]
Floor15 slore Posted 2008-08-22 16:21
铂金会员 Posts 2,478 Credits 5,212

'---------------------------------------------
' Soduko.vbs - Sudoku calculation VBScript script
'
' The code will solve the Sudoku in Soduko.ini in the same directory.
' The code is for learning only, please retain this information when reprinting.
'
' August 22, 2008 By Slore
'---------------------------------------------
Const x = 0
Const y = 1

Const ReadInitFile = 1 'Change to 0 for generation mode

Const ForReading = 1

InitFile = "SodukoE.ini"
If ReadInitFile Then InitFile = "Soduko.ini"


Dim SodukoX
Dim SodukoY(8)
Dim SodukoZ(8)
Dim SodukoBoard(9,9)
Dim SolveSequence()

Dim InitialStr,iPanesToSolve

If LCase(Right(WSH.FullName,11)) = "wscript.exe" Then
Set
objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "Cscript //nologo " & WScript.ScriptFullName
Set objShell = Nothing
WSH.Quit
End If

Soduko_Initialize
CreateExecutionPlan

If iPanesToSolve >= 0 Then
bSuccess = SolvePane(0)
Else
bSuccess = True
End If

If
bSuccess Then
SolveSuccess
Else
MsgBox
"This Sudoku cannot be completed!", vbExclamation,"Result" 'SolveFailed
End If

Private Sub
Soduko_Initialize()

For i = 0 To 8
SodukoY(i) = "000000000"
SodukoZ(i) = "000000000"
Next

Set
objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(InitFile,ForReading)
InitialStr = Replace(objFile.ReadAll," ","")
objFile.Close
Set
objFile = Nothing
Set
objFSO = Nothing

SodukoX = Split(InitialStr,vbCrLf)
InitialStr = Replace(InitialStr,vbCrLf,"")

For i = 1 To 81
iValue = Mid(InitialStr,i,1)
PosX = (i + 8) \ 9
PosY = i + 9 - PosX * 9 '((i+8) mod 9)+1
PosZ = ((PosX - 1) \ 3) * 3 + (PosY - 1) \ 3

SodukoBoard(PosX,PosY) = iValue
SodukoY(PosY - 1) = Left(SodukoY(PosY - 1),PosX - 1) & iValue & Mid(SodukoY(PosY - 1),PosX + 1)
Pos = ((PosX - 1) Mod 3) * 3 + (PosY - 1) Mod 3
SodukoZ(PosZ) = Left(SodukoZ(PosZ),Pos) & iValue & Mid(SodukoZ(PosZ),Pos + 2)

If iValue = 0 Then
ReDim Preserve SolveSequence(1,iCount)
SolveSequence(x,iCount) = PosX
SolveSequence(y,iCount) = PosY
iCount = iCount + 1
End If
Next

iPanesToSolve = iCount - 1

End Sub


Private Sub
CreateExecutionPlan()
Do
iPreSolvedCount = 0
For iCount = 0 To iPanesToSolve
PosX = SolveSequence(x,iCount)
PosY = SolveSequence(y,iCount)

If PosX <> - 1 Then
sValues = GetValuesToTest(PosX,PosY)

If Len(sValues) <= 1 Then
If Len(sValues) = 1 Then
Call SetValue(PosX,PosY,sValues)
End If
SolveSequence(x,iCount) = - 1
iPreSolvedCount = iPreSolvedCount + 1
End If
End If
Next
If
iPreSolvedCount = 0 Then
Exit Do
Else
bRearrangeExecutionArray = True
End If
Loop

If
bRearrangeExecutionArray Then
For iCount = 0 To iPanesToSolve
If SolveSequence(x,iCount) <> - 1 Then
SolveSequence(x,iLastArrayPos) = SolveSequence(x,iCount)
SolveSequence(y,iLastArrayPos) = SolveSequence(y,iCount)

iLastArrayPos = iLastArrayPos + 1
End If
Next

If
iLastArrayPos > 0 Then
ReDim Preserve SolveSequence(1,iLastArrayPos - 1)
End If
iPanesToSolve = iLastArrayPos - 1
End If
End Sub


Private Function
SolvePane(ByVal iSolveSequence)

PosX = SolveSequence(x,iSolveSequence)
PosY = SolveSequence(y,iSolveSequence)

sValueList = GetValuesToTest(PosX, PosY)
Randomize
l = Len(sValueList)
If l > 0 Then

Do While l
iValuePos = Int(Rnd * l) + 1

iValue = CInt(Mid(sValueList, iValuePos, 1))
sValueList = Left(sValueList, iValuePos - 1) & Mid(sValueList, iValuePos + 1)
Call SetValue(PosX,PosY,iValue)

If iSolveSequence < iPanesToSolve Then
bSuccess = SolvePane(iSolveSequence + 1)
Else
bSuccess = True
End If

If
bSuccess Then
Exit Do
End If
l = Len(sValueList)
Loop

Else
bSuccess = False
End If

If
bSuccess = False Then
Call
SetValue(PosX,PosY,0)
End If

SolvePane = bSuccess
End Function

Private Function
GetValuesToTest(PosX,PosY)
PosZ = ((PosX - 1) \ 3) * 3 + (PosY - 1) \ 3
SetedValue = SodukoX(PosX - 1) & SodukoY(PosY - 1) & SodukoZ(PosZ)
For i = 1 To 9
If InStr(1,SetedValue,i) = 0 Then
GetValuesToTest = GetValuesToTest & i
End If
Next
End Function

Private Sub
SetValue(PosX,PosY,iValue)
SodukoBoard(PosX,PosY) = iValue
PosZ = ((PosX - 1) \ 3) * 3 + (PosY - 1) \ 3
SodukoX(PosX - 1) = Left(SodukoX(PosX - 1),PosY - 1) & iValue & Mid(SodukoX(PosX - 1),PosY + 1)
SodukoY(PosY - 1) = Left(SodukoY(PosY - 1),PosX - 1) & iValue & Mid(SodukoY(PosY - 1),PosX + 1)
Pos = ((PosX - 1) Mod 3) * 3 + (PosY - 1) Mod 3
SodukoZ(PosZ) = Left(SodukoZ(PosZ),Pos) & iValue & Mid(SodukoZ(PosZ),Pos + 2)
End Sub

Private Sub
SolveSuccess()
'For i = 0 To 8
' WSH.Echo SodukoX(i)
'Next

For i = 1 To 9
OutStr = ""
For j = 1 To 9
OutStr = OutStr & SodukoBoard(i,j) & " "
If (j Mod 3) = 0 Then OutStr = OutStr & " "
Next
WSH.Echo OutStr
If (i Mod 3) = 0 Then WSH.Echo
Next
MsgBox
"Sudoku filled successfully!", vbInformation,"Result"

End Sub


Attachment:
Download

[ Last edited by slore on 2008-8-22 at 04:27 PM ]
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023