Creating an array(?) of Sets in Java - java

I'm trying to write an algorithm that finds the number of solutions in a given partially filled in Sudoku board. i.e. given
"1 6 4 0 0 0 0 0 2",
"2 0 0 4 0 3 9 1 0",
"0 0 5 0 8 0 4 0 7",
"0 9 0 0 0 6 5 0 0",
"5 0 0 1 0 2 0 0 8",
"0 0 8 9 0 0 0 3 0",
"8 0 9 0 4 0 2 0 0",
"0 7 3 5 0 9 0 0 1",
"4 0 0 0 0 0 6 7 9"
where 0s represent blank spots. I want to create 3 separate arrays of sets, one for each set of numbers in each column, row, and 3x3 square. I am trying the declaration:
horizontal = new HashSet<Integer>[9];
Where private HashSet[] horizontal is declared earlier, but this doesn't work. What is the correct declaration or can I not make declare an array of Sets?

The problem is the type parameter. You can't create generic arrays in Java. You can remove the type parameter and it will work, but you should get a warning about unchecked operations.

You might try this:
horizontal = new ArrayList<HashMap<Integer>>();
Access it with horizontal.get(1); and you can treat it the same way as a normal array. As #user599152 said, you can't create generic arrays. So you need to figure out another way. A list is probably your best bet.

An array of sets is a strange way to store the data. A 2d array or a 2d array of 2d arrays might be a much more intuitive way of modeling this. Possibly even a 2d array of some custom class ("SudokuCube") would also work better than an array of sets.

Related

Give a part of chessboard of 15-puzzle, how to get all of the state of the the part chessboard using BFS?

the board is like this:
1 2 3 4
5 6 7 0
0 0 0 0
0 0 0 0
the '0' represents that is empty, we can move the non-zero number to the '0'.
so how to get all of the state of the board using BFS?
for example, there are two state of the board:
1 2 3 4
0 0 0 0
5 6 7 0
0 0 0 0
1 2 3 0
4 0 0 0
5 0 0 0
6 7 0 0
The reason I ask this question is that I need to process all of the 15-puzzle state using Disjoint pattern database to solve the nearly most difficult state of 15-puzzle in 1 minutes.
15 14 13 12
11 10 9 8
7 6 5 4
3 1 2 0
I need to process all of the 15-puzzle state [..] to solve the nearly most difficult state of 15-puzzle in 1 minutes
Approach 1 - using a database and storing all states
For reasons given by Henry as well, and also supported by [1], solving this problem using a database would require generating the entire A_15 , storing all of it and then finding the shortest path, or some path between a given state and the solved state. This would require a lot of space and a lot of time. See this discussion for an outline of this approach.
Approach 2 - using a specialized depth-first search algorithm
Here is an implementation of this search strategy that uses the IDA algorithm.
Approach 3 - using computational group theory
Yet another way to handle this in a much shorter amount of time is to use GAP (which implements a variant of Schreier-Sims) in order to decompose a given word into a product of generators. There is an example in the docs that shows how to use it to solve the Rubik's cube, and it can be adapted to the 15-puzzle too [2].
[1] Permutation Puzzles - A Mathematical Perspective by Jamie Mulholland - see page 103 and 104 for solvability criteria, and the state space being |A_15| ~ 653 billion
[2] link2 - page 37

How to create guitar chord variation?

