Unique methods to generate sudoku puzzle [duplicate] - algorithm

This question already has answers here:
How to generate Sudoku boards with unique solutions
(15 answers)
Closed 8 years ago.
How many possible unique ways are there to generate a Sudoku Puzzle??
I can think of only two possible ways
1) Take a solved Sudoku puzzle and shuffle the rows and columns
2) Generate a random number and check if it violates any Sudoku constraints, repeat untill number does not violate any Sudoku constraint for every square(theoretically possible but normally it leads to deadlocking )
Are there any other ways?

Here is a 20-page PDF, titled "Sudoku Puzzles Generating: from Easy to Evil", that you'd probably find useful in your quest.
To answer your question:
Are there any other ways?
Yes. Yes there are.

Simple way to generate up to Take a Solved Sudoko puzzle,
step 1 ) replace all 1 with A , 2 with B till 9 with I,
Step 2) do a shuffle in each horizontal and vertical block block of using a random between 1 and 3, here in each there can only be 3 possible combinations.
step 3) now shuffle the block there can only be 3 vertical and 3 horizontal shuffle
step 4) rotate the block 1 to 4 time..
step 5) mirror the puzzle vertically and horizontally using a random between 1 and 2.
step 6) replace all A with any number 1 to 9..
guessing this will produce around 38,093,690,880 combos....

Related

Maximize revenue algorithm [duplicate]

This question already has answers here:
Algorithm design: can you provide a solution to the multiple knapsack problem?
(2 answers)
Optimal way of filling 2 knapsacks?
(3 answers)
Closed 6 years ago.
We have two boxes, size of 20 and 30. We want to pack fruits in the boxes in a way that gives us the biggest revenue.
Price Size
APPLES 5 19
ORANGES 11 11
BANANAS 2 5
KIWI 8 16
MANGO 22 23
CHERRY 2 9
Example looks very similar to rod cutting problem. First solve rod cutting problem for the first box (size of 20) and then with the remaining items solve rod cutting problem for the second box (size of 30). Or vice versa.
But then I came up with this example:
If we first pack first box (size of 20), the best combination leads to Oranges (11|11) and Bananas (2|5) which gives us price 13 and size 16. Rod cutting 'algorithm' will always choose best Price/Size combination.
Now lets continue with the second box (size of 30).
We pack Mango (22|23) and that's it. We have ran out of space for any other fruit.
Total revenue of two boxes in this example equals to 13 + 22 = 35. However, we could have had revenue of 37, with Cherry (2|9) in the first box and Bananas (2|5) in the second one.
If we first packed the second, bigger box, we would get correct result. But does that way (packing boxes starting with bigger ones) always works? I believe, despite starting with bigger boxes, there will always be the case where this algorithm will fail. What would be better approach to solve this problem?
EDIT:
In this example we have two boxes but it could be N boxes. I am looking for a solution that would solve this problem in general, N boxes.

Given a matrix of ints, find the longest consecutive snake of incrementing by 1 numbers [duplicate]

This question already has answers here:
Find maximum length of good path in a grid
(4 answers)
Closed 7 years ago.
Basically, you have something like this:
0 9 5 3'
4 1 5' 4'
5 7' 6' 9
2 8' 5 10
In this case, the longest snake would be 3 -> 4 -> 5 -> 6 -> 7 -> 8. I put ' behind the numbers in this to help show it visually.
You can go both horizontally and vertically. The matrix can be n x m, so there isn't really a limit to the number of rows and columns.
What is the most optimal way to figure this out?
I've thought about starting at position n/2 and m/2, then recursively doing breadth-first search and keeping track of the max interval I can find. I'm not sure how to best tackle it.
You could create a graph where nodes are matrix positions and vertices are pointing from a number N to a N+1 neighbour.
Once the graph is built, your problem amounts to finding one of the longest paths in this graph.

