Thursday 15 April 2010

java - What is wrong with my backtracking algorithm? -


I am working on a program that generates Sudoku puzzles, I use a back tracking algorithm to do this. I was trying to do but my program is not working. The program runs just infinitely and never resolves. I do not know if there is a small problem with it or how I incorrectly write a backstraking algorithm

  package sudoku; Import java.util.Random; Public square puzzle {// 9x 9 puzzle Private Ent Puzzle [] [] = New Int [9] [9]; // A fully resolved Sudoku board generates public int [] [] Generate () {random people = new random (); // by adding squares to square squares (int y = 0; y & lt; 9; y ++) {for (int x = 0; x & lt; 9; x ++) {// random Generate 1-9 int Num = gen.nextInt (9) + 1; Int count = 0; Boolean valid = false; While valid (valid == incorrect) {// check if the number is valid (checker (num, x) & check call (num, y) & amp; check sign (num, x, y)) {// add board puzzle number [x] [y] = num; // Exit Loop; Valid = true to go to the next category; } Else {// Try the next number if (num == 9) {num = 1; } And {num ++; } // increase counter count ++; // If the counter arrived at 9, all the numbers were tried and // was not valid, start back tracking if (calculation == 9) {// return 1 square if (x == 0) { X = 8; Y--; } And {x--; } // empty square puzzle [x] [y] = 0; // reset count count = 0; }}}}} Return puzzle; } // check each element for num, if returned, then returned false private boolean check rho (int number, int row) {for (int i = 0; i    

There are several problems that may occur. I'll just put an example.

The main reason is not working behind, because you are not backstracking. If you see only one state back in the tree, backtracking means that you check all the possibilities of sub-tree and then (if none is valid), then you ignore that subprime, no matter how big.

Let's see. Your attitude "keeps all the lines in one line and hopes that the class is completed. If there is any error in handling the current class, clean the previous one."

There is no problem at the beginning, getting the first lines will not cause any errors, but think of the possibility that you have completed the first 8 lines, with something like:

  1 2 3 ---- 4 5 6 --- 79 832 | 179 | There is no valid value for 456 x   

x . What does your algorithm do? Go back and try to change 6! Surprisingly, 6 will end by changing with 6, and trying to set the value to x will be trying again.

Those who are the sudoak generators in the internet, take a valid solution and bring many changes in return, in this way, all changes bring valid solutions (for more details, ask Google).

If you want to use back tracking, you should scan if Sudoku remains still (or at least it is not "broken") in each step. And there is a way to repeat non-resolvable combinations.

In addition to this, it seems to be trying to keep the numbers in order (it is an opinion) to make a barrier very strong at the beginning. Filling the first two rows is easy, but it will put the condition of the entire solution (note that filling the first row does not affect it!) - D)

No comments:

Post a Comment