Saved Bookmarks
| 1. |
Solve : Sudoku glitch? |
|
Answer» This has REALLY GOT me stumped. I created a sudoku-solving script (still in the works) that's getting a really weird issue. 4_153_68_(By the way, this is the answer I got when I ran the script)Quote 4 2 1 5 3 9 6 8 7 But I get a weird issue when I try this puzzle: (which can be solved, ACCORDING to http://www.sudokusolver.co.uk/) Quote 7_4_2__96Issue: Quote 7 _ 4 _ 2 _ _ 9 6 <-- No spots filled, even though it pauses (meaning it's done) And for some other puzzles, it messes up and says one of the numbers is "_=". Puzzle: Quote 4_3__9168Result: Quote 4 2 3 5 7 9 1 6 8 Anyway, here is the script I'm using: Code: [Select]@echo off setlocal enabledelayedexpansion set line0=4_153_68_ set line1=639____51 set line2=_7_6412_9 set line3=___152_74 set line4=782___915 set line5=14_798___ set line6=2_7486_9_ set line7=39____768 set line8=_16_735_2 ::set num=0 ::for /f "delims=" %%A in (sudoku4.txt) do set line!num!=%%A & set /a num+=1 for /l %%L in (0,1,8) do ( for /l %%C in (0,1,8) do ( set l%%Lc%%C=!line%%L:~%%C,1! ) ) :re call :display for /l %%L in (0,1,8) do ( for /l %%C in (0,1,8) do ( if !l%%Lc%%C!==_ ( set l%%Lc%%Cposs=123456789 for /l %%Z in (0,1,8) do ( for %%P in (!l%%Lc%%Z!) do ( set l%%Lc%%Cposs=!l%%Lc%%Cposs:%%P=! ) ) for /l %%Z in (0,1,8) do ( for %%P in (!l%%Zc%%C!) do ( set l%%Lc%%Cposs=!l%%Lc%%Cposs:%%P=! ) ) ::check box if %%L LEQ 2 ( if %%C LEQ 2 ( set box=1 ) else if %%C LEQ 5 ( set box=2 ) else ( set box=3 ) ) else if %%L LEQ 5 ( if %%C LEQ 2 ( set box=4 ) else if %%C LEQ 5 ( set box=5 ) else ( set box=6 ) ) else ( if %%C LEQ 2 ( set box=7 ) else if %%C LEQ 5 ( set box=8 ) else ( set box=9 ) ) ::echo %%L,%%C is in !box! if box==1 for %%P in (!l0c0! !l0c1! !l0c2! !l1c0! !l1c1! !l1c2! !l2c0! !l2c1! !l2c2!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==2 for %%P in (!l3c0! !l3c1! !l3c2! !l4c0! !l4c1! !l4c2! !l5c0! !l5c1! !l5c2!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==3 for %%P in (!l6c0! !l6c1! !l6c2! !l7c0! !l7c1! !l7c2! !l8c0! !l8c1! !l8c2!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==4 for %%P in (!l0c3! !l0c4! !l0c5! !l1c3! !l1c4! !l1c5! !l2c3! !l2c4! !l2c5!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==5 for %%P in (!l3c3! !l3c4! !l3c5! !l4c3! !l4c4! !l4c5! !l5c3! !l5c4! !l5c5!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==6 for %%P in (!l6c3! !l6c4! !l6c5! !l7c3! !l7c4! !l7c5! !l8c3! !l8c4! !l8c5!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==7 for %%P in (!l0c6! !l0c7! !l0c8! !l1c6! !l1c7! !l1c8! !l2c6! !l2c7! !l2c8!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==8 for %%P in (!l3c6! !l3c7! !l3c8! !l4c6! !l4c7! !l4c8! !l5c6! !l5c7! !l5c8!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! if box==9 for %%P in (!l6c6! !l6c7! !l6c8! !l7c6! !l7c7! !l7c8! !l8c6! !l8c7! !l8c8!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=! ping localhost -n 1 > nul call :length !l%%Lc%%Cposs! if !len!==1 ( set l%%Lc%%C=!l%%Lc%%Cposs! goto re ) ) ) ) pause exit :length set #=%1 set len=0 :lenloop if defined # (set #=%#:~1%&set /A len += 1&goto lenloop) goto :eof :display cls echo. for /l %%L in (0,1,8) do ( set line= for /l %%C in (0,1,8) do ( set line=!line! !l%%Lc%%C! ) echo !line! ) As you can see, it solves the puzzle in the code fairly quickly, but like I said, it doesn't work for some. I've tried 5 different puzzles, but those are the issues I get.Oh dear... a sodoku solver in batch? Well, the first thing that leaps out at me is that there is no way you are using all the different algorithms which are required to reliably solve sodoku puzzles. Brute force/CRME will solve some, but not all; I can't really tell but I'm pretty sure you are only doing a brute force attempt to solve, and aren't performing any checks for lone rangers or twins. For your first "weird" issue puzzle, my program gives me this: I have no idea if it's right or not. I'm surprised it even looks like it worked, I would have practically bet money that it either crashed or only half-filled the board- I guess I don't have a lot of faith in any of my old VB6 programs anymore... As for your puzzle that "messes up"... it's not even a valid puzzle. the 5th column has two fours in the initial setup. the "result" still has two fours, but no doubt the fact that there were two fours has caused the equals sign issue. I wouldn't have even noticed if my program didn't PREVENT me from selecting four for the second instance . You can discount that "problem" entirely since it's not even a valid puzzle to start with. any other problems are almost certainly because you are only using CRME (Column-Row Minigrid Elimination) and aren't dealing with Lone Rangers or twins; I gave up on mine when it couldn't complete some of the "hard" puzzles from a puzzle book; no doubt I need to implement triplets to get those to be solved problem-free. Wow, I'm surprised I didn't realize that the puzzles were wrong originally. I realized that I wouldn't be able to solve every puzzle, but thanks for your help anyway.Here's a very good example of what you're talking about BC. Puzzle: Quote __91_7_42 Solution from the script: Quote 6 3 9 1 5 7 8 4 2 Solution from the script (and me): Quote 6 3 9 | 1 5 7 | 8 4 2Mine solved it! yay. Sorry I'm just surprised at how well it seems to work. What's really weird, my script should have solved the puzzle I just posted... Quote
|
|