sudoku

Determine whether a Sudoku has a unique solution

我与影子孤独终老i 提交于 2019-12-20 04:31:28
问题 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

Sudoku Backtracking Non valid Sudoku

强颜欢笑 提交于 2019-12-20 03:33:16
问题 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

Sudoku validity check algorithm - how does this code works?

橙三吉。 提交于 2019-12-18 11:07:20
问题 I was reading a question posted here: Sudoku algorithm in C# And one of the solutions posted was this piece of code. public static bool IsValid(int[] values) { int flag = 0; foreach (int value in values) { if (value != 0) { int bit = 1 << value; if ((flag & bit) != 0) return false; flag |= bit; } } return true; } The idea is that it will detect duplicates in the array of values; but I'm overwhelmed by how much I don't know. Can someone explain this to me? EDIT: Thanks everyone. So many great

Sudoku validity check algorithm - how does this code works?

余生颓废 提交于 2019-12-18 11:07:03
问题 I was reading a question posted here: Sudoku algorithm in C# And one of the solutions posted was this piece of code. public static bool IsValid(int[] values) { int flag = 0; foreach (int value in values) { if (value != 0) { int bit = 1 << value; if ((flag & bit) != 0) return false; flag |= bit; } } return true; } The idea is that it will detect duplicates in the array of values; but I'm overwhelmed by how much I don't know. Can someone explain this to me? EDIT: Thanks everyone. So many great

Optimizing the backtracking algorithm solving Sudoku

て烟熏妆下的殇ゞ 提交于 2019-12-18 10:55:15
问题 I'm hoping to optimize my backtracking algorithm for my Sudoku Solver. What it does now: The recursive solver function takes a sudoku puzzle with various given values. I will scour through all the empty slots in the puzzle, looking for the slot that has the least possibilities, and get the list of values. From the list of values, I will loop through it by placing one of the values from the list in the slot, and recursively solve it, until the entire grid is filled. This implementation still

Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)

前提是你 提交于 2019-12-17 20:03:47
问题 I'm trying to code a sudoku solver, and the way I attempted to do so was to have a 9x9 grid of pointers that hold the address of "set" objects that posses either the solution or valid possible values. I was able to go through the array with 2 for loops, through each column first and then going to the next row and repeating. However, I'm having a hard time imagining how I would designate which sub-grid (or box, block etc) a specific cell belongs to. My initial impression was to have if

Creating sudoku initial boards

霸气de小男生 提交于 2019-12-17 18:52:27
问题 Is there an algorithm or way I can get initial state sudoku puzzles for a sudoku game. Preferably with the ability to have different levels of difficulty? 回答1: Basically there are two approaches. In both you need to have 2 solvers, a humanlike solver, which uses strategies performable by a human and a backtracking solver. With the first approach you generate a random complete solution and iteratively remove random cells solutions. Backtracking solver will make sure, that there still exist

Sudoku table generator failure, lisp

别说谁变了你拦得住时间么 提交于 2019-12-14 02:18:07
问题 I have a problem with some part of my lisp code. It is a sudoku table generator. It works fine until this part: (loop for e in entries do (if (and (not (member e sub)) (not (member e col))) (progn (setq choices (nconc choices (list e))) (print choices))) (if (= (length choices) 1) (setq pick (car choices)) (if (not (= (length choices) 0)) (setq pick (nth (random (+ 0 (length choices))) choices)))) Basically, I am on a row x and a column y, and I need to insert an element. I watch the

Sudoku generator loop

瘦欲@ 提交于 2019-12-13 22:17:08
问题 I'm trying to make a sudoku generator so I can make into a sudoku game and I've encountered a problem... I have successfully made a method which checks a certain cell and whether the number in it repeats in the same row, column or 3x3 square it belongs to but I have a problem with generating the numbers randomly and filling them in. Basically first I fill the first line with random numbers from 1-9 which only appear once in the line. My question is, is it possible to fill cell after cell with

Creating 9 subarrays from a 9x9 2d array java

半城伤御伤魂 提交于 2019-12-13 17:48:59
问题 Hello I need help creating 9 sub arrays of 3x3 dimensions from a 9x9 array. I've seen that stackOverflow had a similar question already asked but unfortunately it was in c++. Can anyone point me in the right direction of how to create a a sub array. Edit: had aa similar changed to had a similar public static void Validate(final int[][] sudokuBoard) { int width = sudokuBoard[0].length; int height = sudokuBoard.length; for(int i = 0; i < width; i++) if(!IsValidRow(sudokuBoard, i, width)) {