JAVA PROJECT
Sudoku is a popular logic puzzle that uses a 9 by 9 array of squares that are organized into 3 by 3 subarrays. The puzzle solver must fill in the squares with the digits 1 to 9 such that no digit is repeated in any row, any column, or any of the nine 3 by 3 subgroups of squares. Initially, some squares are filled in already and cannot be changed. For example, the following might be a starting configuration for a Sudoku puzzle:
Please find my code:
public class SolveSudoku {
private static final int N = 9;
public static void main(String[] args) {
int grid[][] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}};
if (solveSudoku(grid)) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}
else {
System.out.println("Sudoku can't be solved");
}
}
private static boolean solveSudoku(int[][] grid) {
RowColumn rowColumn = new RowColumn(0 , 0);
if (!findUnassignedLocation(grid, rowColumn)) {
return true;
}
for (int num = 1; num <= N; num++) {
if (isSafe(grid, rowColumn.row, rowColumn.column, num)) {
grid[rowColumn.row][rowColumn.column] = num;
if (solveSudoku(grid))
return true;
grid[rowColumn.row][rowColumn.column] = 0;
}
}
return false;
}
private static boolean isSafe(int[][] grid, int row, int column, int num) {
return !usedInRow(grid, row, num) &&
!usedInColumn(grid, column, num) &&
!usedInBox(grid, row - row%3, column - column%3, num);
}
private static boolean usedInBox(int[][] grid, int boxStartingRow, int boxStartingColumn, int num) {
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (grid[row + boxStartingRow][column + boxStartingColumn] == num)
return true;
}
}
return false;
}
private static boolean usedInColumn(int[][] grid, int column, int num) {
for (int row = 0; row < N; row++) {
if (grid[row][column] == num)
return true;
}
return false;
}
private static boolean usedInRow(int[][] grid, int row, int num) {
for (int column = 0; column < N; column++) {
if (grid[row][column] == num)
return true;
}
return false;
}
private static boolean findUnassignedLocation(int[][] grid, RowColumn rowColumn) {
for (rowColumn.row = 0; rowColumn.row < N; rowColumn.row++) {
for (rowColumn.column = 0; rowColumn.column < N; rowColumn.column++) {
if (grid[rowColumn.row][rowColumn.column] == 0) {
return true;
}
}
}
return false;
}
/*
* Inner class : so that the value of row and column as passed as a reference
* */
private static class RowColumn {
int row;
int column;
RowColumn(int row, int column) {
this.row = row;
this.column = column;
}
}
}
JAVA PROJECT Sudoku is a popular logic puzzle that uses a 9 by 9 array of...
Python Code. Sudoku is a puzzle where you're given a partially-filled 9 by 9 grid with digits. The objective is to fill the grid with the constraint that every row, column, and box (3 by 3 subgrid) must contain all of the digits from 1 to 9. Implement an efficient sudoku solver.
Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...
Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...
A Sudoku puzzle consists of a 9 x9 grid which holds the numbers 1-9 in each space.The objective is to fill the grid with digits so that each column, each row, and each of the nine 3x3 sub-grids that compose the grid contain all of the digits from 1 to 9 (with no repeats). You are to write a recursive method that will construct a valid solution to the puzzle and return that solution. You may construct as many additional...
Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here } int main() { int sudoku[9][9] = {{7, 3, 5, 6, 1, 4, 8, 9, 2}, {8, 4, 2, 9, 7, 3, 5, 6, 1}, {9, 6, 1, 2, 8, 5, 3, 7, 4}, {2, 8, 6, 3, 4, 9, 1, 5, 7}, {4, 1, 3, 8, 5, 7, 9, 2, 6}, {5, 7, 9, 1, 2, 6, 4, 3, 8}, {1, 5, 7, 4,...
Hi, I don't know how to deal with this python question. Modules are not recommended. Sudoku is a popular number puzzle that appears in many newspapers on a daily basis. The objective of the game (adopted from Wikipedia) is: "... to fill a 9x9 grid with digits so that each column, each row, and each of the nine 3x3 sub-grids that compose the grid contains all of the digits from I to 9. The puzzle setter provides a partially completed...
need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class. Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop() Test your class thoroughly before using it within the soduku program Part II. Create...
* Your goal in this exercise is to practice recursion and * to see how a properly written recursive solution can * take care of fairly complicated tasks with a few lines * of (well thought out) code. * * We will be solving Sudoku puzzles. In case you have never * solved or seen a Sudoku, you can learn more about them * here: * * https://en.wikipedia.org/wiki/Sudoku * * Your task if to write a function that takes an...
Multiples of 9’s (JAVA) Using the logic behind the algorithm in Part 1, it’s easy to calculate the first nine multiples of 9 (9x1, 9x2, 9x3…, 9x9). Let’s use two examples. First, to determine the product of 9 times 6, perform the following algorithm: Step 1: Subtract 1 from the number being multiplied by 9 (in this case 6) to get the first digit of the product: 6 – 1 = 5. Step 2: Subtract that digit from 9 to get...
Su Doku Problem 96 Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its origin is unclear, but credit must be attributed to Leonhard Euler who invented a similar, and much more difficult puzzle idea called Latin Squares. The objective of Su Doku puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid in such that each row, column, and 3 by 3 box contains each of the digits...