I just recently did a sudoku solver in java using backtracking.
Is it possible, given the solutions, to formulate a problem or puzzle?
EDIT
to formulate the original puzzle, is there some way to achieve this?
and an additional question,
given the puzzle and the solutions.
If I am able to solve the puzzle using the solutions (result is puzzles)
and at the same time able to solve the solutions using the puzzle (result is solutions)
Which has the greater number?
the puzzles? or the solutions?
It is possible to formulate one of the multiple possible original states.
Start with the final solution (all numbers are present)
Remove one number (chosen randomly or not)
Check if this number can be found back given the current state of the board (you already have a solver, it should be easy)
If this number can be calculated, everything is OK. Go back to 2.
If this number cannot be found back, put it back where it was. Go back to 2.
If no more numbers can be removed, you have reached one of the original states of the puzzle.
If you chose the numbers you remove randomly (step 2), you can execute this several times, and get different starting points that lead to the same final puzzle.
Creating a puzzle from a solution is very simple. You just apply the solving steps backwards.
For example, if a line contains 8 digits, you can fill in the 9th. If you do that backwards, if a line contains 9 digits, you can remove one. This will yield a very boring puzzle, but still a valid one (a valid puzzle is a puzzle with only one solution).
The more complicated the steps you make are, the more difficult the puzzle will be. In fact, brute forcing the puzzle is the most difficult strategy, executing it backwards probably boils down to randomly removing a digit, and brute force check if there is still only 1 unique solution. Notice you do not have to solve the entire puzzle: It's enough to prove there is only 1 way to add the removed digit back into the puzzle.
As for the second part of your question: This feels a bit like a mathematical question, but let me answer:
A good puzzle only has 1 solution. Since there are multiple puzzles that yield the same solution (like, there are 81 ways to fill in 80 of the 81 squares, all yielding the same solution from a different puzzle), you could say there are many more puzzles than solutions.
If you also allow puzzles with multiple solutions, it changes. For every puzzle, there must be one or more solutions, but all those solutions also belong to that puzzle, so the number of solutions to puzzles is equal to the number of puzzles to solutions. Invalid puzzles do not change this: Since they belong to 0 solutions, you need no additional puzzles belonging to those solutions.
Ps. It's also trivial to create puzzles if they do not need to be uniquely solvable: Just randomly remove some digits and you're done.
If I am able to solve the puzzle using the solutions (result is puzzles)
and at the same time able to solve the solutions using the puzzle (result is solutions)
Which has the greater number?
the puzzles? or the solutions?
There is no well-defined answer.
Each solution has exactly 281 corresponding puzzles, including the trivial one. Not all of these have a unique solution, but many do. Therefore, if the chosen set of solutions contains only one element, then the maximal set of puzzles that jointly correspond to the solution set is vastly larger.
On the other hand, the completely blank puzzle affords 6670903752021072936960 solutions. There are many pairs of those that share only the blank grid as a common puzzle. Thus, if the chosen set of puzzles includes only the blank grid then the set of corresponding solutions is vastly larger.
Related
I'm working on writing a k means algorithm that takes in a double[][] that stores locations and returning two clusters of locations.
I just have a really quick question: what is the best way to choose what the initial cluster values should be?
I've tried randomizing the values but that doesn't always work well, and I can't find any good answers to this question online. Any help is much appreciated.
One popular strategy that is usually more effective than random selection is to pick the first value at random, and then choose the second value by finding the farthest data point from the first selection.
The next value would then be chosen to be the furthest from both of the first two, and so on.
This is similar to the slightly more complex initialization algorithm K-means++.
Honestly, I only knew of such a game recently and I wonder how one can create a solving algorithm using the recursive search method?
There are 15 holes in total in the triangular board. Making that 14 pegs with a total of 13 moves.
I don't know where to start with this in C++ nor Java. I have studied C++ for an year before. So I'm familiar with the concepts of stacks, linked lists etc.
I just don't know how to start the code. The program firstly asks the user where they want to start (How is this done?)
Then once it solves it , a certain number of pegs more than just one will be left and the program will ask the user for a better solution (like this until the board is left to just one peg.)
I certainly cannot think of how to make the moves possible ( How do I write a code that "SHOWS" that one peg moves over a hole ,into another?)
I'd love some coding assistance here. It would really be appreciated.
Try treating the board a linked list of holes or positions. Each data field in a node would represent a hole. Each node would have a vector of links to other holes, depending on its position relative to the edge of the board.
To move a peg, iterate over the possible links.
This is just one method, there are probably better ones out there.
Take a look at my answer here: Timeout on a php Peg Puzzle solver. The 15-peg puzzle was the first program that I ever wrote (over 10 years ago), after just learning c++.
The posted solution is a re-write that I did several years later.
Since I was the answerer I can tell you that a triplet is a term that I made up for a move. There are three pegs involved in a move (a triplet). Each peg is represented by a bit. In order for a move to be legal two consecutive pegs (bits) need to be set and the other (the target location) needs to be clear.
The entire solution involves a depth first search using a 16-bit integer to represent the board. Simple bit manipulation can be used to update the board state. For example if you xor the current state with a move (triplet) it will make the move against the board. The same operation applied a second time against the board is an undo of the move.
To be successful as a programmer, you need to develop the skill of examining a problem, figuring out how it can be solved, and come up with a reasonable in-program representation that enables you to solve it.
For problems like this, I find it helpful to have the puzzle in front of me, so I can try it out by hand. Failing that, at least having a picture of the thing will help:
http://www.obsidiandesigns.com/pyramidsol.jpg
Here's another way to look at it, and possibly consider representing it in memory like this:
OXXXX
XXXX
XXX
XX
X
Each "X" is a peg. The "O" is a hole. One can change "OXX" to "XOO" by jumping the 3rd X over the middle one, into the hole. Also consider vertical and diagonal jumps.
Linked lists could make a lot of sense. You might actually want "2D" links, instead of the usual "1D" links. That is each "hole" instance could contain pointers to the two or three holes next to it.
Once you have a representation, you need a way to find valid moves. And to make valid moves.
For this "game," you may want to search the whole space of possible moves, so you'll probably want to either have each move produce a new board, or to have a way to "undo" moves.
A valid move is where one peg jumps over another peg, into an empty hole. The jumped peg is removed. You can't jump from "just anywhere" to anywhere, over any peg you wish -- the three holes have to be next to each other and in line.
The above should give you some hints that will help you get started.
I'm practicing up for a programming competition, and i'm going over some difficult problems that I wasn't able to answer in the past. One of them was the King's Maze. Essentially you are given an NxN array of numbers -50<x<50 that represent "tokens". You have to start at position 1,1(i assume that's 0,0 in array indexes) and finish at N,N. You have to pick up tokens on cells you visit, and you can't step on a cell with no token (represented by a 0). If you get surrounded by 0's you lose. If there is no solution to a maze you output "No solution". Else, you output the highest possible number you can get from adding up the tokens you pick up.
I have no idea how to solve this problem. I figured you could write a maze algorithm to solve it, but that takes time, and in programming contests you are only given two hours to solve multiple problems. I'm guessing there's some sort of pattern i'm missing. Anyone know how I should approach this?
Also, it might help to mention that this problem is meant for high school students.
This type of problem is typically solved using dynamic programming or memoization.
Basically you formulate a recursive solution, and solve it bottom up while remembering and reusing previously computed results.
The simple approach (i.e. simplest to code) is try all the possible paths - try each first step; for each first step try each second step; for each first step/second step combination try each third step; and so on. However depending on how big the maze is this may take too long to run (or it may not).
Your next step is to think about how you can do this faster. The first step is usually to eliminate moves that you know can't lead to a finish, or can't lead to a finish with higher points than the one you already have. Since this is practice for a competition we'll leave you to do this work yourself.
Think "graph" algorithms: The Algorithm Design Manual
Yes, I know this is nothing new and there are many questions already out there (it even has its own tag), but I'd like to create a Sudoku Solver in Java solely for the purpose of training myself to write code that is more efficient.
Probably the easiest way to do this in a program is have a ton of for loops parse through each column and row, collect the possible values of each cell, then weed out the cells with only one possibility (whether they contain only 1 number, or they're the only cell in their row/column that contains this number) until you have a solved puzzle. Of course, a sheer thought of the action should raise a red flag in every programmer's mind.
What I'm looking for is the methodology to go about solving this sucker in the most efficient way possible (please try not to include too much code - I want to figure that part out, myself).
I want to avoid mathematical algorithms if at all possible - those would be too easy and 100% not my work.
If someone could provide a step-by-step, efficient thought process for solving a Sudoku puzzle (whether by a human or computer), I would be most happy :). I'm looking for something that's vague (so it's a challenge), but informative enough (so I'm not totally lost) to get me started.
Many thanks,
Justian Meyer
EDIT:
Looking at my code, I got to thinking: what would be some of the possibilities for storing these solving states (i.e. the Sudoku grid). 2D Arrays and 3D Arrays come to mind. Which might be best? 2D might be easier to manage from the surface, but 3D Arrays would provide the "box"/"cage" number as well.
EDIT:
Nevermind. I'm gonna go with a 3D array.
It depends on how you define efficient.
You can use a brute force method, which searches through each column and row, collects the possible values of each cell, then weeds out the cells with only one possibility.
If you have cells remaining with more than one possibility, save the puzzle state, pick the cell with the fewest possibilities, pick one of the possibilities, and attempt to solve the puzzle. If the possibility you picked leads to a puzzle contradiction, restore the saved puzzle state, go back to the cell and choose a different possibility. If none of the possibilities in the cell you picked solves the puzzle, pick the next cell with the fewest possibilities. Cycle through the remaining possibilities and cells until you've solved the puzzle.
Attempt to solve the puzzle means searching through each column and row, collecting the possible values of each cell, then weeding out the cells with only one possibility. When all of the cells are weeded out, you've solved the puzzle.
You can use a logical / mathematical method, where your code tries different strategies until the puzzle is solved. Search Google with "sudoku strategies" to see the different strategies. Using logical / mathematical methods, your code can "explain" how the puzzle was solved.
When I made mine, I thought I could solve every board using a set of rules without doing any backtracking. This proved impossible as even puzzles targeting human players potentially require making a few hypothesis.
So I starting with implementing the basic "rules" for solving a puzzle, trying to find the next rule to implement that would allow the resolution of where it stopped last time. In the end, I was forced to add a brute forcing recursive algorithm, but most puzzles are actually solved without using that.
I wrote a blog post about my sudoku solver. Just read through the "The algorithm" section and you'll get a pretty good idea how I went about it.
http://www.byteauthor.com/2010/08/sudoku-solver/
Should anyone need a reference Android implementation, I wrote a solution that uses the algorithm from the post above.
Full open-source code here: https://github.com/bizz84/SudokuSolver
Additionally, this solution loads Sudoku Puzzles in JSON format from a web server and posts back the results.
You should think about reducing the Sudoku Problem to a SATisfiability problem.
This method will avoid you to think too mathematically but more logically about the AI.
The goal step by step is basically :
* Find all the constraints that a Sudoku has. (line, column, box).
* Write these constraints as boolean constraints.
* Put all these constraints in a Boolean Satisfiability Problem.
* Run a SAT solver (or write your own ;) ) on this problem.
* Transform the SAT solution into the solution of the initial Sudoku.
It has been done by Ivor Spence by using SAT4J and you can find the Java Applet of his work here : http://www.cs.qub.ac.uk/~I.Spence/SuDoku/SuDoku.html.
You can also download directly the Java code from SAT4J website, to see how it look like : http://sat4j.org/products.php#sudoku.
And finally, the big advantage of this method is : You can solve N*N Sudokus, and not only the typical 9*9, which is I think, much more challenging for an AI :).
I have a data set with attributes like this:
Marital_status = {M,S,W,D}
IsBlind = {Y,N}
IsDisabled = {Y,N}
IsVetaran = {Y,N}
etc. There are about 200 such variables.
I need an algorithm to generate combinations of the attributes, with one value at a time.
In other words, my first combination would be:
Marital_status = M, IsBlind = Y, IsDisabled = Y, IsVeteran = Y
The next set would be:
Marital_status = M, IsBlind = Y, IsDisabled = Y, IsVeteran = N
I tried to use a simple combination generator, treating each value for each attribute as an attribute itself. It did not work because the mutually exclusive choices are included in the combinations and the number of possible combinations was really huge (133873417996074857185490633899939406700260683726864088366400 to be precise)
Could you please suggest an algorithm (preferably coded in Java)?
Thanks!!
As others have pointed out (and yourself also), it is impossible to test exhaustively this.
I suggest you take the sampling approach, and test with that. You have strong theoretical background, so you will be able to find your way in the internet to find and understand this.
But let me give a small example. For now, I will ignore possible "clusters" of parameters (that are strongly related).
Create a sample of one data, containing all possible values for all your 200 parameters. This exhaustivity ensures that no parameter value could be forgotten.
It doesn't have to be created upfront, the values can be created by a loop.
To each sample of one data, you need to add the other values. A simple approach would be to choose a number of times you want to test each one-sample (say N = 100). For each sample of one data, you would generate randomly N times the other values.
If there are 1000 possible values using all 200 parameters, and N=100, that would give us 100K tests.
You could elaborate on this basic idea in many ways:
If you want your test to be repeatable, you could generate it only once, store it, and then reuse the same sets in all future tests.
You could control your distribution so that each value gets selected a fair number of times.
In real life, all 200 parameters wouldn't be without connections. Many parameters would actually be connected to some others, in that the probability of finding the values together are not even. Instead of making the initial exhaustive set on only one parameter as I did previously,
I would run the exhaustive set on a cluster of connected parameters.
Find another way. If you have 200 variables, and each one has at least 2 choices, you're going to have >= 2^200 combinations. If you generate one combination each nanosecond, it would take about 10^43 years to enumerate 2^200 choices.
As Keith pointed out, the number of combinations will be impossibly large if there are no excluded combinations, which would make your need unmeetable. However, since you've already said that you have mutually exclusive choices, the solution space will be smaller.
How much smaller? Depends on how many choices are mutually exclusive. I recommend doing some math on that before going too hard.
Assuming that enough choices are exclusive, you're still going to have to essentially brute-force it, but you're very unlikely to find an existing, useful algorithm.
Which brings me to the question: what's your reason for doing this - exhaustive testing? Sounds good, but you may find that that's not possible. I've encountered this issue myself, and in the end, you may well be forced to some combination of carefully selected "edge" cases, plus some quasi-randomly selected other cases.
Having read your comment above, it appears you define "mutual exclusion" differently than I do, and I fear that you may have a problem.
So a given patient is not both blind and not blind. Great. But that's not what I (and I suspect everyone else here) understood when you mentioned mutual exclusions.
By those, I'm talking about e.g., if blind, cannot be non-disabled, or something like that.
Without a significant number of mutually exclusive inter-relationships between your attributes which limit their combinations, you will be unable to complete your exhaustive testing.