I'm trying to create guitar chord application but the chord chart for a single chord is too many. Listing all would take time and not efficient. For example, C major chord has variation such as the chart below
x 3 2 0 1 0
x 3 2 0 1 3
x 3 5 5 5 0
8 10 10 9 8 8
Is there any way that the chart can be generated by with/without knowing the keys to create the chord? ie, CEG for chord C major.
Contrary to the comments I think this is possible. If you got all of the open chord shapes such as..
0 2 2 1 0 0 (open E)
0 0 2 2 2 0 (open A)
0 3 2 0 1 0 (open C)
X 0 0 2 3 2 (open D)
Then you can shift them up using bar chords and you should be able to write code to calculate all the permutations for a particular chord. For example if you wanted all the C major triads (as in your question) you know one is
0 3 2 0 1 0 (open C)
But you also know one of them is the open A shape above but shifted UP 3 frets...
3 3 5 5 5 3 (open A shifted up as a bar chord)
This is your third chord in your question. You can also calculate where to shift the open E shape, which would be...
8 10 10 7 8 8
Again, this is one of the chords in your question. This way you can calculate all the positions for any chord using code - providing you have a sufficient set of open chords that can be comfortably shifted up like this.
So your set of C chords using these initial open shapes would be...
C Chords...
8 10 10 7 8 8
3 3 5 5 5 3
0 3 2 0 1 0
X 10 10 12 14 12
Now if you wanted to know the set of D chords you can actually calculate these by adding 2 to all the numbers above (two of them wrap around an octave but you can work this into a calculation)
D Chords...
10 12 12 11 10 10
5 5 7 7 7 5
10 13 12 10 11 10
X 0 0 2 3 2
Have you ever seen the book called "Fret Logic"? It shows the mathematical logic of the guitar fretboard.
There are also shortcut versions of most chords, such as the nice 3 string version used a lot in cross picking:
x
x
x
x
5
5
3
This is a shortened version the C chord in the A position, but it is used a lot.

Increasing the Elements of an Array Like a Number

I'm attempting to make a Sudoku solving program. To reach the puzzle's solution, the program interprets 0's as empty slots, and then makes an array that has a length equivalent to the number of zeros in the entire puzzle. From there, it sets all of the values in the array to 1 (the minimum value any slot can have in a Sudoku puzzle). What I'm trying to do is simulate a number's incremental pattern in the array starting from the element with the greatest index.
For example, a puzzle with three empty slots would result in an array of 3 elements. The array would then increase based on the pattern mention above:
0 0 0 (Initiation)
1 1 1 (Set to possible values)
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
1 2 1 (Skips 1 2 0 since it would include a 0)
1 2 2
etc.
This is a modified form of a base 10 number increment. Instead of 0-9, it uses 1-9. How may I build a method that will increment the array in this manner?
The basic algorithm here is to increment the right most digit then, if it overflows, increment the next to the left and so on. Recursion is a neat way of solving this. I'll do it in pseudocode and leave you to convert to Java
function increment(array, digit)
if (array[digit] < 9)
array[digit] += 1
else if (digit > 0)
array[digit] == 1;
increment(array, digit - 1)
else
you are finished
Then each time you call this with: increment(array, array.length - 1)

Iterative Reduction to Null Matrix

Here's the problem: I'm given a matrix like
Input:
1 1 1
1 1 1
1 1 1
At each step, I need to find a "second" matrix of 1's and 0's with no two 1's on the same row or column. Then, I'll subtract the second matrix from the original matrix. I will repeat the process until I get a matrix with all 0's. Furthermore, I need to take the least possible number of steps.
I need to print all the "second" matrices in O(n) time. In the above example I can get to the null matrix in 3 steps by subtracting these three matrices in order:
Expected output:
1 0 0
0 1 0
0 0 1
0 0 1
1 0 0
0 1 0
0 1 0
0 0 1
1 0 0
I have coded an attempt, in which I am finding the first maximum value and creating the second matrices based on the index of that value. But for the above input I am getting 4 output matrices, which is wrong:
My output:
1 0 0
0 1 0
0 0 1
0 1 0
1 0 0
0 0 0
0 0 1
0 0 0
1 0 0
0 0 0
0 0 1
0 1 0
My solution works for most of the test cases but fails for the one given above. Can someone give me some pointers on how to proceed, or find an algorithm that guarantees optimality?
Test case that works:
Input:
0 2 1
0 0 0
3 0 0
Output
0 1 0
0 0 0
1 0 0
0 1 0
0 0 0
1 0 0
0 0 1
0 0 0
1 0 0
Summing of each row / column and taking the largest of those sums gives you the optimal number of matrix subtractions required to reduce to a null matrix.
For example:
1 2 4 0 = 7
2 2 0 1 = 5
0 0 1 0 = 1
3 0 2 1 = 6
= = = =
6 4 7 2
Which means that this matrix will take 7 optimal subtractions to empty.
I believe that counting backwards from this and removing from columns / row with that value will solve your problem (I am not sure of an efficient way of selecting these - brute force?).
You can also use your previous method to remove extra elements.
For example (using the above matrix).
Step 7:
We must subtract from row 1 & column 3.
0 0 1 0
0 0 0 0
0 0 0 0
0 0 0 0
Solves this, so now we can use your previous method to remove "bonus" elements.
0 0 1 0
1 0 0 0
0 0 0 0
0 0 0 1
Now apply the sum of each row / column again and continue for the next step.
Step 6:
1 2 3 0 = 6
1 2 0 1 = 4
0 0 1 0 = 1
3 0 2 0 = 5
= = = =
5 4 6 1
Next subtraction:
0 0 1 0
0 1 0 0
0 0 0 0
1 0 0 0
And so on.
Note: This still does not work very well with "all 1" matrices, as you get stuck on the problem of selecting 1 from every row and column (same as you did in your example).
But someone may be able to extend my solution.
Let Number of rows = Number of columns = N
for iteration=1:N
for row=1:N
cell(row,(row+iteration)%N) := 0
Number of iterations is N. In every iteration N one's will be changed to 0
I'm not entirely sure if this is what you are after, but could you create a list of available columns and mark them as used for each iteration.
For Example:
repeat until an empty matrix
mark all columns as available
for each row
find the maximum value in all available columns and store it's coordinates
mark that column as unavailable
print, decrement and clear the list of stored coordinates
This doesn't work, but it does show the algorithm that user1459032 is using.
1) If all you want to do is iterate through all the elements in your matrix...
2) then all you have to do is loop for (int i=0; i < rows*cols; i++) {} ...
3) And such a loop is ALREADY O(n) (i.e. it increases LINEARLY with the #/elements in your matrix)
I'm pretty sure that this is some kind of variant of the exact cover problem, which is known to be NP-complete. Your proposed algorithm is a simple greedy solution. The problem with greedy solutions is that they often work well enough to convince you that greed is good and then suddenly leave you high and dry looking for a better solution. (Consider the global economy, for example.) Anyway, Knuth's Dancing Links technique is a standard way of solving the problem (exact set cover, not global economy).

logics for crossword

I have a task to create a crossword, a specific one. All the answers are given, but their places are unknown. Program must read a file with board scheme like this :
0 1 0 0 0 0 0 0 1 0 0
0 1 0 1 1 1 1 1 1 1 1
0 1 0 1 0 0 1 0 1 0 1
0 S 1 1 0 1 1 1 1 0 1
0 1 0 0 1 0 1 0 1 0 0
1 1 1 1 1 1 1 S 1 1 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0
treating each column/row of ones as one possible answer. Is there any way to parse through this file and marking answers without using gazilion if's for each field ?
Rest of the logics is as follows :
- on the base of the parsed file crossword is created.
- user selects answers from lists of possibilities
- user clicks on the first block of Answer and if length and letters of selected answer and Answer match - fields are updated
Game board should be stored in 2d array I guess, and each Answer should have indexes of fields in it ?
Crossword puzzle construction is NP-Complete in general (i.e nxn board of 1s and 0s and a given set from which to pick answers). Look at: http://en.wikipedia.org/wiki/List_of_NP-complete_problems which just mentions this. Garey and Johnson's classic book also has a mention of this, saying Exact cover by 3 sets can be reduced to it.
So, you probably will have to use some backtracking/heuristic to fill the grid.
Perhaps this project report of two students from Dartmouth college will be of some help: Crossword Puzzle Generator. It contains some heuristics which you might be able to use.
Of course, you seem to imply there is a human involved, but it is not clear if you can leverage that person to fill the grid and whether your problem is basically some UI programming problem in helping the user out.

Categories

Resources