I have modified the question on n-puzzle. In this scenario, the puzzle has two blanks instead of one blank.
Initial State
3 5 1
4 6 -
7 2 -
Goal State
- 1 7
3 2 -
5 6 4
Is there any algorithm that I can use for this?
All existing algorithms that solve the regular sliding tile puzzle (such as A* or IDA*) can solve this variant as well. The puzzle with multiple blanks is equivalent to a pattern database for the sliding-tile puzzle - the exact solution to the puzzle with some pieces replaced with blanks can be used as a heuristic for the original puzzle with only a single blank.
(To be precise they are equivalent to additive pattern databases. You can combine several together and add their heuristic values as long as the action cost of swapping two blanks is 0 and none of the tiles are duplicated.)
Firstly, apologies for not using the correct terminology here. I don't actually know the correct terms, and thus have failed miserably to find a solution. Please accept my example as the question, and I'll update the question accordingly if somebody can enlighten me (or delete and read the actual solution where it exists).
So far my research has only yielded comparisons of single bits within the whole, rather than showing all.
Given a set of integers:
ELEMENT
-----------
Bricks 1
Plaster 2
Cement 4
Concrete 8
I have a result set that provides how these materials are used:
MIXTURE ELEMENTS
----------------------
MixtureFoo 3
MixtureBar 7
MixtureBaz 11
I need to show the final set of mixtures, but with each constituent element listed that is used in the respective mixture:
MIXTURE ELEMENTS ELEMENT
------------------------------
MixtureFoo 3 1
MixtureFoo 3 2
MixtureBar 7 1
MixtureBar 7 2
MixtureBar 7 4
MixtureBaz 11 1
MixtureBaz 11 2
MixtureBaz 11 8
You could use bitwise operations:
SELECT *
FROM t
JOIN ELEMENT e
ON t.ELEMENTS & e.w = e.w
ORDER BY MIXTURE, w;
db<>fiddle demo
I have a problem of tee diagram that represents city roads and need to calculate all possible ways -permutations- to establish these roads.
The role is " you can not start level 2 without finishing level 1" and so on for higher levels
this image illustrates the ideahere
i tried to think of it as an array for every level and then a single column for every branch like this
level1=[1 2]
level2=[3 4
5 6]
and
l
;evel1=[1 2 3]
level2=[4 5 0;
6 7 8;
9 10 0]
level3=[11 12;
13 0;
14 15;
16 17;
0 0;
0 0;
18 19]
But i stopped and have no idea how to complete. so i need to know how to think in this kind of problems.
You should look into the concept of "abstract datatypes" as your current design won't scale. Every time you add a new level, you need to create a new array and then update your code to know about this new array.
And the process of searching one level before moving on to the next is known as "breadth first" searching. If you create a list type, you can add all the nodes from the current level to the list and then as you process the list, you add the child nodes to the end of the list.
But that isn't really applicable to generating all permutations - doing "depth first" is more suitable as you travel down the tree and when you can't go no further, the path you've taken is a permutation.
I have a large vector of numbers, say 500 numbers. I would like a program to detect patterns (reoccurrence in this case) in such vector based on following rules:
A sequence of numbers is a pattern if:
The size of the sequence is between 3 and 20 numbers.
The RELATIVE positions of the numbers in sequence is repeated at
least one other time in a vector. So let's say if I have a sequence
(1,4,3) and then (3,6,5) somewhere else in the vector then (1,4,3) is
a pattern. (as well as (2,5,4), (3,6,5) etc.)
The sequences can't intersect. So, a vector (1,2,3,4,5) does not
contain patterns (1,2,3) and (3,4,5)(we can't use the same number for
both sequences). However, (1,2,3,3,4,5) does contain a pattern
(1,2,3) (or (3,4,5))
A subset A of a pattern B is a pattern ONLY IF A appears somewhere
else outside B. So, a vector (1,2,3,4,7,8,9,2,3,4,5) would contain
patterns (1,2,3,4) and (1,2,3), because (1,2,3,4) is repeated (in a
form of (2,3,4,5)) and (1,2,3) is repeated (in a form (7,8,9)).
However, if the vector was (1,2,3,4,2,3,4,5) the only pattern will
be (1,2,3,4), because (1,2,3) appeares only in context of (1,2,3,4).
I'd like to know several things:
First of all I hope the rules don't go against each other. I made them myself so there might be a clash somewhere that I didn't notice, please let me know if you do notice it.
Secondly, how would one implement such system in the most efficient way? Maybe someone can point out towards some particular literature on the subject? I could go number by number starting with searching a sequence repetition for all subsets of 3, then 4,5 and till 20. But that seems to be not very efficient..
I am interested in implementation of such system in C, but any general guidance is very welcome.
Thank you in advance!
Just a couple of observations:
If you're interested in relative values, then your first step should be to calculate the differences between adjacent elements of the vector, e.g.:
Original numbers:
1 4 3 2 5 1 1 3 6 5 6 2 5 4 4 4 1 4 3 2
********* ********* ********* *********
Difference values:
3 -1 -1 3 -4 0 2 3 -1 1 4 3 -1 -3 0 -3 3 -1 -1
****** ****** ****** ******
Once you've done that, you could use an autocorrelation method to look for repeated patterns in the data. This can be computed in O(n log n) time, and possibly even faster if you're only concerned with exact matches.
I'm having trouble with my n-puzzle solver. Thought it was working, but it turns out it is solving insoluble puzzles. I've tried to trace it, but that's a lot of tracing and so far I see no cheating. I think I understand the algorithm for determining solubility, and my implementation agrees with the odd/even parity of some examples from the web... that is to say, when I count up the number of tiles after a given tile that are smaller than it, for every tile, and then add the row index of the blank tile, I get the same odd or even number as others have gotten.
So a thought that has occurred to me. In my model of, say, the 8-puzzle, my solution state is:
_ 1 2
3 4 5
6 7 8
Rather than
1 2 3
8 _ 4
7 6 5
Or
1 2 3
4 5 6
7 8 _
As it is in some other formulations. Could this be affecting which puzzles are soluble and which are not?
Thanks!
z.
In general, yes: If a configuration is solvable to the standard solution, it will not be solvable to an unsolvable configuration.
In particular, it depends on the exact configuration you're using as a solution. You will need to check to see if you can solve from that configuration to the standard one.
EDIT: This of it this way:
Let A be the standard solution.
Let B be your preferred solution.
Let C be your starting configuration.
If you can get from A to B, and you can get from C to A, then you can get from C to B.
But if you can't get from A to B, and you can get from C to A, then you can't get from C to B.