Sudoku Solver

The Sudoku puzzle consists of a grid of 9x9 cells. This 81-cell grid is further subdivided into nine subgrids of 3x3 cells. A few of the cells are filled with a single digit between 1 and 9 (the givens). To solve the puzzle, the empty cells must be filled in such a way that every 9-cell row, column, and subgrid contains each of the digits between 1 and 9.

Most published Sudoku puzzles can be solved with deductive reasoning. For example, if the digit 9 appears in the first 2 rows and subgrids, then the digit 9 must appear in the 3rd row in the top-right subgrid. If in this subgrid, there is only one empty cell, then the empty cell must contain 9.

This solver does not employ deductive reasoning. Instead, it employs a technique called bifurcation which systematically tries every possible digit in every empty cell until the solution is achieved. Here's a summary of the algorithm.

1. Find an empty cell
   1a. If there are no more empty cells, the puzzle is solved
2. Fill the empty cell with the lowest possible valid number
   2a. If no valid number exists, backtrack to the previous empty cell
      and try the next higher valid number in that cell
3. Go to step 1
This program consists of two classes – the SudokuSolver class which is the main class and the Grid class for holding the values of a grid while it is being solved.
/**
 * This program is executed in the following way:
 *    java SudokuSolver <input-file>
 * For details of the input-file format, see the Grid.java class.
 *
 * @author  Patrick Chan
 * @version 1, 12/31/05
 * @see Grid
 */
import java.io.*;
import java.util.*;

public class SudokuSolver {
    public static void main(String[] args) throws Exception {
        // Open the file containing the givens
        File file = new File(args[0]);
        FileReader rd = new FileReader(args[0]);

        // Process each grid in the file
        while (true) {
            Grid grid = Grid.create(rd);
            if (grid == null) {
                // No more grids
                break;
            }

            // Find a solution
            List<Grid> solutions = new ArrayList<Grid>();
            solve(grid, solutions);

            printSolutions(grid, solutions);
        }
    }

    // Recursive routine that implements the bifurcation algorithm
    private static void solve(Grid grid, List<Grid> solutions) {
        // Return if there is already a solution
        if (solutions.size() >= 2) {
            return;
        }

        // Find first empty cell
        int loc = grid.findEmptyCell();

        // If no empty cells are found, a solution is found
        if (loc < 0) {
            solutions.add(grid.clone());
            return;
        }

        // Try each of the 9 digits in this empty cell
        for (int n=1; n<10; n++) {
            if (grid.set(loc, n)) {
                // With this cell set, work on the next cell
                solve(grid, solutions);

                // Clear the cell so that it can be filled with another digit
                grid.clear(loc);
            }
        }
    }

    private static void printSolutions(Grid grid, List<Grid> solutions) {
        // Print the grid with the givens
        System.out.println("Original");
        System.out.println(grid);

        // Print the solution
        if (solutions.size() == 0) {
            System.out.println("Unsolveable");
        } else if (solutions.size() == 1) {
            System.out.println("Solved");
        } else {
            System.out.println("At least two solutions");
        }

        // Print the solution(s)
        for (int i=0; i<solutions.size(); i++) {
            System.out.println(solutions.get(i));
        }
        System.out.println();
        System.out.println();
    }
}
Here is the code for the Grid.java:
/**
 * @author  Patrick Chan
 * @version 1, 12/31/05
 * @see SudokuSolver
 */
import java.io.*;
import java.util.*;

/**
 * A Grid object holds the currently known values of the Sudoku puzzle.
 * The grid consists of 9x9 cells that hold integer values from 0 to 9.
 * A value of 0 indicates that the cell is empty.
 *
 * Each of the 81 cells has a location which is used to identify a
 * specific cell. There are 81 locations, labelled location 0 to location 80.
 * Cell locations are ordered from left-to-right and top-down.
 * For example, location 0 refers to the top-leftmost cell.
 * Location 8 refers to the top-righttmost cell.
 * Location 71 refers to the bottom-leftmost cell.
 * Location 80 refers to the bottom-rightmost cell.
 *
 * The grid consists of 9 columns, labelled column 0 to column 8.
 * The grid consists of 9 rows, labelled row 0 to row 8.
 *
 * The grid consists of 9 subgrids, labelled subgrid 0 to subgrid 8.
 * Each subgrid contains a subgrid of 3x3 cells.
 * Subgrid 0 contains cells - 0, 1, 2, 9, 10, 11, 18, 19, 20.
 * Subgrid 8 contains cells - 60, 61, 62, 69, 70, 71, 78, 79, 80
 */
public class Grid implements Cloneable {
    // Array that contains values of all 81 cells in the grid.
    int[] cells = new int[81];

