Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an implementation of the backtracking approach:</p> <pre><code>import java.util.Random; public class Sudoku { public static void main(String[] args) { Random rand = new Random(); int r = rand.nextInt(9); int c = rand.nextInt(9); int value = rand.nextInt(9) + 1; Board board = new Board(); board.set(r, c, value); System.out.println(board); solve(board, 0); System.out.println(board); } private static boolean solve(Board board, int at) { if (at == 9*9) return true; int r = at / 9; int c = at % 9; if (board.isSet(r, c)) return solve(board, at + 1); for (int value = 1; value &lt;= 9; value++) { if (board.canSet(r, c, value)) { board.set(r, c, value); if (solve(board, at + 1)) return true; board.unSet(r, c); } } return false; } static class Board { private int[][] board = new int[9][9]; private boolean[][] rs = new boolean[9][10]; private boolean[][] cs = new boolean[9][10]; private boolean[][][] bs = new boolean[3][3][10]; public Board() {} public boolean canSet(int r, int c, int value) { return !isSet(r, c) &amp;&amp; !rs[r][value] &amp;&amp; !cs[c][value] &amp;&amp; !bs[r/3][c/3][value]; } public boolean isSet(int r, int c) { return board[r][c] != 0; } public void set(int r, int c, int value) { if (!canSet(r, c, value)) throw new IllegalArgumentException(); board[r][c] = value; rs[r][value] = cs[c][value] = bs[r/3][c/3][value] = true; } public void unSet(int r, int c) { if (isSet(r, c)) { int value = board[r][c]; board[r][c] = 0; rs[r][value] = cs[c][value] = bs[r/3][c/3][value] = false; } } public String toString() { StringBuilder ret = new StringBuilder(); for (int r = 0; r &lt; 9; r++) { for (int c = 0; c &lt; 9; c++) ret.append(board[r][c]); ret.append("\n"); } return ret.toString(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload