Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Sudoku Solver

#include <iostream>

#include <vector>

using namespace std;

bool isValid(vector < vector < char >> & board, int row, int col, char c) {
  for (int i = 0; i < 9; i++) {
    if (board[i][col] == c)
      return false;

    if (board[row][i] == c)
      return false;

    if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c)
      return false;
  }
  return true;
}

bool solveSudoku(vector < vector < char >> & board) {
  for (int i = 0; i < board.size(); i++) {
    for (int j = 0; j < board[0].size(); j++) {
      if (board[i][j] == '.') {
        for (char c = '1'; c <= '9'; c++) {
          if (isValid(board, i, j, c)) {
            board[i][j] = c;

            if (solveSudoku(board))
              return true;
            else
              board[i][j] = '.';
          }
        }

        return false;
      }
    }
  }
  return true;
}
int main() {
    vector<vector<char>>board{
        {'9', '5', '7', '.', '1', '3', '.', '8', '4'},
        {'4', '8', '3', '.', '5', '7', '1', '.', '6'},
        {'.', '1', '2', '.', '4', '9', '5', '3', '7'},
        {'1', '7', '.', '3', '.', '4', '9', '.', '2'},
        {'5', '.', '4', '9', '7', '.', '3', '6', '.'},
        {'3', '.', '9', '5', '.', '8', '7', '.', '1'},
        {'8', '4', '5', '7', '9', '.', '6', '1', '3'},
        {'.', '9', '1', '.', '3', '6', '.', '7', '5'},
        {'7', '.', '6', '1', '8', '5', '4', '.', '9'}
    };
   
    solveSudoku(board);
        	
    for(int i= 0; i< 9; i++){
        for(int j= 0; j< 9; j++)
            cout<<board[i][j]<<" ";
            cout<<"
";
    }
    return 0;
}
Comment

Sudoku solver


/*
	This is an implementation that demonstrates
	how to solve a partially filled 9*9 Sudoku board. 
	

	Time complexity: O(1) 
	Space complexity: O(1)
*/
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

public class SudokuSolver {

	public static void main(String[] args) {
		ArrayList<ArrayList<Integer>> board = new ArrayList<>();
		board.add(new ArrayList<>(Arrays.asList(7, 8, 0, 4, 0, 0, 1, 2, 0)));
		board.add(new ArrayList<>(Arrays.asList(6, 0, 0, 0, 7, 5, 0, 0, 9)));
		board.add(new ArrayList<>(Arrays.asList(0, 0, 0, 6, 0, 1, 0, 7, 8)));
		board.add(new ArrayList<>(Arrays.asList(0, 0, 7, 0, 4, 0, 2, 6, 0)));
		board.add(new ArrayList<>(Arrays.asList(0, 0, 1, 0, 5, 0, 9, 3, 0)));
		board.add(new ArrayList<>(Arrays.asList(9, 0, 4, 0, 6, 0, 0, 0, 5)));
		board.add(new ArrayList<>(Arrays.asList(0, 7, 0, 3, 0, 0, 0, 1, 2)));
		board.add(new ArrayList<>(Arrays.asList(1, 2, 0, 0, 0, 7, 4, 0, 0)));
		board.add(new ArrayList<>(Arrays.asList(0, 4, 9, 2, 0, 6, 0, 0, 7)));
		board = solveSudoku(board);
		for (List<Integer> row : board)
			System.out.println(row);
		/*
		 * The above outputs:
		 * [7, 8, 5, 4, 3, 9, 1, 2, 6]
		 * [6, 1, 2, 8, 7, 5, 3, 4, 9]
		 * [4, 9, 3, 6, 2, 1, 5, 7, 8]
		 * [8, 5, 7, 9, 4, 3, 2, 6, 1]
		 * [2, 6, 1, 7, 5, 8, 9, 3, 4]
		 * [9, 3, 4, 1, 6, 2, 7, 8, 5]
		 * [5, 7, 8, 3, 9, 4, 6, 1, 2]
		 * [1, 2, 6, 5, 8, 7, 4, 9, 3]
		 * [3, 4, 9, 2, 1, 6, 8, 5, 7]
		 */
	}

	private static ArrayList<ArrayList<Integer>> solveSudoku(ArrayList<ArrayList<Integer>> board) {
		solveSudokuRec(board, 0, 0);
		return board;
	}

	private static boolean solveSudokuRec(ArrayList<ArrayList<Integer>> board, int row, int col) {
		if (col == board.get(row).size()) {
			row += 1;
			col = 0;
			if (row == board.size()) {
				return true;
			}
		}

		if (board.get(row).get(col) == 0) {
			return tryDigitsAtPosition(board, row, col);
		}

		return solveSudokuRec(board, row, col + 1);
	}

	private static boolean tryDigitsAtPosition(ArrayList<ArrayList<Integer>> board, int row, int col) {
		for (int digit = 1; digit < 10; digit++) {
			if (isValidAtPosition(digit, board, row, col)) {
				board.get(row).set(col, digit);
				if (solveSudokuRec(board, row, col + 1))
					return true;
			}
		}

		board.get(row).set(col, 0);
		return false;
	}

	private static boolean isValidAtPosition(int digit, ArrayList<ArrayList<Integer>> board, int row, int col) {
		boolean isRowValid = !board.get(row).contains(digit);
		boolean isColumnValid = true;

		for (int rowIdx = 0; rowIdx < board.size(); rowIdx++) {
			if (board.get(rowIdx).get(col) == digit) {
				isColumnValid = false;
			}
		}

		if (!isRowValid || !isColumnValid) {
			return false;
		}

		int subgridRowStart = (row / 3) * 3;
		int subgridColStart = (col / 3) * 3;

		for (int rowIdx = 0; rowIdx < 3; rowIdx++) {
			for (int colIdx = 0; colIdx < 3; colIdx++) {
				int rowToCheck = subgridRowStart + rowIdx;
				int colToCheck = subgridColStart + colIdx;
				int existingValue = board.get(rowToCheck).get(colToCheck);

				if (existingValue == digit) {
					return false;
				}
			}
		}

		return true;
	}

}
Comment

solve the sudoku


//User function Template for Java

class Solution
{
    static boolean isSafe(int[][] grid,int row,int col,int v){
      for(int i=0;i<9;i++){
          if(grid[i][col]==v)return false;
          if(grid[row][i]==v)return false;
          if(grid[3*(row/3)+i/3][3*(col/3)+i%3]==v)return false;
      }
      return true;
  }
    //Function to find a solved Sudoku. 
    static boolean SolveSudoku(int grid[][])
    {
        // add your code here
        for(int i=0;i<9;i++){
          for(int j=0;j<9;j++){
              if(grid[i][j]==0){
                  for(int v = 1;v<=9;v++){
                      if(isSafe(grid,i,j,v)){
                          grid[i][j]=v;
                          if(SolveSudoku(grid)){
                              return true;
                          }
                          else{
                              grid[i][j]=0;
                          }
                      }
                  }
                  return false;
              }
          }
      }
      return true;
    }
    
    //Function to print grids of the Sudoku.
    static void printGrid (int grid[][])
    {
        // add your code here
        for(int i=0;i<9;i++){
          for(int j=0;j<9;j++){
              System.out.print(grid[i][j]+" ");
          }
         }
    }
}
Comment

Sudoku Solver

class Solution {
public:
    void solveSudoku(vector<vector<char>>& board) {
        
    }
};
Comment

Sudoku Solver

class Solution {
    public void solveSudoku(char[][] board) {
        
    }
}
Comment

Sudoku Solver



void solveSudoku(char** board, int boardSize, int* boardColSize){

}
Comment

Sudoku Solver

public class Solution {
    public void SolveSudoku(char[][] board) {
        
    }
}
Comment

Sudoku Solver

/**
 * @param {character[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var solveSudoku = function(board) {
    
};
Comment

Sudoku Solver

# @param {Character[][]} board
# @return {Void} Do not return anything, modify board in-place instead.
def solve_sudoku(board)
    
end
Comment

Sudoku Solver

class Solution {
    func solveSudoku(_ board: inout [[Character]]) {
        
    }
}
Comment

Sudoku Solver

class Solution {

    /**
     * @param String[][] $board
     * @return NULL
     */
    function solveSudoku(&$board) {
        
    }
}
Comment

Sudoku Solver

/**
 Do not return anything, modify board in-place instead.
 */
function solveSudoku(board: string[][]): void {

};
Comment

PREVIOUS NEXT
Code Example
Java :: java convert date to localdate 
Java :: java create unmodifiable list 
Java :: javafx main class 
Java :: java extract zip 
Java :: findviewbyid in kotlin Just using id name . 
Java :: testing the web layer without authentication spring 
Java :: iterate through an arraylist java 
Java :: android:windowLightStatusBar programmatically 
Java :: java delete column from csv 
Java :: android studio change launcher icon 
Java :: how to update java on windows 10 
Java :: Java NoClassDefFoundError but class is there 
Java :: glide library in android studio 
Java :: this in java 
Java :: diamond star pattern in java 
Java :: java binart tree print 
Java :: set textview text android java 
Java :: loop hash map 
Java :: java printf tab 
Java :: Access items from the ArrayList using get() function 
Java :: palindrome find in java 
Java :: how to save a string to a text file 
Java :: how to scan as a letter in java 
Java :: java vector push_back 
Java :: java list to array 
Java :: check if first character of a string is a number java 
Java :: Java Longest String In String Array 
Java :: string methods in java 
Java :: java base64 to file 
Java :: current time stamp android java 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =