<%! public int[][] board = new int[9][9]; private int[] row = new int[9]; private int[] col = new int[9]; private int[][] box = new int[3][3]; private javax.servlet.jsp.JspWriter m_out; private boolean test(int x, int j) { return (x & (1< 0; } private int set(int x, int i) { return x | (1<"); for(int i=0; i<9; i++) { m_out.print(col[i] + " "); } m_out.println("
"); for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { m_out.print(box[i][j] + " "); } } m_out.println("

"); } private boolean doit(int x, int y) { if(x==9) return true; if(board[x][y] != 0) return doit(x+(y==8 ? 1 : 0), (y+1)%9); for(int i=0; i<9; i++) { if(!test(row[x], i) && !test(col[y], i) && !test(box[x/3][y/3], i)) { row[x] = set(row[x], i); col[y] = set(col[y], i); box[x/3][y/3] = set(box[x/3][y/3], i); if(doit(x+(y==8 ? 1 : 0), (y+1)%9)) { board[x][y] = i+1; return true; } row[x] = unset(row[x], i); col[y] = unset(col[y], i); box[x/3][y/3] = unset(box[x/3][y/3], i); } } return false; } public boolean solve() { for(int i=0; i<9; i++) { row[i] = 0; col[i] = 0; box[i/3][i%3] = 0; } for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { if(board[i][j] != 0) { row[i] |= 1<<(board[i][j]-1); col[j] |= 1<<(board[i][j]-1); box[i/3][j/3] |= 1<<(board[i][j]-1); } } } return doit(0,0); } public void clearBoard() { for(int i=0; i<9; i++) { row[i] = 0; col[i] = 0; box[i/3][i%3] = 0; for(int j=0; j<9; j++) { board[i][j] = 0; } } } %> Online Sudoku Solver Home

Online Sudoku Solver

Enter the values between 0 and 9.

<% boolean error = false; boolean solveThis = false; if(request.getParameter("solve") != null) { solveThis = true; for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { try { board[i][j] = Integer.parseInt(request.getParameter("board" + i + "" + j)); if(board[i][j] < 0 || board[i][j] > 9) { error = true; } } catch(Exception e) { board[i][j] = 0; error = true; } } } } else { clearBoard(); } if(error) { out.println("

Invalid Data Entered. Please enter values between 0 and 9."); } else if(solveThis) { boolean result = solve(); if(!result) { out.println("

No Result found for the given data

"); } } %>
<% for(int i=0; i<3; i++) { out.println(""); for(int j=0; j<3; j++) { out.println(""); } out.println(""); } %>
"); for(int si = 0; si<3; si++) { out.println(""); for(int sj=0; sj<3; sj++) { String id = "board" + (i*3+si) + "" + (j*3+sj); int tabIndex = (i*3+si)*9 + j*3 + sj + 1; out.println(""); } out.println(""); } out.println("