    // A set of bit-vectors that represent the known values for each column.
    // Specifically, if column c contains the digits d1 and d2,
    //   colsSet[c] = 2^(d1-1)|2^(d2-1)
    // For example, if column 0 contains the values 1 and 4, colsSet[0] = 9.
    // The information in this variable is redundant with the information
    // in the cells variable. The purpose of this variable is to reduce
    // the cost of determining whether a particular digit can be set in
    // a particular cell.
    int[] colsSet = new int[9];

    // This purpose and behavior of this variable is similar to colsSet.
    int[] rowsSet = new int[9];

    // This purpose and behavior of this variable is similar to colsSet.
    int[] subgridSet = new int[9];

    /**
     * This method returns a grid of givens and empty cells ready to be solved.
     * The cells containing givens have values between 1 and 9.
     * Empty cells have the value 0.
     *
     * Characters are read one at a time from the input stream and placed
     * into the grid in left-to-right and top-down order.
     * - The characters 0 or . indicates an empty cell.
     * - The characters 1 to 9 indicates a given.
     * - The character # is used for comments; subsequent characters are
     *   ignored until a newline is encountered.
     * - All other characters are simply ignored.
     *
     * @param     rd  Reader containing the givens
     * @return    null if there are not enough characters in 'rd' to form a grid.
     */
    public static Grid create(Reader rd) throws Exception {
        Grid grid = new Grid();

        // Read until all 81 cells are filled
        for (int loc=0; loc<grid.cells.length; ) {
            // Read a character
            int ch = rd.read();

            // -1 is returned if the input stream has no more characters
            if (ch < 0) {
                // No more characters so return null
                return null;
            }
            if (ch == '#') {
                // Skip to end-of-line
                while (ch >= 0 && ch != '\n' && ch != '\r') {
                    ch = rd.read();
                }
            } else if (ch >= '1' && ch <= '9') {
                // A given
                grid.set(loc, ch-'0');
                loc++;
            } else if (ch == '.' || ch == '0') {
                // Empty cell
                loc++;
            }
        }
        return grid;
    }

    /*
     * Finds an empty cell.
     * @return the location of an empty cell or -1 if there are no empty cells.
     *         Values must be in the range [-1, 80].
     */
    public int findEmptyCell() {
        for (int i=0; i<cells.length; i++) {
            if (cells[i] == 0) {
                return i;
            }
        }
        return -1;
    }

    /*
     * Sets a number in a cell. This method checks to see if
     *   1. the cell is empty
     *   2. the cell is allowed to contain the specified number. E.g. if
     *      the number is 5 but the row already has a 5, the cell will
     *      not be set and false is returned.
     * @param loc  the location of the target cell.
     *             Values must be in the range [0, 80].
     * @param num  the number to set in the cell.
     *             Values must be in the range [1, 9].
     * @return     true if the set was successful.
     */
    public boolean set(int loc, int num) {
        // Compute row and column
        int r = loc/9;
        int c = loc%9;
        int blockLoc = (r/3)*3+c/3;

        boolean canSet = cells[loc] == 0
            && (colsSet[c] & (1<<num)) == 0
            && (rowsSet[r] & (1<<num)) == 0
            && (subgridSet[blockLoc] & (1<<num)) == 0;
        if (!canSet) {
            return false;
        }

        cells[loc] = num;
        colsSet[c] |= (1<<num);
        rowsSet[r] |= (1<<num);
        subgridSet[blockLoc] |= (1<<num);
        return true;
    }

    /*
     * Removes the number in a cell.
     * @param loc  the location of the target cell.
     *             Values must be in the range [0, 80].
     */
    public void clear(int loc) {
        // Compute row and column
        int r = loc/9;
        int c = loc%9;
        int blockLoc = (r/3)*3+c/3;

        int num = cells[loc];
        cells[loc] = 0;
        colsSet[c] ^= (1<<num);
        rowsSet[r] ^= (1<<num);
        subgridSet[blockLoc] ^= (1<<num);
    }


    /**
     * Returns a copy of this grid. Any modifications to the returned
     * grid will not affect this grid.
     *
     * @return a non-null deep copy of this grid.
     */
    public Grid clone() {
        Grid grid = new Grid();
        grid.cells = cells.clone();
        grid.colsSet = colsSet.clone();
        grid.rowsSet = rowsSet.clone();
        grid.subgridSet = subgridSet.clone();
        return grid;
    }

    /**
     * Returns a string representing the current contents of the grid.
     * Used for debugging purposes.
     *
     * @return a non-null string representing the current contents of the grid.
     */
    public String toString() {
        StringBuffer buf = new StringBuffer();
        for (int r=0; r<9; r++) {
            if (r%3 == 0) {
                buf.append("-------------------------\n");
            }
            for (int c=0; c<9; c++) {
                if (c%3 == 0) {
                    buf.append("| ");
                }
                int num = cells[r*9+c];
                if (num == 0) {
                    buf.append(". ");
                } else {
                    buf.append(num+" ");
                }
            }
            buf.append("|\n");
        }
        buf.append("-------------------------");
        return buf.toString();
    }
}
Here's an example of an input file with three puzzles:
# This puzzle was obtained from sudoku.com
.6.|1.4|.5.
..8|3.5|6..
2..|...|..1
---+---+---
8..|4.7|..6
..6|...|3..
7..|9.1|..4
---+---+---
5..|...|..2
..7|2.6|9..
.4.|5.8|.7.
-----------

