sudoku

The Dancing Links Algorithm - An explanation that is less explanatory but more on implementation?

点点圈 提交于 2019-12-03 01:31:05
问题 I've been working on a Sudoku Solver, my current solver uses the backtracking algorithm but it still takes too long. I'm hoping to get it down to less than a second for most cases. As such, I've decided to rewrite it with the dancing links algorithm, understanding it is one of the better bruteforce methods that works well especially with a constraint problem such as the Sudoku Puzzle. I have tried to read the Wiki and Knuth's paper on it, however both of them are kinda hard to comprehend and

Sudoku algorithm in C#

被刻印的时光 ゝ 提交于 2019-12-02 21:22:18
I need one liner (or close to it) that verifies that given array of 9 elements doesn't contain repeating numbers 1,2,3,...,9. Repeating zeroes do not count (they represent empty cells). The best I have came out so far is: var a = new int[9] {1,2,3,4,5,6,7,8,9}; var itIsOk = a.Join(a, i => i, j => j, (x, y) => x) .GroupBy(y => y).Where(g => g.Key > 0 && g.Count() > 1).Count() == 0; If you don't want to solve my problems :), could you at least tell if the above algorithm works correctly? And, yes, a have read this one . Lucky for you I built a sudoku solver myself not too long ago :) The whole

The Dancing Links Algorithm - An explanation that is less explanatory but more on implementation?

风流意气都作罢 提交于 2019-12-02 14:55:50
I've been working on a Sudoku Solver, my current solver uses the backtracking algorithm but it still takes too long. I'm hoping to get it down to less than a second for most cases. As such, I've decided to rewrite it with the dancing links algorithm, understanding it is one of the better bruteforce methods that works well especially with a constraint problem such as the Sudoku Puzzle. I have tried to read the Wiki and Knuth's paper on it, however both of them are kinda hard to comprehend and extremely verbose. I also read Sudopedia's version on it, and it seems that once it got to the Sudoku's

A cool algorithm to check a Sudoku field?

纵饮孤独 提交于 2019-12-02 14:54:55
Does anyone know a simple algorithm to check if a Sudoku-Configuration is valid? The simplest algorithm I came up with is (for a board of size n) in Pseudocode for each row for each number k in 1..n if k is not in the row (using another for-loop) return not-a-solution ..do the same for each column But I'm quite sure there must be a better (in the sense of more elegant) solution. Efficiency is quite unimportant. Radu094 You need to check for all the constraints of Sudoku : check the sum on each row check the sum on each column check for sum on each box check for duplicate numbers on each row

Sudoku solver, not backtracking solver [closed]

偶尔善良 提交于 2019-12-02 13:36:35
The past few weeks I've been working on a sudoku game. The game as a lot of features such as: play game, print sudoku, solve sudoku. The solve function uses conventional backtracking but thats not the issue, the issue is that I need the game to be able to produce a humanely solvable sudoku, for that I need a method that will be able to solve a sudoku as a human would do it. If anyone could help me work out the mechanics of how this could be done I would greatly appreciate it. An overwhelming collection of Sudoku solving strategies for human players is nicely presented and explained on Andrew

Dynamic generate sudoku board table using javascript

前提是你 提交于 2019-12-02 12:49:40
I am trying to to generate a Sudoku board using this script: The problem is that I don't know how to validate to generate unique numbers on columns and squares. Actually is just validating and generating unique numbers only on rows. Here is that code : function generate(count, values) { return Array.apply(null, { length: count }).map(function () { var r = [], array = values.slice(); while (array.length) { r.push(array.splice(Math.floor(Math.random() * array.length), 1)[0]); } return r; }); }; var myStringArray = generate(9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); Array.from(document

How to create lists of 3x3 sudoku block in python

守給你的承諾、 提交于 2019-12-02 09:21:04
问题 I need help creating a list for each of the 9 3x3 blocks in sudoku. so I have a list of lists representing the original sudoku board (zero means empty): board=[[2,0,0,0,0,0,0,6,0], [0,0,0,0,7,5,0,3,0], [0,4,8,0,9,0,1,0,0], [0,0,0,3,0,0,0,0,0], [3,0,0,0,1,0,0,0,9], [0,0,0,0,0,8,0,0,0], [0,0,1,0,2,0,5,7,0], [0,8,0,7,3,0,0,0,0], [0,9,0,0,0,0,0,0,4]] I need to turn these into a list of lists containing the 3x3 blocks. So for example: [[2,0,0,0,0,0,0,4,8],[etc]] i tried creating one list called

convert an 2d array from rows to blocks

为君一笑 提交于 2019-12-02 07:20:25
I am trying to work in this code for weeks. I need to convert rows and columns in a 2d array to blocks. it should work on any matrix in size n*n. (that I have been given the size of the array) for example: this: int[][] input = {{1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}, {1,2,3,4,5,6,7,8,9}} ; this is need to be the output: {{1,2,3,1,2,3,1,2,3} {4,5,6,4,5,6,4,5,6} {7,8,9,7,8,9,7,8,9} {1,2,3,1,2,3,1,2,3} {4,5,6,4,5,6,4,5,6} {7,8,9,7,8,9,7,8,9} {1,2,3,1,2,3,1,2,3} {4,5,6,4

Determine whether a Sudoku has a unique solution

╄→尐↘猪︶ㄣ 提交于 2019-12-02 04:13:13
I'm struggling with a backtracking algorithm to determine wether a Sudoku has a unique solution or if it has multiple Solutions. Here's the backtracking code i use: static boolean solve(int i, int j, int[][] cells) { if (i == 9) { i = 0; if (++j == 9) return true; } if (cells[i][j] != 0) // skip filled cells return solve(i+1,j,cells); for (int val = 1; val <= 9; ++val) { if (legal(i,j,val,cells)) { cells[i][j] = val; if (solve(i+1,j,cells)) return true; } } cells[i][j] = 0; // reset on backtrack return false; } First approach: If i change for (int val = 1; val <= 9; ++val) { for (int val = 9;

Sudoku Backtracking Non valid Sudoku

為{幸葍}努か 提交于 2019-12-01 23:00:22
I created a Sudoku Backtracking solver and it works just fine, but now i want to give out an error if the sudoku cant be solved because it isnt valid for example if this sudoku is given: http://img5.imageshack.us/img5/2241/sudokugq.jpg what can i do to make my solving method give out an error if it isnt solveable? I always end up with Zeros or stuck in a loop. public void solve( int row, int col ) throws Exception { // Throw an exception to stop the process if the puzzle is solved if( row > 8 ){ //Gelöst } // If the cell is not empty, continue with the next cell if( sudo[row][col] != 0 ){ next