Hi , I have some problem with my code. My intention is to check whether that number in that column and row is unique and later on print the unique number in the 9 x 9 matrix. I initialized the array a[i][j]=0 first and later on my intention is to place numbers from 1 to 9 in each cell provided that number is unique in that column and row. But here the problem is-- the loop which I numbered as 1 here for identification keeps on iterating even if I get unique number. So that the number is getting replaced in a[i][j]. So, is there any technique like breaking the loop of one method from another method? Or any other solution? Please suggest. Thanks in advance.
I am not surprised you have problems. You are nesting loops 5 ddep. That is surely unnecessary. That looks as if you had simply written code without working out what you need to write.
What is the algorithm you are using? That is the important thing. Write it out on paper and then post ti, so we can see what is happening.
What I did was to start by adding code tags, which you haven’t seen before, and deleting some empty lines, and you can see how much easier it is to read your code
But I am not going to try to work out what the code is doing.
There are all sorts of ways to work out whether your numbers are unique. Let’s see what you have got, then we can think of some other suggestions.
If you want to see whether you are looping endlessly, I suggest you put some print statements into your loops. Print i, j, etc. If you start with the outermost loop, you ought to get 012345678. In the second loop you should get 012345678012345678012345678012345678012345678012345678012345678012345678012345678. In the third loop you should get eyestrain
anoopkumar anoo wrote:I initialized the array a[i][j]=0 first and later on my intention is to place numbers from 1 to 9 in each cell provided that number is unique in that column and row.
OK, so this sounds part of a solution for something like Sudoku.
First off, let me congratulate you on splitting out this "can I place" problem into an module of its own - well done - because it has nothing whatsoever to do with the rest of the game.
However, your code is way over the top.
Think about it this way: You have a number, n, that you want to place in a position, r,c, such that it is unique in both row r and column c.
My advice: Forget about the entire matrix and concentrate on the problem.
In order to work out if you can place it in a row, the only thing you need to do is check the other values in that row (in your case a maximum of 8) - there's absolutely no need to check the entire matrix - and likewise for a column.
For a row, you have a ready-made solution: the array that contains the data for that row; ie, a[r].
For a column it's a little trickier, but still pretty straightforward; just write a method that returns you all the elements of a particular column as an array, viz:and then, when you check the values, just 'miss out' the position that you want to place into.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joined: Jul 29, 2012
Sorry Campbell, It was my blunder. Don't why I made that without putting on paper and just posted. But thanks for your reply. I tried it and I got the output up to some extent.
The following is my code that I wrote to prepare Sudoku. But Sorry in advance even if you watch this, you may get eyestrain ◙, but what to do, the program itself is getting like this for me. And even I know the reason for it, as it is unable to create any new unique number, it is printing the default value that is 0.
anoopkumar anoo wrote:The following is my code that I wrote to prepare Sudoku. But Sorry in advance even if you watch this, you may get eyestrain ◙, but what to do, the program itself is getting like this for me. And even I know the reason for it, as it is unable to create any new unique number, it is printing the default value that is 0.
Right, and the reason is that you're trying to solve the problem existentially, ie, just ploughing in values that conform to the rules until you run into a problem - and I'm afraid it won't work. You might luck into a combination that works, but chances are heavily against you.
To be honest, I'm not sure what the solution is for Sudoku, but I bet there are tons of 'helper' sites out there in Internet-land; and I'd also care to bet that most use heuristics.
My advice: Have a look at this page and see if you can find a few Sudoku "filler" solutions on Google. If you understand them, try out your own; if not, lower your sights - at least for the moment.
Joined: Jul 29, 2012
Hi Winston, Thanks for your reply. Yes I thought first I had to give a hard try up to which I could. I have referred some sites just now, as you said they did use some high techniques to get this logic, but I am unable to get those clearly. I am gonna check in that link which you had provided, whether I could find any other alternative logic.