# http://www.sudoku.org.uk/bifurcation.htm
1 0 0 9 0 7 0 0 3
0 8 0 0 0 0 0 7 0
0 0 9 0 0 0 6 0 0
0 0 7 2 0 9 4 0 0
4 1 0 0 0 0 0 9 5
0 0 8 5 0 4 3 0 0
0 0 3 0 0 0 7 0 0
0 5 0 0 0 0 0 4 0
2 0 0 8 0 6 0 0 9

# This puzzle has multiple solutions
1 2 3 4 5 6 7 8 9
0 6 0 0 0 0 0 3 0
0 7 0 0 0 0 0 4 0
7 8 9 1 2 3 4 5 6
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
4 5 6 7 8 9 1 2 3
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Here's the output of the program when execute with the sample input file:
Original
-------------------------
| . 6 . | 1 . 4 | . 5 . |
| . . 8 | 3 . 5 | 6 . . |
| 2 . . | . . . | . . 1 |
-------------------------
| 8 . . | 4 . 7 | . . 6 |
| . . 6 | . . . | 3 . . |
| 7 . . | 9 . 1 | . . 4 |
-------------------------
| 5 . . | . . . | . . 2 |
| . . 7 | 2 . 6 | 9 . . |
| . 4 . | 5 . 8 | . 7 . |
-------------------------
Solved
-------------------------
| 9 6 3 | 1 7 4 | 2 5 8 |
| 1 7 8 | 3 2 5 | 6 4 9 |
| 2 5 4 | 6 8 9 | 7 3 1 |
-------------------------
| 8 2 1 | 4 3 7 | 5 9 6 |
| 4 9 6 | 8 5 2 | 3 1 7 |
| 7 3 5 | 9 6 1 | 8 2 4 |
-------------------------
| 5 8 9 | 7 1 3 | 4 6 2 |
| 3 1 7 | 2 4 6 | 9 8 5 |
| 6 4 2 | 5 9 8 | 1 7 3 |
-------------------------


Original
-------------------------
| 1 . . | 9 . 7 | . . 3 |
| . 8 . | . . . | . 7 . |
| . . 9 | . . . | 6 . . |
-------------------------
| . . 7 | 2 . 9 | 4 . . |
| 4 1 . | . . . | . 9 5 |
| . . 8 | 5 . 4 | 3 . . |
-------------------------
| . . 3 | . . . | 7 . . |
| . 5 . | . . . | . 4 . |
| 2 . . | 8 . 6 | . . 9 |
-------------------------
Solved
-------------------------
| 1 6 4 | 9 5 7 | 2 8 3 |
| 3 8 5 | 6 2 1 | 9 7 4 |
| 7 2 9 | 4 3 8 | 6 5 1 |
-------------------------
| 5 3 7 | 2 8 9 | 4 1 6 |
| 4 1 2 | 7 6 3 | 8 9 5 |
| 6 9 8 | 5 1 4 | 3 2 7 |
-------------------------
| 8 4 3 | 1 9 5 | 7 6 2 |
| 9 5 6 | 3 7 2 | 1 4 8 |
| 2 7 1 | 8 4 6 | 5 3 9 |
-------------------------


Original
-------------------------
| 1 2 3 | 4 5 6 | 7 8 9 |
| . 6 . | . . . | . 3 . |
| . 7 . | . . . | . 4 . |
-------------------------
| 7 8 9 | 1 2 3 | 4 5 6 |
| . . . | . . . | . . . |
| . . . | . . . | . . . |
-------------------------
| 4 5 6 | 7 8 9 | 1 2 3 |
| . . . | . . . | . . . |
| . . . | . . . | . . . |
-------------------------
At least two solutions
-------------------------
| 1 2 3 | 4 5 6 | 7 8 9 |
| 5 6 4 | 8 9 7 | 2 3 1 |
| 9 7 8 | 2 3 1 | 6 4 5 |
-------------------------
| 7 8 9 | 1 2 3 | 4 5 6 |
| 2 3 1 | 5 6 4 | 8 9 7 |
| 6 4 5 | 9 7 8 | 3 1 2 |
-------------------------
| 4 5 6 | 7 8 9 | 1 2 3 |
| 3 1 2 | 6 4 5 | 9 7 8 |
| 8 9 7 | 3 1 2 | 5 6 4 |
-------------------------
-------------------------
| 1 2 3 | 4 5 6 | 7 8 9 |
| 5 6 4 | 8 9 7 | 2 3 1 |
| 9 7 8 | 2 3 1 | 6 4 5 |
-------------------------
| 7 8 9 | 1 2 3 | 4 5 6 |
| 2 3 1 | 5 6 4 | 8 9 7 |
| 6 4 5 | 9 7 8 | 3 1 2 |
-------------------------
| 4 5 6 | 7 8 9 | 1 2 3 |
| 8 9 7 | 3 1 2 | 5 6 4 |
| 3 1 2 | 6 4 5 | 9 7 8 |
-------------------------


Comments

26 Jan 2010 - 9:09am by Balazs (not verified)

Nice Job!

27 Jan 2010 - 12:10am by Abi (not verified)

you are a great man

28 Jan 2010 - 1:44am by Anonymous (not verified)

Funny and handy !

28 Jan 2010 - 5:55am by Anonymous (not verified)

good work

28 Jan 2010 - 9:13am by Bogdan (not verified)

very nice

2 Feb 2010 - 3:59am by Anonymous (not verified)

I want a program says if an array is aleady sorted

please help

9 Mar 2010 - 11:12pm by Anonymous (not verified)

Thanks

17 Mar 2010 - 11:25pm by Anonymous (not verified)

Thats nice

5 Apr 2010 - 4:48am by Anonymous (not verified)

That's a great code.. By the way is it optimal??

14 Apr 2010 - 5:46pm by Lolandrea (not verified)

I have one which uses "Las Vegas" algorithm... Is faster and is shorter.... :P

19 Apr 2010 - 11:21pm by waism (not verified)

please tell the basic of making the frames and buttons in java

19 Apr 2010 - 11:21pm by waism (not verified)

please tell the basic of making the frames and buttons in java

26 Apr 2010 - 2:57pm by can any 1 pls explain this for me pleas i am trying to learn (not verified)

i got this code form java2s web site saying that /Passing Objects to a Method
does it means that it has passed instant of the class as you can see class (Sphere)

class Sphere {
double radius; // Radius of a sphere

Sphere() {

}

// Class constructor
Sphere(double theRadius) {
radius = theRadius; // Set the radius

}

}

public class MainClass {
public static void main(String[] arg){
Sphere sp = new Sphere();

aMethod(sp);
}

private static void aMethod(Sphere sp){
System.out.println(sp);
}
}

23 Jun 2010 - 6:15am by Bruno (not verified)

Oi vc poderia me ajudar me ajude o programa é mais ou menos assim 2 form 1 com cadastro de produto que tera 3 label codigo , descriçao, preço, 3 caixa de text para os tres labels, um button gravar(aqui grava a tabela produto) e um button sair. No outro form cadastro de venda tera codigo da venda 1, 2 ou 3, a data, preço,quantidade e o total (ou seja multiplicando o preço pela quantidade de produtos aparecendo o valor na caixa de text total!!!) a mesma coisa do outro colocando os numeros nas ciaxas de text e a descriçaõ do produto entende? os produtos podem ser laranja, maça e melancia!! entao essas duas classes no caso né cadastro de produtoe cadastro de venda vai chamar essas duas tabelas no MYSQL T1:codigo, descrição, preço e T2: codigo venda , data da venda total entende? porque ai tem lá as tabelas no mysql para conecta com o java entende? me ajuda ai precisava para amanha!!!!! é porque só encontrei o site hoje!!!!

23 Jun 2010 - 6:44am by Bruno (not verified)

O PROGRAMA NÃO É NetBeans !

23 Jun 2010 - 6:45am by Bruno (not verified)

O PROGRAMA É NetBeans

29 Jul 2010 - 7:04am by Anonymous (not verified)

While(true)... aahh que coisa feia...

19 Oct 2010 - 11:44pm by Anonymous (not verified)

very very lengthy and not able to understand to beginers

30 Oct 2010 - 10:17pm by Anonymous (not verified)

U are not mentioned how to execute the program i.e how to pass the problems to the main program . so pls specify the required one

regards
satish

3 Nov 2010 - 12:18pm by Promotional Products (not verified)

I want to take this opportunity to say that I really love this blog. It has been a good resource of information for me in my research. Thank you so much admin.the herbal products are to be sale in discount interesting people can come to see.they provide the best services.

3 Nov 2010 - 12:19pm by Web Design (not verified)

This blog gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice blog.

5 Nov 2010 - 7:37pm by Ed hardy T (not verified)

If you go to the you can buy

9 Nov 2010 - 1:24am by Premium WordPress Themes (not verified)

I like this post. It really gives a good information to everyone who will read this article. I hope many of us can discover this one.

18 Nov 2010 - 6:24am by Anonymous (not verified)

Beautiful code.
so simple. so smart.

22 Nov 2010 - 9:33am by Gudz (not verified)

Handy and simple with understandable comments too!! Nice work!!

5 Dec 2010 - 7:17am by underfloor heating (not verified)

Thanks a lot. It’ll help me a lot.

5 Dec 2010 - 7:18am by underfloor heating (not verified)

Thanks a lot. It’ll help me a lot.

