Sudoku Solving

Below is a fast algorithm that solves sudoku puzzles.

It assumes a NxN grid, where N = t^2 for some integer t. It will not TLE for t <=3.

Java
/******************************************************* *                                                     * *                    Lord Klotski                      * *                                                     * ********************************************************/ class zerg {    int a, b ; public zerg(int y,int z)    { a = y ; b = z ; } } public class SudokU {    static boolean[][][] x  ; // contains set of all possible points at [i][j] static int[][] y ; // length of the set of all possible points at [i][j] static int[][] arr ; static int numS ; static boolean allsolutions; static int N ; public static void init {        // normally I'd do tricky DP here to minimize the running time, but with N = 9 // I won't bother to do better than N^4 or N^5 (ish) for (int i = 0 ; i < N ; i ++) {            for (int j = 0 ; j < N ; j ++) {                if (arr[i][j] > 0) continue ; // for all spaces i,j // try all numbers 1...N                for (int k = 1 ; k < N+1 ; k ++) {                    // try to find it in a row or column boolean fail = false ; for (int l = 0 ; l < N ; l ++) {                        if (arr[i][l] == k || arr[l][j] == k) fail = true ; }                    // try to find it in the 3x3 square // assume N is a square number int s = 0; s = (int)Math.round(Math.sqrt(N)) ; int minx = (i/s)*s ; int miny = (j/s)*s ; for (int l = minx ; l <= minx + 2 ; l ++) for (int m = miny ; m <= miny + 2 ; m ++) if (arr[l][m] == k) fail = true ; if (!fail) {                        // point is valid x[i][j][k] = true ; y[i][j] ++ ; }                }             }         }     }     public static boolean solve(int blanks) {        if (blanks == 0) {            // print solution numS ++ ; printArr ; return true; }        int min = N+1; int minI = N+1 ; int minJ = N+1 ; for (int i = 0 ; i < N ; i ++) for (int j = 0 ; j < N ; j ++) {                if (min > y[i][j] && arr[i][j] == 0 && y[i][j] > 0) {                    min = y[i][j]; minI = i;                    minJ = j;                 } }        if (min == N+1) return false ; // greedily place at the spot with the fewest number of total outcomes; this maximises // the local probability that the point placed will be correct. int numSuccess = 0 ; // need numSuccess to be equal to 1 for a unique solution // 0 implies no solution // 2+ implies multiple solutions, which is also bad (means the problem sucks, lol) for (int k = 1 ; k < N+1 ; k ++) {            if (x[minI][minJ][k]) {                // idea is to place this value k into the unfinished sudoku puzzle and see if it                 // can be solved // need to find the array of points where it is currently possible to place k,                // and it won't be possible any more // setting any other cell's y[i][j] to be 0 is a contradiction, so we won't do that ArrayList h = new ArrayList ; for (int i = 0 ; i < N ; i ++) {                    if (x[minI][i][k]) {                        if (i == minJ) continue ; h.add(new zerg(minI,i)) ; }                    if (x[i][minJ][k]) {                        if (i == minI) continue ; h.add(new zerg(i,minJ)) ; }                }                 int s = (int)Math.round(Math.sqrt(N)) ; int minx = (minI/s)*s ; int miny = (minJ/s)*s ; for (int i = minx ; i <= minx+2 ; i ++) for (int j = miny ; j <= miny + 2 ; j ++) {                        if (i == minI || j == minJ) continue ; // already dealt with this case if (x[i][j][k]) h.add(new zerg(i,j)) ; }                // h now contains the set of all points that will be affected by a change // of the current cell (minI, minJ) from blank to 'k'                // check to see that none have y[i][j] == 1; this would imply a contradiction boolean f = false ; for (int i = 0 ; i < h.size ; i ++) {                    zerg t = h.get(i) ; if (y[t.a][t.b] == 1){f = true ;} }                if (f) continue ; // place K, update all arrays. Then unplace K and proceed as normal arr[minI][minJ] = k ; for (int i = 0 ; i < h.size ; i ++) {                    zerg t = h.get(i) ; y[t.a][t.b] -- ; x[t.a][t.b][k] = false ; }                int temp = y[minI][minJ] ; y[minI][minJ] = 0 ; boolean lulz = solve(blanks - 1) ; if (lulz) numSuccess ++ ; for (int i = 0 ; i < h.size ; i ++) {                    zerg t = h.get(i) ; y[t.a][t.b] ++ ; x[t.a][t.b][k] = true ; }                y[minI][minJ] = temp ; arr[minI][minJ] = 0; if (!allsolutions) if (numS >=1) return false ; }        }         if (numSuccess == 1) return true ; return false ; }    public static void printArr {        for (int i = 0 ; i < N ; i ++) {            for (int j = 0; j < N; j++) {                System.out.print(arr[i][j] + " "); }            System.out.println(""); }        return ; }    public static void main(String[] args) {        int debug = 0 ; allsolutions = false ; // set to true for all solutions, set to false for any solution Scanner input = new Scanner(System.in) ; // read in a 9X9 board, integers separated by spaces. 0 is blank, 1-9 is filled while (input.hasNextInt) {            numS = 0 ; N = input.nextInt ; x = new boolean[N][N][N+1] ; y = new int[N][N] ; arr = new int[N][N]; int blanks = 0 ; for (int i = 0 ; i < N ; i ++) for (int j = 0 ; j < N ; j ++) {                    arr[i][j] = input.nextInt ; if (arr[i][j] == 0) blanks ++ ; }            if (blanks == 0) // no work to be done {                System.out.println("Already solved"); continue ; }            init ; // time to solve System.out.println(""); System.out.println(""); boolean z = solve(blanks) ; System.out.println(""); System.out.println(""); }    } } It's essentially a branch-and-bound algorithm, but it averages less than 10000 positions evaluated per N=9 puzzle, and it can do well over 1 million positions per second on my computer.