Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search. I'm beginner programmer and I've challenged myself to write sudoku solver in C.
I have finished it in few days and now I want to make it faster. With this sample input execution time is 1. Does anyone know how can I make this faster? Would it be faster if I used recursion? And also how can I measure real execution time? Instead of having checkRow , checkCol and checkSquare , I suggest you allocate a simple char array for each row, column and minisquare:.
You initialize each visited[0] , Next, when you put a number in an empty cell, you mark it in three such "sets": current column, current row and current minisquare. The idea is as follows. You march through the board rows, each row from left to write.
You leave the cells that have a predefined value as is, but you put 1 to the first empty cell, and recur to the next cell. If 1 does not belong, you try 2 , 3 and so on. Once you have found a valid value, move to the next empty cell. At some point it might happen than none of the numbers fit.
In such a case you go one step backwards backtracking and try increment the previous value. If you can translate from Java, see this, starting from line So you can put all the number that has only one possibility. In the sudoku. Here's my code: I think it's the best. Shlomo Gottlieb Shlomo Gottlieb 1 1 silver badge 9 9 bronze badges.
I am a recursion fan, and very much like your code. Excellent brevity, quite readable. I did find one small glitch however MOEN, you're right about the glitch, it was just homework so we had to assume the input is valid.
Harjinder Harjinder 11 2 2 bronze badges. This is the simplest way I could think of to do this. No activation records required. Could you please 1 properly indent your code, 2 use meaningful variable names rather than o , oo , ooo etc and 3 add some explanations to help the reader understand what your code is doing and how it's actually solving Sudokus?
Then the number in the cell is checked for duplicates in it's respective 3X3 grid. If it works, the number is added into the cell and i or j is incremented for the next cell. If the number does not fit, it is deleted and i or j is decremented to the previous cell and incremented by If no solution exists, the screen hangs. I had to totally understand what I was trying to do before I wrote the program.
This method is how I was able to solve this problem. Thanks for the explanations. Could you edit that into your answer? Testing Testing 2 2 silver badges 11 11 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. For example, since 1,2,4,6,7, and 9 are not possible numbers for square.
This allows my solve function to use human knowledge to solve the board. Obviously if the number 2 evaluates true in only one square on a single row then you can fill that number in. Likewise you can repeat for each column and block. The only downside to this is after going through all the rows and filling in some numbers you have to update your isPossibleValues[81][9]. That is what my checkPossibles Board function does. To allow some efficiency my checkPossibles will also fill in any numbers where there is only one possible value there.
It can get complicated which is why I recommend writing a good algorithm. However this could easily be modified to run colored in linux. Below is just the solve function of my program.
0コメント