5 Dec 2010 - 12:14pm by Flooded Basemenet (not verified)

Excellent blog! I truly love how it’s easy on my eyes as well as the info are well written. I am wondering how I may be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day!

8 Dec 2010 - 1:10pm by Mario (not verified)

Hi! it works very good. And I also need a diagram in UML. Where can I find it?
Thanks!

16 Dec 2010 - 5:44pm by ron (not verified)

nice one dude!

25 Dec 2010 - 11:25am by Anonymous (not verified)

Great job!!!!

I develope in java for 10 years but your site is still helpfull!

7 Jan 2011 - 10:01pm by 0 Down Mortgages (not verified)

An excellent resource of information I will certainly return to check on the latest posts.

9 Jan 2011 - 6:11pm by electric underfloor heating (not verified)

I am so much excited after reading your blog. Your blog is very much innovative and much helpful for any industry as well as for person.

12 Jan 2011 - 6:53am by hair loss product (not verified)

Hi everybody. Interesting topic for a blog. I have been reading a lot of blogs and forums lately. I am choosing to add your website to my bookmarks so I can do it at another date. Good article.Thank you.

13 Jan 2011 - 1:43am by Industrial Furniture (not verified)

It's a very nice game. Their is need a creative mind to play this game. I have completed this game so many times. It's so nice game.
Industrial Furniture

18 Jan 2011 - 11:44am by forexrobot (not verified)

Nice game. I am sure a lot users will be interested in this.

23 Jan 2011 - 9:19am by uniqueguy (not verified)

Superb

25 Jan 2011 - 7:18am by A Lonely Man (not verified)

Good...

3 Feb 2011 - 11:17pm by car repair (not verified)

You have given to us with such an large collection of information. Great work you have done by sharing them to all!

11 Feb 2011 - 7:36pm by Negocios con futuro (not verified)

Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

14 Feb 2011 - 6:02pm by Solana Beach Carpet (not verified)

This is information I’ve been looking for some time,I will save it to favorites and let all my friends know about it. It’s been a pleasure to read something like this.

15 Feb 2011 - 10:35am by Papo (not verified)

Você quer papo

1 Mar 2011 - 6:52am by miller (not verified)

very good article,i like it.

3 Mar 2011 - 10:48pm by Anonymous (not verified)

Checked past and also discovered out that average auto on line stores don't continue unmissable 12v Prius batteries. Yesterday the auto would not start (no energize by most), just so today I took The old battery out as well as went in To The official Toyota sections retail outlet (into a divergent vehicle) as well as purchased the Recommended replacement battery. gorgeous standard replacement. The battery is smaller than a average automobile battery as well as it is stored in the trunk, besides that no issues.

3 Mar 2011 - 10:49pm by Anonymous (not verified)

Fantastic blog. Both my wife and I found it really interesting. I understand that I am being slightly impolite, but might I ask for a link to my web-sites oval lockets and sun stone earrings, I really would be grateful. My apologies if I've overstepped the mark here, if that is so please remove my remark and, again, I am truly sorry.

13 Mar 2011 - 5:52am by change your life (not verified)

It has been a good resource of information for me in my research. Thank you so much admin.

15 Mar 2011 - 12:52am by miller (not verified)

very good article,i like it.

16 Mar 2011 - 5:33pm by MaxiM (not verified)

hello i got some problems in your code it say that
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at SudokuSolver.main(SudokuSolver.java:7)

Process completed.

and for the Grid.java this problem

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.

i dunno whats the problem.. i just copy ur code thats all..
pls tell me the errors.. thx

29 Mar 2011 - 12:04pm by Yoshiblade (not verified)

I like the valuable info you provide in your articles. I will bookmark your weblog and check again here regularly. I am quite sure I will learn many new stuff right here!

31 Mar 2011 - 10:27am by change your life (not verified)

Also, a lot of readers are lazy, so it’s a good idea, as some have already said, to make sure you have good titles and good subheadings. Thanks for the great post

1 Apr 2011 - 1:54pm by Pension (not verified)

Great! I really enjoyed what you had to say. Keep going because you definitely bring a new voice to this subject. Not many people would say what you’ve said and still make it interesting. Well, at least I’m interested

13 Apr 2011 - 2:07am by high risk pregnancy (not verified)

Just want to say your article is as astonishing. The clarity to your publish is just cool and that i could assume you are an expert on this subject. Well with your permission let me to grasp your feed to keep updated with drawing close post. Thank you a million and please carry on the rewarding work.

16 Apr 2011 - 11:30am by Acai Berry Scams (not verified)

I wanted to write a quick note to express my thanks. I'm really impressed by a blog unique and perfectly chosen and organized!

27 Apr 2011 - 8:32am by mediation, (not verified)

Nice game. I am sure a lot users will be interested in this.

2 May 2011 - 11:04am by divorce mediation (not verified)

I am so much excited after reading your blog.

9 May 2011 - 7:39am by For Sale In Victorville (not verified)

you are a great man this side help the students.

18 May 2011 - 4:49am by Zenerect (not verified)

Once again great post. You seem to have a good understanding of these themes.When I entering your blog,I felt this . Come on and keep writting your blog will be more attractive. To Your Success!

19 May 2011 - 5:23pm by god (not verified)

I'm can't understand,It is really very difficult

3 Jun 2011 - 11:59pm by mona (not verified)

good job done. god bless you

4 Jun 2011 - 12:24pm by miami free legal consultation (not verified)

hi nice posting i really enjoyed it a lot i have bookmarked this page hope that u will provide that kind of nice information in future also thank you

7 Jun 2011 - 5:16am by Anonymous (not verified)

nice one a hey for ur talent man goodwork

18 Jun 2011 - 9:59pm by Anonymous (not verified)

nice

18 Jun 2011 - 10:01pm by Binod (not verified)

Very nice

28 Jun 2011 - 12:31am by Ashwini (not verified)

You have done a nice job

29 Jun 2011 - 5:41am by Anonymous (not verified)

/**
* @author Patrick Chan
* @version 1, 12/31/05
* @see SudokuSolver
*/
import java.io.*;
import java.util.*;

/**
* A Grid object holds the currently known values of the Sudoku puzzle.
* The grid consists of 9x9 cells that hold integer values from 0 to 9.
* A value of 0 indicates that the cell is empty.
*
* Each of the 81 cells has a location which is used to identify a
* specific cell. There are 81 locations, labelled location 0 to location 80.
* Cell locations are ordered from left-to-right and top-down.
* For example, location 0 refers to the top-leftmost cell.
* Location 8 refers to the top-righttmost cell.
* Location 71 refers to the bottom-leftmost cell.
* Location 80 refers to the bottom-rightmost cell.
*
* The grid consists of 9 columns, labelled column 0 to column 8.
* The grid consists of 9 rows, labelled row 0 to row 8.
*
* The grid consists of 9 subgrids, labelled subgrid 0 to subgrid 8.
* Each subgrid contains a subgrid of 3x3 cells.
* Subgrid 0 contains cells - 0, 1, 2, 9, 10, 11, 18, 19, 20.
* Subgrid 8 contains cells - 60, 61, 62, 69, 70, 71, 78, 79, 80
*/
public class Grid implements Cloneable {
// Array that contains values of all 81 cells in the grid.
int[] cells = new int[81];

// A set of bit-vectors that represent the known values for each column.
// Specifically, if column c contains the digits d1 and d2,
// colsSet[c] = 2^(d1-1)|2^(d2-1)
// For example, if column 0 contains the values 1 and 4, colsSet[0] = 9.
// The information in this variable is redundant with the information
// in the cells variable. The purpose of this variable is to reduce
// the cost of determining whether a particular digit can be set in
// a particular cell.
int[] colsSet = new int[9];

// This purpose and behavior of this variable is similar to colsSet.
int[] rowsSet = new int[9];

// This purpose and behavior of this variable is similar to colsSet.
int[] subgridSet = new int[9];

/**
* This method returns a grid of givens and empty cells ready to be solved.
* The cells containing givens have values between 1 and 9.
* Empty cells have the value 0.
*
* Characters are read one at a time from the input stream and placed
* into the grid in left-to-right and top-down order.
* - The characters 0 or . indicates an empty cell.
* - The characters 1 to 9 indicates a given.
* - The character # is used for comments; subsequent characters are
* ignored until a newline is encountered.
* - All other characters are simply ignored.
*
* @param rd Reader containing the givens
* @return null if there are not enough characters in 'rd' to form a grid.
*/
public static Grid create(Reader rd) throws Exception {
Grid grid = new Grid();

// Read until all 81 cells are filled
for (int loc=0; loc= 0 && ch != '\n' && ch != '\r') {
ch = rd.read();
}
} else if (ch >= '1' && ch <= '9') {
// A given
grid.set(loc, ch-'0');
loc++;
} else if (ch == '.' || ch == '0') {
// Empty cell
loc++;
}
}
return grid;
}

/*
* Finds an empty cell.
* @return the location of an empty cell or -1 if there are no empty cells.
* Values must be in the range [-1, 80].
*/
public int findEmptyCell() {
for (int i=0; i

20 Jul 2011 - 12:41am by deadbeat revolution (not verified)

The clarity to your publish is just cool and that i could assume you are an expert on this subject. Well with your permission let me to grasp your feed to keep updated with drawing close post. Thank you

21 Jul 2011 - 8:21am by bean bag (not verified)

It has been a good resource of information for me in my research....

21 Jul 2011 - 10:32am by bean bag (not verified)

It has been a good resource of information for me in my research...Well done

21 Jul 2011 - 9:17pm by airil (not verified)

Very informative. I love playing Sudoku and this presentation made everything clearer to me.

29 Jul 2011 - 7:24am by robert Umera (not verified)

This is so wonderful. Nice one!

31 Jul 2011 - 8:07am by auto power blogs 2.0 (not verified)

Hmmmm you post good but complicated.....

1 Aug 2011 - 11:18pm by canon digital camera (not verified)

This is what I have been searching in many websites and I finally found it here. Amazing article. I am so impressed. Could never think of such a thing is possible with it...I think you have a great knowledge especially while dealings with such subjects.

10 Aug 2011 - 6:45am by wowgold (not verified)

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can’t wait to read lots of your posts

19 Aug 2011 - 9:17am by poop bags (not verified)

So informative things are provided here,I really happy to read this post,I was just imagine about it and you provided me the correct information I really bookmark it,for further reading,So thanks for sharing the information.

20 Aug 2011 - 12:10am by travel insurance for pensioners (not verified)

Many people like to solve sudoku.You have done a great job solving in such an easy way.Everyone can understand what you did.I was thinking that why i do not think such way ?Because i am also looking to solve sudoku.

25 Aug 2011 - 1:33pm by Anonymous (not verified)

This is cool dude..... great work.

29 Aug 2011 - 4:14am by Vivek (not verified)

thats gr8 thought , keep it up dude....

29 Aug 2011 - 7:08am by Preemie Clothes (not verified)

Really needed those code man, great work, thanks.

29 Aug 2011 - 5:45pm by Anonymous (not verified)

.

10 Sep 2011 - 2:26am by Anonymous (not verified)

insufficent material to understand sudoku

10 Sep 2011 - 2:28am by Anonymous (not verified)

insufficent material to learn it

15 Sep 2011 - 8:41am by Brooklyn Staten Island Queens dentists (not verified)

What I want to know is why I should care? I mean, not to say that what you’ve got to say isn’t important, but I mean, it’s so generic. Everyone is just talking about this man. Give us something more, something that we can get behind so we can feel as passionately about it as you do.

25 Sep 2011 - 9:01pm by laxman (not verified)

its grt

30 Sep 2011 - 3:45am by pratikbutani (not verified)

VERY NICE,,,,

30 Sep 2011 - 12:23pm by handmade handbags (not verified)

Example Depot is very nice blog which bear much information.

7 Oct 2011 - 2:41am by lpn training (not verified)

I am stricken by the way you mastered this topic. It is not often I come across a web site with charming articles like yours. I will note your feed to keep up to date with your hereafter updates. I like it and do continue up the good work.

8 Oct 2011 - 6:06am by cna training (not verified)

It is great to have the opportunity to read a good quality article with useful information on topics that plenty are interested on. The points that the data stated are all first hand on actual experiences even help more. Go on doing what you do as we enjoy reading your work.

10 Oct 2011 - 2:44am by CNA certification (not verified)

Contrary to some ideas, correctly investigated articles still bring throughout testers like me. A person confirmed obvious idea of the niche problem as well as my personal ideas are actually complete subsequent studying distribute. Ensure that you preserve the seem operate and i’ll enroll in your current really simply syndication feed being well-informed from a growing posts.

10 Oct 2011 - 7:04am by Anonymous (not verified)

my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post.

11 Oct 2011 - 3:33am by CNA certification (not verified)

Hello, Someone said text messaging on your weblog and i also grew to be interested in the topic. I like your internet site

that i'm thinking no matter if I can make use of your words inside my operate? Would it be doable? If yes, make sure you

speak to with me at night.Many thanks.

13 Oct 2011 - 2:57am by rajani (not verified)

ur great

19 Nov 2011 - 8:07am by Anonymous (not verified)

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!pulse monitors

5 Dec 2011 - 4:28am by Anonymous (not verified)

Good. Thank you~

7 Dec 2011 - 3:44am by superstar RajniKanth (not verified)

this was piece of cake

13 Dec 2011 - 5:32am by cot mattress (not verified)

This is really a good site with great information along with excellent post for all to view and comment on this site.The information present here is helpful for the readers.Overall wonderful site.

15 Dec 2011 - 12:49pm by jeux la roulette (not verified)

This is what I Have Been Many websites and in searching I finally found it here. Amazing article. I am so impressed. Could never think of Such a thing is possible ... I think With It You have a great while Dealings with Knowledge Especially Such subjects.

16 Dec 2011 - 10:34pm by roulette e n ligne (not verified)

What a great article, I read all of it then i am satisfied to say that It’s a terse idiom, and if you’re concerned about the performance you must not have a database or anything else that consumes resources orders of magnitude greater. Consider yourself lucky, otherwise don’t optimize at this level unless you really need to. Keep blogging.

22 Dec 2011 - 5:00am by Anonymous (not verified)

Test

7 Jan 2012 - 10:36am by cheap wedding rings (not verified)

I certainly cheers you for the intel circulated here. I figured out the idea the topic now. Your webpage is really useful. I will love to take a while to browse your blog to receive further data. It is certainly practical to let your website to make money online thru enhancing it with a selection of advertising systems.

7 Jan 2012 - 11:59am by cheap wedding rings (not verified)

This is so wonderful. Nice one!

8 Jan 2012 - 12:52am by cheap wedding rings (not verified)

A very good and informative article indeed . It helps me a lot to enhance my knowledge, I really like the way the writer presented his views.

10 Jan 2012 - 11:07pm by chat rooms (not verified)

very good grup. super info

21 Jan 2012 - 3:39am by iPhone Broken (not verified)

We specialise in repairing out-of-warranty iPhones and other Apple portable devices.Over the last five years, we've repaired thousands of these devices with almost every fault imaginable.

21 Jan 2012 - 3:40am by iPhone Broken (not verified)

We specialise in repairing out-of-warranty iPhones and other Apple portable devices.Over the last five years, we've repaired thousands of these devices with almost every fault imaginable.

[url=www.fixxed.co.nz]iPhone Cracked[/url]
[url=www.fixxed.co.nz]iPhone Broken[/url]

25 Jan 2012 - 1:13pm by gratis bonus (not verified)

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I just want to emphasize the good work on this blog, has excellent views and a clear vision of what you are looking for...

28 Jan 2012 - 12:00am by Life insurance (not verified)

This is an article that takes a deeper looking into the best life insurance policies available for consumers. This article also does a basic comparision to help show consumers hidden benefits.

28 Jan 2012 - 1:04am by gokkast (not verified)

I have come here for the first time but i assure you that i would love to come here again and again, Because the information is so nice and helpful that i can't stop my self concentrating on it.Thank you for sharing I'm touched to be featured in your blog

7 Feb 2012 - 4:43am by Essay Writing Service (not verified)

This page is simply enlightening and enjoyable to check out. I discovered a whole lot through discovering this. Thank you for putting up awesome written content and then keep up the excellent posting great stuff.

8 Feb 2012 - 12:08pm by physical therapy spokane (not verified)

It’s really very informative post and I like it very much. I really want to share it with my friends.

9 Feb 2012 - 7:24am by craps (not verified)

Really i am impressed from this post....the person who created this post is a genious and knows how to keep the readers connected..thanks for sharing this with us.i found it informative and interesting. Looking forward for more updates.

12 Mar 2012 - 9:17am by Richmond Hill Condos (not verified)

In 1972 the average home price on the Toronto Real Estate Board was $32,513.

In 1985 the average home price on the Toronto Real Estate Board was $109,094.

In 1995 the average home price on the Toronto Real Estate Board was $203,028.

In 2006 the average home price on the Toronto Real Estate Board was $353,724.

Every decade the media said, “Prices have reached their peak, our children will not be able to

afford to buy homes.” They were wrong. No one knows how high the prices will go. But at the

minimum, they will follow the rate inflation. If the inflation rate stays at 4%, today’s average home

priced at $353,724 will sell for $523,597.93 in 10 years. Think that’s crazy? Look above! In 1972

everyone thought $32,513 was an outrageous price for a home in Toronto.

Real estate is a great long-term investment, and we should all buy more of it!
www.tonylai.ca

13 Mar 2012 - 10:49pm by poker spelen (not verified)

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!

20 Mar 2012 - 7:45am by escort website design (not verified)

Wow! It's awesome blog post here.... really very interesting for reading.....As programmers, it is our habit to put on our headphones and listen to our portable music players to drown out all of the noise from everyone else. The boss recently sent an email just to the programmers demanding. Thanks for sharing the informative post.

8 Apr 2012 - 2:11am by Ryan (not verified)

I feel strongly about it and enjoy understanding extra on this subject. If doable, as you gain experience, would you thoughts updating your weblog with a lot more info? It really is very useful for me.

14 Apr 2012 - 6:53am by wallpaper (not verified)

This article was really informative and I have learnt so much after reading this. I wonder some day I would be able to share such valueable information on my own blog.

14 Apr 2012 - 6:54am by wallpaper (not verified)

This article was really informative and I have learnt so much after reading this. I wonder some day I would be able to share such valueable information on my own blog.

15 Apr 2012 - 5:43am by Wallpaper (not verified)

This article was really informative and I have learn t so much after reading this. I wonder someday I would be able to share such valuable information on my own blog.

29 Apr 2012 - 12:09pm by James Riley (not verified)

Thank you for your usefull information.I like this kind of post which tell us much wonderfull massage.

1 May 2012 - 3:41am by Anonymous (not verified)

can anyone help with the error main class not found.

4 May 2012 - 11:49am by wie bekomme ich eine freundin (not verified)

wow great i have read many articles about this topic and everytime i learn something new i dont think it will ever stop always new info , Thanks for all of your hard work!

14 May 2012 - 2:23pm by monapandit74 (not verified)

I like the helpful info you provide in your articles I will bookmark your blog and check again here frequentlyI am quite certain I will learn a lot of new stuff right here! Good luck for the next!

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.