Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:43:04+00:00 2026-05-30T14:43:04+00:00

Possible Duplicate: Sudoku solver in java, using backtracking and recursion I am creating a

  • 0

Possible Duplicate:
Sudoku solver in java, using backtracking and recursion

I am creating a program that will solve a sudoku using recursion and brute force. My key problem is that I do not understand how I could concievably make it backtrack one it gets stuck.

The general algorithm of the program is the following:

  1. Find the number of zeros in the sudoku.

  2. In the location of the first 0 (getNextEmpty method does this), insert a number(insertnumber checks to make sure a value complies with sudoku rules and returns true if it does).

  3. Then I make a recursive call, end when there are no more zeroes (n is the number of zeros).

  4. If the program reaches a point that it gets stuck, I must backtrack to change a piece. But how is this possible?

The Cell class actually holds the location of the cell to be adjusted in an array of the format [row, column]. It has methods to return the row, column, or smaller grid associated with that cell.

I am not asking for hand-holding or all the code, just a nudge in the right direction will suffice as I am legitimately interested in understanding the recursion.

public static int[][] getSolution(int[][] grid) {
    for (int i = 0; i < 9; i++) {
        System.arraycopy(grid[i], 0, SolveSudoku.grid[i], 0, 9);
    }// end for
    int n = getZeroes();
    return getSolution(n);
}//end getSolution

private static int[][] getSolution(int n) {
    if (n == 0) {
        return grid;        
    }//end if
    Cell cell = getNextEmpty();
    boolean fits = false;
    for (int i = 0; i <= 9; i++) {
        fits = insertNumber(cell, i);
        if (fits) {
            break;
        }else {
            //I do not understand what I should do here
        }
    }//end for
    return getSolution(n - 1);
}//end getSolution
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:43:05+00:00Added an answer on May 30, 2026 at 2:43 pm

    A nudge in the right direction. Your approach needs a little tweaking since you’re not keeping track of all the information you need to solve the grid.

    private static int[][] getSolution(int n) {
        if (n == 0) {
           return grid;        
        }//end if
    
        Cell cell = getNextEmpty();
        boolean fits = false;
        for (int i = 0; i <= 9; i++) {
            fits = insertNumber(cell, i);
            if (fits) {
                break; // means i fits in Cell
            } else {
                // i doesn't fit... try the next i
                // don't need to do anything
            }
        }//end for
        if (!fits) {
            // There are no numbers that fit in this Cell
            // What should happen?
            // Did I make a bad guess?
            // How do I BACKTRACK and correct a previous guess?
        }
        return getSolution(n - 1);
    }//end getSolution
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: What are the stages of compilation of a C++ program? I find
Possible Duplicate: How do I make a list with checkboxes in Java Swing? I
Possible Duplicate: How can I increment a date by one day in Java? I
Possible Duplicate: How to rename java.exe/javaw.exe process? Hi, I am working on java desktop
Possible Duplicate: C# early and late binding I want to ask that When does
Possible Duplicate: C++ templates that accept only certain types For example, if we want
Possible Duplicate: XPath: How to match attributes that contain a certain string I'm trying
Possible Duplicate: A Singleton that is not globally accessible Do you know a good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.