Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I struggle with a problem of creating an algorithm which can solve a Super-sudoku. My Sudoku is 21x9 and consists of three 9x9 Sudokus (rows 1-9 it's first, 7-15 second and 12-21 thrid). I tried to write an algorithm but it doesn't solve sudoku properly. I think something is wrong in function that checks whether it's possible to insert the number(notValid()). Maybe someone can notice where I made a mistake.

What I have tried:

Here's what I tried to do:
C++
#include<stdio.h>
#include<algorithm>

bool checkCol(int workArray[21][9], short begin, short end, short checkingValue, short y){
      for (int i = begin; i < end; ++i){
            if (workArray[i][y] == checkingValue){
                  return false;
            }
      }
      return true;
}

bool notValid(int workArray[21][9], short value, short pointX, short pointY){
      short rows = (pointX / 3) * 3;
      short columns = (pointY / 3) * 3;
      for (int i = 0; i < 9; ++i){
            if (workArray[rows + (i % 3)][columns + (i / 3)] == value){
                  return false;
            }
      }
      for (int i = 0; i < 9; ++i){
            if (workArray[pointX][i] == value){
                  return false;
            }
      }
      if (pointX >= 0 && pointX <= 5){
            if (!checkCol(workArray, 0, 9, value, pointY) && !checkCol(workArray, 6, 15, value, pointY)/* &&
                    !checkCol(workArray, 12, 21, value, pointY)*/){
                  return false;
            }
      }
      else if (pointX >= 6 && pointX <= 8){
            if (!checkCol(workArray, 0, 9, value, pointY) && !checkCol(workArray, 6, 15, value, pointY) /*&&
                    !checkCol(workArray, 12, 21, value, pointY)*/){
                  return false;
            }
      }
      else if (pointX >= 9 && pointX <= 11){
            if (!checkCol(workArray, 6, 15, value, pointY) && !checkCol(workArray, 0, 9, value, pointY) &&
                    !checkCol(workArray, 12, 21, value, pointY)){
                  return false;
            }
      }
      else if (pointX >= 12 && pointX <= 14){
            if (!checkCol(workArray, 12, 21, value, pointY) && !checkCol(workArray, 6, 15, value, pointY)/* &&
                    !checkCol(workArray, 0, 9, value, pointY)*/){
                  return false;
            }
      }
      else if (pointX >= 15 && pointX <= 20){
            if (/*!checkCol(workArray, 6, 15, value, pointY) &&*/ !checkCol(workArray, 12, 21, value, pointY)){
                  return false;
            }
      }
      return true;
}

bool findEmptySpace(int workArray[21][9], short &pointX, short &pointY);

bool solveTheSudoku(int workArray[21][9]){
      short pointX, pointY;
      if (!findEmptySpace(workArray, pointX, pointY)){
            return true;
      }
      for (int i=1; i<=9; ++i){
            if (notValid(workArray, i, pointX, pointY)){
                  workArray[pointX][pointY] = i;
                  if (solveTheSudoku(workArray)){
                        return true;
                  }
                  workArray[pointX][pointY] = 0;
            }
      }
      return false;
}

bool findEmptySpace(int workArray[21][9], short &pointX, short &pointY){
      for (pointX = 20; pointX >= 0; --pointX){
            for (pointY = 8; pointY >= 0; --pointY){
                  if (workArray[pointX][pointY] == 0){
                        return true;
                  }
            }
      }
      return false;
}

void printSudoku(int sudoku[21][9]){
      for (int i = 0; i < 21; ++i){
            for (int j = 0; j < 9; ++j){
                  printf("%d ",sudoku[i][j]);
            }printf("\n");
      }
}

int main(){
      int sudoku[21][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 9},
                           {4, 7, 0, 0, 0, 0, 0, 0, 0},
                           {0, 0, 0, 5, 6, 2, 0, 0, 3},
                           {0, 6, 0, 0, 0, 0, 0, 0, 0},
                           {0, 0, 4, 0, 0, 3, 0, 6, 0},
                           {0, 5, 9, 0, 0, 0, 0, 0, 0},
                           {0, 0, 0, 2, 0, 0, 0, 0, 0},
                           {6, 0, 0, 4, 0, 0, 0, 0, 0},
                           {0, 4, 8, 0, 0, 0, 6, 0, 0},
                           {0, 0, 4, 0, 0, 0, 0, 0, 0},
                           {0, 2, 0, 0, 0, 0, 1, 0, 0},
                           {0, 9, 1, 0, 0, 4, 0, 0, 5},
                           {0, 0, 0, 0, 0, 0, 0, 0, 0},
                           {4, 0, 0, 6, 0, 0, 0, 0, 5},
                           {5, 0, 0, 0, 0, 0, 8, 6, 0},
                           {0, 0, 0, 0, 8, 7, 0, 2, 1},
                           {0, 1, 0, 0, 0, 3, 0, 8, 4},
                           {0, 2, 0, 0, 0, 0, 3, 0, 0},
                           {8, 0, 0, 3, 0, 0, 6, 4, 2},
                           {0, 4, 0, 7, 0, 0, 5, 0, 8},
                           {3, 0, 0, 4, 2, 0, 0, 7, 0}};
      //int arrayTmp[21][9];
      //std::copy(sudoku, sudoku+21, arrayTmp);
      if (solveTheSudoku(sudoku)){
            printSudoku(sudoku);
      }
      else{
            printf("No solution find\n");
            printSudoku(sudoku);
      }
      return 0;
}
Posted
Updated 3-May-17 0:03am
v4
Comments
enhzflep 29-Apr-17 7:51am    
For what it's worth - there's an article here at CodeProject that uses a webcam to look for images of a Sudoku puzzle. Once located, the code solves the puzzle and superimposes the solution over the top of the live video feed.
Perhaps you'd get some mileage from that article. You can find it here: Realtime Webcam Sudoku Solver
Patrice T 29-Apr-17 8:44am    
Is it only 3 sudoku or is there a link between them ?
weegeee 29-Apr-17 9:25am    
Yes, they are related. These three Sudoku overlap and together form one Super-Sudoku.
Patrice T 29-Apr-17 10:33am    
Give details on how they overlap.
Use Improve question to update your question.
So that everyone can pay attention to this information.
Patrice T 29-Apr-17 11:45am    
Can you explain how is supposed to work your code?
Your actual code is surprising short.

Just dumping your code on us and saying "I think something is wrong in function that checks whether it's possible to insert the number" doesn;t help - we have no idea what algorithm you are trying to use, or how your code is supposed to work.

Development is not a case of "write it, compile it, fix compiler errors, compile it, ship it" - just compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using teh debugger to find out why. Put a breakpoint on your line:
C#
myaverage.DisplayAverage();

and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

So give it a try: learn to use the debugger on a relatively trivial example like this before you are faced with a bug in a 200,000 line lump of code someone else wrote the day he left the company!
 
Share this answer
 
Quote:
These smaller Sudokus overlap: rows 1-9 are first Sudoku, but rows 7-9 are also part of second Sudoku (the whole is 7-15 rows). The analogy is in case of the third Sudoku, the three last rows (12-15) are parts of the third Sudoku (rows 12-21).

As I understand, you have 5 3 Sudoku that are at rows 0-8, 3-11, 6-14, 9-17, 12-20.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
Quote:
It's using backtracking. Starting from a bottom it tries to solve the Sudoku. If there is a empty place I check whether the number is fit and enter it, if not fit I return to the previous entered number.

This is a brut force attack, it is highly inefficient. Human like solving algorithm is way more efficient.
 
Share this answer
 
v4
Comments
weegeee 30-Apr-17 16:01pm    
First of all I want my algorithm to work properly, but I still have a problem with right values in the columns (the numbers are duplicate).
Patrice T 30-Apr-17 17:22pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Try this:
#include<stdio.h>
#include<algorithm>

bool checkCol(int workArray[21][9], short begin, short end, short checkingValue, short y){
      for (int i = begin; i < end; ++i){
            if (workArray[i][y] == checkingValue){
                  return false;
            }
      }
      return true;
}

bool notValid(int workArray[21][9], short value, short pointX, short pointY){
      short rows = (pointX / 3) * 3;
      short columns = (pointY / 3) * 3;
      for (int i = 0; i < 9; ++i){
            if (workArray[rows + (i % 3)][columns + (i / 3)] == value){
                  return false;
            }
      }
      for (int i = 0; i < 9; ++i){
            if (workArray[pointX][i] == value){
                  return false;
            }
      }
      if (pointX >= 0 && pointX <= 5){
            if (!checkCol(workArray, 0, 9, value, pointY) && !checkCol(workArray, 6, 15, value, pointY)/* &&
                    !checkCol(workArray, 12, 21, value, pointY)*/){
                  return false;
            }
      }
      else if (pointX >= 6 && pointX <= 8){
            if (!checkCol(workArray, 0, 9, value, pointY) && !checkCol(workArray, 6, 15, value, pointY) /*&&
                    !checkCol(workArray, 12, 21, value, pointY)*/){
                  return false;
            }
      }
      else if (pointX >= 9 && pointX <= 11){
            if (!checkCol(workArray, 6, 15, value, pointY) && !checkCol(workArray, 0, 9, value, pointY) &&
                    !checkCol(workArray, 12, 21, value, pointY)){
                  return false;
            }
      }
      else if (pointX >= 12 && pointX <= 14){
            if (!checkCol(workArray, 12, 21, value, pointY) && !checkCol(workArray, 6, 15, value, pointY)/* &&
                    !checkCol(workArray, 0, 9, value, pointY)*/){
                  return false;
            }
      }
      else if (pointX >= 15 && pointX <= 20){
            if (/*!checkCol(workArray, 6, 15, value, pointY) &&*/ !checkCol(workArray, 12, 21, value, pointY)){
                  return false;
            }
      }
      return true;
}

bool findEmptySpace(int workArray[21][9], short &pointX, short &pointY);

bool solveTheSudoku(int workArray[21][9]){
      short pointX, pointY;
      if (!findEmptySpace(workArray, pointX, pointY)){
            return true;
      }
      for (int i=1; i<=9; ++i){
            if (notValid(workArray, i, pointX, pointY)){
                  workArray[pointX][pointY] = i;
                  if (solveTheSudoku(workArray)){
                        return true;
                  }
                  workArray[pointX][pointY] = 0;
            }
      }
      return false;
}

bool findEmptySpace(int workArray[21][9], short &pointX, short &pointY){
      for (pointX = 20; pointX >= 0; --pointX){
            for (pointY = 8; pointY >= 0; --pointY){
                  if (workArray[pointX][pointY] == 0){
                        return true;
                  }
            }
      }
      return false;
}

void printSudoku(int sudoku[21][9]){
      for (int i = 0; i < 21; ++i){
            for (int j = 0; j < 9; ++j){
                  printf("%d ",sudoku[i][j]);
            }printf("\n");
      }
}

int main(){
      int sudoku[21][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 9},
                           {4, 7, 0, 0, 0, 0, 0, 0, 0},
                           {0, 0, 0, 5, 6, 2, 0, 0, 3},
                           {0, 6, 0, 0, 0, 0, 0, 0, 0},
                           {0, 0, 4, 0, 0, 3, 0, 6, 0},
                           {0, 5, 9, 0, 0, 0, 0, 0, 0},
                           {0, 0, 0, 2, 0, 0, 0, 0, 0},
                           {6, 0, 0, 4, 0, 0, 0, 0, 0},
                           {0, 4, 8, 0, 0, 0, 6, 0, 0},
                           {0, 0, 4, 0, 0, 0, 0, 0, 0},
                           {0, 2, 0, 0, 0, 0, 1, 0, 0},
                           {0, 9, 1, 0, 0, 4, 0, 0, 5},
                           {0, 0, 0, 0, 0, 0, 0, 0, 0},
                           {4, 0, 0, 6, 0, 0, 0, 0, 5},
                           {5, 0, 0, 0, 0, 0, 8, 6, 0},
                           {0, 0, 0, 0, 8, 7, 0, 2, 1},
                           {0, 1, 0, 0, 0, 3, 0, 8, 4},
                           {0, 2, 0, 0, 0, 0, 3, 0, 0},
                           {8, 0, 0, 3, 0, 0, 6, 4, 2},
                           {0, 4, 0, 7, 0, 0, 5, 0, 8},
                           {3, 0, 0, 4, 2, 0, 0, 7, 0}};
      //int arrayTmp[21][9];
      //std::copy(sudoku, sudoku+21, arrayTmp);
      if (solveTheSudoku(sudoku)){
            printSudoku(sudoku);
      }
      else{
            printf("No solution find\n");
            printSudoku(sudoku);
      }
      return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900