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 9187191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:42:30+00:00 2026-06-17T19:42:30+00:00

I already asked for help here, but nobody answer, and it really frustrating, I

  • 0

I already asked for help here, but nobody answer, and it really frustrating,
I am trying to write a code that solve a Sudoku in recursive function (backtracking),
here is the code:

#include <stdio.h>
#include <windows.h>

#define SIZE        9
#define SQUARE_SIZE 3

void print(int arrBoard[SIZE][SIZE])
{
     system("cls");
     int i, j;
     for (i = 0; i < SIZE; i++)
     {
         if (i % 3 == 0) printf("_ _ _ _ _ _ _ _ _ _ _ _\n");
         for (j = 0; j < SIZE; j++)
         {
             if (j % 3 == 0) printf("| ");
             printf("%d ", arrBoard[i][j]);
         }
         printf("\n");
     }

     Sleep(100);
}

BOOL IsValidRow(int arrBoard[SIZE][SIZE], int iNum, int iRow)
{
    int iCol;

    for (iCol = 0; iCol < SIZE; iCol++)
        if (arrBoard[iRow][iCol] == iNum) return FALSE;

    return TRUE;
}

BOOL IsValidCol(int arrBoard[SIZE][SIZE], int iNum, int iCol)
{
    int iRow;

    for (iRow = 0; iRow < SIZE; iRow++)
        if (arrBoard[iRow][iCol] == iNum) return FALSE;

    return TRUE;
}

BOOL IsValidSquare(int arrBoard[SIZE][SIZE], int iNum, int iRow, int iCol)
{
    iRow-= (iRow % SQUARE_SIZE);
    iCol-= (iCol % SQUARE_SIZE);

    int i, j;
    for(i = 0; i < SQUARE_SIZE; i++)
        for (j = 0; j < SQUARE_SIZE; j++)
            if (arrBoard[iRow + i][iCol + j] == iNum) return FALSE;

    return TRUE;
}

BOOL IsValid(int arrBoard[SIZE][SIZE], int iNum, int iRow, int iCol)
{
    if (IsValidRow(arrBoard, iNum, iRow) == FALSE) return FALSE;
    if (IsValidCol(arrBoard, iNum, iCol) == FALSE) return FALSE;
    if (IsValidSquare(arrBoard, iNum, iCol, iRow) == FALSE) return FALSE;

    return TRUE;
}

BOOL _Solve(int arrBoard[SIZE][SIZE], int iRow, int iCol)
{
    if (iRow == 9) return TRUE;

    if (arrBoard[iRow][iCol])
    {
        iRow = (iCol == 8) ? iRow + 1 : iRow;
        iCol = (iCol == 8) ? 0 : iCol + 1;

        if(_Solve(arrBoard, iRow, iCol)) return TRUE;
        return FALSE;
    }

    int iNewNum, iOldRow, iOldCol;
    for (iNewNum = 1; iNewNum <= SIZE; iNewNum++)
    {
        if (IsValid(arrBoard, iNewNum, iRow, iCol))
        {
            arrBoard[iRow][iCol] = iNewNum;
            print(arrBoard);
            iOldRow = iRow; iOldCol = iCol;
            iRow = (iCol == 8) ? iRow + 1 : iRow;
            iCol = (iCol == 8) ? 0 : iCol + 1;

            if (_Solve(arrBoard, iRow, iCol)) return TRUE;

            iRow = iOldRow; iCol = iOldCol;
            arrBoard[iRow][iCol] = 0;
        }
    }
    return FALSE;
}

int main(void)
{
    int arrBoard[SIZE][SIZE] =
    {0, 1, 0, 3, 6, 4, 0, 2, 0,
     6, 0, 0, 0, 0, 0, 0, 9, 7,
     0, 0, 0, 9, 0, 7, 0, 0, 0,
     0, 0, 6, 1, 0, 2, 8, 0, 0,
     1, 9, 8, 0, 0, 0, 0, 3, 2,
     0, 0, 7, 8, 0, 3, 9, 0, 0,
     0, 0, 0, 7, 0, 8, 0, 0, 0,
     4, 0, 0, 0, 0, 0, 0, 0, 3,
     0, 2, 0, 4, 5, 6, 0, 1, 0};

     _Solve(arrBoard, 0, 0);

    fflush(stdin);
    getchar();
    return 0;
}

its not the real code (just because the UI isnt important now).
so what I did here is to print the board every time it change any cell.
I saw one mistake while it tried to solve, when it reached to cell at row: 2 col: 6 (in index its [1][5]), it skip on the number 1 (which is the real answer), because it didnt try one, it cannot solve the rest of the sudoku.
i cant find out why it doesnt work.

  • 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-06-17T19:42:31+00:00Added an answer on June 17, 2026 at 7:42 pm

    You’ve already accomplished the first step, identifying where and when the problem occurs. With the help of a debugger, you can step through _Solve when iRow is 1 and iCol is 5. Here’s what I see, just looking at the first point when your problem occurs:

    • IsValid(arrBoard, iNewNum, iRow, iCol) returns FALSE.

    • Stepping into that method, IsValidSquare(arrBoard, iNum, iCol, iRow) is returning FALSE.

    • Looking again at that line, it seems a bit odd that you’ve got iCol before iRow, when your pattern elsewhere is row-then-column. Comparing the call to the declaration/definition, it looks like you’ve mixed up the parameters at the call-site.

    Fixing the order of the parameters for that call to IsValidSquare and re-running results in a solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already asked this question but at that time I tought that the refresh
I already asked a similar question, PE Header requirements , but I'm not really
I am sorry I have already asked this question on Superuser, but nobody answers
I am sorry, for I believe that this question has been asked already, but
Already asked but question was closed so here it is again: I need to
I know this question has already been asked and answered, but it didn't help
I already asked that as a secondary question in another thread, but then it's
This question is already asked but was still not answered. ( here ) I
This has already been asked Here , but not by me and the OP
I apologize for posting something that has already been asked here few times before,

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.