Need Multiple Sudoku Solutions [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to output multiple sudoku solutions in my program. For example, when You enter this as input:
8..6..9.5.............2.31...7318.6.24.....73...........279.1..5...8..36..3......
.'s denote blank spaces. Numbers represent already-filled spaces. The output should be a sudoku solution like so:
814637925325149687796825314957318462241956873638274591462793158579481236183562749
However, I want to output multiple solutions. This would be all the solutions that should be printed:
814637925325149687796825314957318462241956873638274591462793158579481236183562749
814637925325941687796825314957318462241569873638472591462793158579184236183256749
834671925125839647796425318957318462241956873368247591682793154579184236413562789
834671925125839647796524318957318462241956873368247591682793154519482736473165289
834671925125839647796524318957318462241965873368247591682793154519482736473156289
But my program only prints out one solution.
Could anyone help me come up with a way to print out multiple solutions? Thanks.

You shouldn't return here:
if(testTheNumber(arr, row, column+1)==true)
{
return true;
}
You should instead let the algorithm try all the possible k values.
And you should only print the solution at the end (when you've found all the numbers).

You can do this by simply not stopping your recursion when you find a solution. For example, something like:
if (row == 9) {
// print solution here
return true;
}
and remove the other return true; and just recurse:
testTheNumber(arr, row, column+1);
The above will stop the recursion when you find a solution (by reaching the end row), and will also continue trying more numbers after that point.
Also, you may have a bug in the if(k == 10) part, because k should never be 10 at that point. You will want to set the cell to 0 after existing from the k loop.

Related

Is this a Infinite loop? program finishes prematurely [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I´ve programmed a random writing program. That uses the markov algorithm. So it selects a order of letters, say [th] then go finding what appears most frequent after that using some randomness etc. But if the program selects a letter that has no "siblings". for example say right at the end of the file this symbol is [%] and it do not appear anywhere else in the file. What happens then is that the program just says "Finished running" it don´t even execute the coutcommand that prints out the string newText.
Why is this?
The rest of the code does basically some manipulation of arrays (adding etc..), to much code to post here.
for (int i = 0; i < fullText.length(); i++)
{
newText += getNext(currentWord, curWordPos, order);
}
cout << "Output: " << newText << endl;
Is this an Infinite loop?
No, it's not.

Fastest way to input integers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the fastest way(code) to take integer input from user via terminal (not file...:P).
P.S 1: Integers are of small size( within size of int) but the total number of intergers is very large.
P.S 2: Scanf toooo... slow
P.S 3: Forget the human limits ,talk technical...plz
I think an approach based on scanf will be hard to beat. In any case, it will be easy to implement. So I'd start with that, if it's not sufficient, benchmark before trying anything else.
If the input consists of whitespace-separated integers:
scanf("%d ", &input)
for continuous input processing you can try this
while( scanf("%d ", &val) == 1)
{
// processing : do what you want
}
also you can use this for file inputs reading (fscanf)

How do I make a smaller range with two given integers? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?

Quitting a While loop in C++ by entering a blank [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a loop in C++ that am stuck with, I want to end the loop by entering a blank, if a character is entered then the loop goes on. using VS2010
Presumably you have a variable storing a single character, and are wanting to stop the loop when someone enters a blank character (I'm going to presume space).
Just use a if statement and a break clause
while (true) {
// Get character here and put it into myChar variable
if ( myChar == ' ' )
{
break;
}
}
You could also put the check in the while condition if you have nothing else there. Another alternative would be a do-while loop.

print a series of numbers optimization part 2 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
earlyer i posted part 1 and got some interesting responces
print a series of numbers optimization part 1
here is another way you could have the program print a repeating series of numbers to the screen, the goal here is to make the most efficiant/fastest algorithm
int series[] = [2,3,4,5,6,7,8,9,1]
int i = 9;
while(true)
{
print(series[i])
i = series[i] - 1;
}
of course ignore any extra overhead created by actually printing the number because that is not the purpose of the problem
the one boolean conditional statement (while true) is required for the infinite loop is required no matter what solution you do, so you can ignore that too
this solution uses memory for 11 int variables, but otherwise it only does one simple computation and one variable assignment per iteration.
so would this be the most time efficiant way to solve the infiniate number series problem?
I would say it's not the most efficient way.
There's a multiplication involved in addressing the array. It's essentially
destinationAddress = baseAddressOfArray + indexRequested * sizeof(elementOfArray)
I think the most efficient way would be to cache the string of one iteration and simply spit out that string over and over again. I'm not up on my exact C++ syntax, it'd be something like
string s = "123456789";
while(true) {
print(s);
}