Powers of a half that sum to one [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!

Looking for New Ideas on An Old Interview Trick [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
The original question is:
Describe an algorithm to output a die roll(a random number from 1 to 6), given a function that outputs a coin toss(a random number from 1 to 2). Each possible outcome should be equally likely.
The most popular answer to this question is:
Flip the coin three times, and use the three coin flips as the bits of a three-bit number. If the number is in the range 1 to 6, output the number. Otherwise, repeat.
My question is:
Most of the discussions on Stack Overflow come in the above flavour. I've also searched the Internet, finding that there exist many answers of other flavours, which they don't explicitly dig into. Could someone please share one or two different ideas on this problem?
There is a small improvement to the "toss 3 times and discard if 110 or 111" algorithm. Discarding 110 or 111 is wasteful, since you are wasting one perfectly good bit of entropy that you could reuse. After one of these values pops up, you only need to toss twice and get the value of the third toss from the mapping {110->tails, 111->heads}. Both of 110 and 111 are equally probable, so you're not introducing any bias this way.
In pseudocode:
bit0 = toss()
while True:
bit1 = toss()
bit2 = toss()
if bit1,bit2,bit3 give i such that 0<=i<=5 then
return i+1
else
bit0 = bit3 // the reuse happens here
The expected number of tosses here is 1 + 2 * expected_number_of_loop_executions = 1+2*8/6 = 11/3
If you just want other options, not necessarily good ones then how about this:
Flip a coin for each possible output value.
If there is one or more heads then discard all the possible values that got tails.
If you have only one value left then stop. Else goto 1.
I would suspect it is going to have a higher expected number of coin tosses than the method you've described and have no advantages at all really.
In general I assume this is why there is not much on the other possible ways of using random numbers. They are just not as good.
Flip 5 coins. If they're all heads or all tails, your answer is 1. If there's only one head or one tail, continue to the next step. If there's more than one head and more than one tail, repeat this step (including reflipping the coins).
Flip 4 coins. If they're all heads or all tails, your answer is 2. If there's only one head or one tail, continue to the next step. If there's two heads and two tails, repeat this step (including reflipping the coins).
Flip 3 coins. If they're all heads or all tails, your answer is 3. Otherwise, continue to the next step
Flip 2 coins. If they're both heads, your answer is 4. If they're both tails, repeat this step (including reflipping the coins). Otherwise, continue to the next step.
Flip 1 coin. If it's heads, your answer is 5. If it's tails, your answer is 6.

number of possible sudoku puzzles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Wiki http://en.wikipedia.org/wiki/Mathematics_of_Sudoku says Sudoku has 6,670,903,752,021,072,936,960 possible permutations.I tried to find out but it seems difficult.Can someone tell me how this number is calculated.
You can find all about it in this Wiki: http://en.wikipedia.org/wiki/Mathematics_of_Sudoku.
"the number of valid Sudoku solution grids for the standard 9×9 grid was calculated by Bertram Felgenhauer and Frazer Jarvis in 2005 to be 6,670,903,752,021,072,936,960 . This number is equal to 9! × 722 × 27 × 27,704,267,971, the last factor of which is prime. The result was derived through logic and brute force computation."
You can read the most recent rewrite of the original publication by Bertram Felgenhauer and Frazer Jarvis : Mathematics of Sudoku, it details the computation over 7 pages. The calculation actually isn't trivial (the idea being to enumerate distinct and valid Sudoku grids, rather than all possible arrangements of digits over a 9x9 grid).
Interestingly there was an estimation of the number of possible sudokus posted in an internet forum before the actual value was calculated and published by Felgenhauer & Jarvis.
The author of the post points out that there are some unproven assumptions in his guess. But the estimated value differs by 0.2% from the actual value published later.
In this Wiki you can find some estimation of other types of sudoku based on similar guesses.
Here is the full post from The New Sudoku Players' Forum:
by Guest » Fri Apr 22, 2005 1:27 pm
Lets try this from a whole different direction:
Step A:
Pretend that the only 'rule' was the 'block' rule, and that the row and column rules did not exist. Then each block could be arranged 9! ways, or 9!^9 ways to populate the puzzle (1.0911*10^50 'solutions').
Step B1:
If we then say 'let us add a rule about unique values in a row', then the top three blocks can be filled as follows:
Block 1: 9! ways
Block 2: 56 ways to select which values go in each 3-cell row, and 3! ways to arrange them (remember that we haven't invented a column rule yet).
Block 3: with 1 and 2 filled, the values that go in each row is now defined, but each row can be arranged 3! ways.
Therefore, we have 9! * 56 * 3!^6 ways to fill the top three blocks, and this value cubed to fill all nine blocks. (or 8.5227*10^35 solutions). Note that this represents a 'reduction ratio' (denoted as R) of 1.2802*10^14, by adding this one new rule.
Step B2: But we could have just as easily added a 'unique in columns' rule, and achieved the same results downward instead of across, with the same value of R.
Step C: (and here is where my solution is not rigorous) What if we assume that each of these rules would constrain the number of valid solutions by exactly the same ratio? Then there would be a combined reduction ratio of R^2. So the intitial value of 1.0911*10^50 solutions would reduce by a factor of R^2, or 1.639*10^28, leaving 6.6571*10^21 valid solutions.
This post and the account are attributed to Kevin Kinfoil (Felgenhauer & Jarvis).
Additional notes
Assume the Block 1 is
1 2 3
4 5 6
7 8 9
Then we have the following possibilities for Block2, if we ignore the order of the rows
1 2 3 4 5 6
4 5 6 7 8 9
7 8 9 1 2 3
this is 1 possibility
1 2 3 7 8 9
4 5 6 1 2 3
7 8 9 4 5 6
this is 1 possibility
1 2 3 two of 4,5,6, one of 7,8,9 3*3
4 5 6 the two remaining of 7,8,9, one of 1,2,3 3
7 8 9 the two remaining of 1,2,3, the remaining of (two of 4,5,6) 1
these are (3*3)*3*1=27 possibilities
1 2 3 two of 7,8,9, one of 4,5,6 3*3
4 5 6 two of 1,2,3, the remaining of 7,8,9 3
7 8 9 the two remaining of 4,5,6, the remaining of two of 1,2,3 1
these are (3*3)*3*1=27
So all in all these are 1+1+27+27=56 possibilities.

Resources