Note that there are some explanatory texts on larger screens.

plurals
  1. POjava sudoku backtracking recursive
    primarykey
    data
    text
    <p>hey i have worte this program to solve sudoko but it just workd for a few cells of sudoku matrix and for other cells returns 0 . can u understand whats wrong with this? i am new in java coding and it really hurts not to be able to write a simple program.</p> <pre><code>public class sudoku { static int sud[][] = new int[9][9]; public static void main(String args[]) { for (int i = 0; i &lt; 9; i++) { for (int j = 0; j &lt; 9; j++) { sud[i][j] = 0; } } solve(sud); for (int i = 0; i &lt; 9; i++) { for (int j = 0; j &lt; 9; j++) { System.out.print(sud[i][j]); } System.out.print("\n"); } } public static boolean solve(int[][] sud) { for (int i = 0; i &lt; 9; i++) { for (int j = 0; j &lt; 9; j++) { if (sud[i][j] != 0) { continue; } for (int x = 1; x &lt; 10; x++) { if (!used(i, j, x)) { sud[i][j] = x; if (solve(sud)) { return true; } } } return false; } } return true; } public static boolean isinrow(int i, int j, int x) { for (int t = 0; t &lt; 9; t++) { if (sud[i][t] == x) { return true; } } return false; } public static boolean isincol(int i, int j, int x) { for (int t = 0; t &lt; 9; t++) { if (sud[t][j] == x) { return true; } } return false; } public static boolean isinsq(int sr, int sc, int x) { for (sr = 0; sr &lt; 3; sr++) { for (sc = 0; sc &lt; 3; sc++) { if (sud[sr][sc] == x) { return true; } } } return false; } static boolean used(int i, int j, int x) { if (!isinrow(i, j, x)) { if (!isincol(i, j, x)) { if (!isinsq(i - (i % 3), j - (j % 3), x)) { return false; } } } return true; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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