Special sliding puzzle - Algorithm to find minimum distance - algorithm

I just stumbled upon a strange (and very annoying game) that I wanted to solve programmatically. It reminds a bit of Rubik's cube, but 2 dimensional. I'm struggling a bit on how to approach this...
There is a 9x9 square with some circles placed into the inner squares. For instance, one get's the following picture:
A B C D E F G H I
-------------------------------------
9 | | | O | | | O | | | | J
-------------------------------------
8 | | | O | | O | | O | | | K
-------------------------------------
7 | | | | O | | | O | O | | L
-------------------------------------
6 | | | O | | | | O | | | M
-------------------------------------
5 | | | O | | | | | | | N
-------------------------------------
4 | | | | O | | O | O | | | O
-------------------------------------
3 | | | | | O | | O | | | P
-------------------------------------
2 | | | | O | | | | | | Q
-------------------------------------
1 | | | O | | | | | | | R
-------------------------------------
0 Z Y X W V U T S
One can use the numbers and letters arround the square to shift entire "rows" or "columns" to either left/right or up/down. Circles that would leave the game area to the right would reappear on the left and vise-versa, same accounts for top/bottom.
The goal is to rearrange the circles to a given pattern with a maximum amount of moves. For instance, one should rearrange the circles in the above picture to reflect the below picture in maximum 17 moves:
A B C D E F G H I
-------------------------------------
9 | | | | | | | | | | J
-------------------------------------
8 | | | O | O | O | O | O | | | K
-------------------------------------
7 | | | O | | | | O | | | L
-------------------------------------
6 | | | O | | | | O | | | M
-------------------------------------
5 | | | O | | | | O | | | N
-------------------------------------
4 | | | O | | | | O | | | O
-------------------------------------
3 | | | O | O | O | O | O | | | P
-------------------------------------
2 | | | | | | | | | | Q
-------------------------------------
1 | | | | | | | | | | R
-------------------------------------
0 Z Y X W V U T S
I would like to feed the starting and the end position of the circles to a program that delivers the shortest path possible. I'm struggling a bit to find an approach that doesn't just try all possible moves until a given maximum number of moves is reached.
Also it doesn't seem to be that easy to modify the approach that's being used to solve a Rubik's cube for instance...
Well, I thought it was a very interesting problem, and maybe somebody here has an illuminating idea.
UPDATE:
Just trying all the possible moves doesn't really seem realistic after a first try. There are just too many permutations. I think this could be really hard to solve...if possible at all.

Related

How can i use the functions of "survey" package in "expss" package in R

I try to use the expss packages for survey data analysis, but the result of standard errors, variances and confidence intervals differ of the survey package result.
In survey:
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
svyby(~api99, ~stype, dclus1, svymean)
stype api99 se
E E 607.7917 22.81660
H H 595.7143 41.76400
M M 608.6000 32.56064
In expss:
apiclus1 %>% tab_cells(api99) %>%
tab_rows(stype) %>% tab_weight(pw) %>%
tab_stat_fun(w_mean,w_se) %>% tab_pivot()
| | | | | #Total |
| ----- | -- | ----- | ------ | ------ |
| stype | E | api99 | mean | 607.8 |
| | | | se | 1.6 |
| | H | api99 | mean | 595.7 |
| | | | se | 4.7 |
| | M | api99 | mean | 608.6 |
| | | | se | 3.7 |
How can i use the functions of survey package within expss?

Turing Machine algorithms

I'm trying to determine how a Turing Machine (consisting of only 0's and 1's, no blanks) could recognize a sequence of 8 1's. Every algorithm I've found has a TM searching for an indeterminate number of 1's or 0's, not a specific number.
Essentially, if you have this tape:
1 1 1 1 1 1 1 1 0 0 0 1 0 0 0 1
How can you recognize that the 8 1's represent addition, and you want to add 0 0 0 1 and 0 0 0 1?
I take it that 11111111 is like an opcode and 0001, 0001 are the operands for that opcode. At least, that's the only interpretation I am seeing.
A TM can look for a fixed, finite number of symbols by using a similar fixed, finite number of states, the sole purpose of each one being to recognize that another of the expected symbols has been seen. For instance, here's a four-tape TM that recognizes addition and does the binary addition:
|----|----|----|----|----||----|-----|-----|-----|-----|----|----|----|----|
| Q | T1 | T2 | T3 | T4 || Q' | T1' | T2' | T3' | T4' | D1 | D2 | D3 | D4 |
|----|----|----|----|----||----|-----|-----|-----|-----|----|----|----|----|
// read the opcode /////////////////////////////////////////////////////////
| qA | 1 | x | y | z || qB | 1 | x | y | z | R | S | S | S |
| qB | 1 | x | y | z || qC | 1 | x | y | z | R | S | S | S |
| qC | 1 | x | y | z || qD | 1 | x | y | z | R | S | S | S |
| qD | 1 | x | y | z || qE | 1 | x | y | z | R | S | S | S |
| qE | 1 | x | y | z || qF | 1 | x | y | z | R | S | S | S |
| qF | 1 | x | y | z || qG | 1 | x | y | z | R | S | S | S |
| qG | 1 | x | y | z || qH | 1 | x | y | z | R | S | S | S |
| qH | 1 | x | y | z || qI | 1 | x | y | z | R | S | S | S |
// read the first addend ///////////////////////////////////////////////////
| qI | w | x | y | z || qJ | w | w | y | z | R | R | S | S |
| qJ | w | x | y | z || qK | w | w | y | z | R | R | S | S |
| qK | w | x | y | z || qL | w | w | y | z | R | R | S | S |
| qL | w | x | y | z || qM | w | w | y | z | R | R | S | S |
// read the second addend //////////////////////////////////////////////////
| qM | w | x | y | z || qN | w | x | w | z | R | S | R | S |
| qN | w | x | y | z || qO | w | x | w | z | R | S | R | S |
| qO | w | x | y | z || qP | w | x | w | z | R | S | R | S |
| qP | w | x | y | z || qQ | w | x | w | z | R | S | R | S |
// prepare the output tape /////////////////////////////////////////////////
| qQ | w | x | y | z || qR | w | x | y | z | S | S | S | R |
| qR | w | x | y | z || qS | w | x | y | z | S | S | S | R |
| qS | w | x | y | z || qT | w | x | y | z | S | S | S | R |
| qT | w | x | y | z || qU | w | x | y | z | S | S | S | R |
// handle addition when no carry ///////////////////////////////////////////
| qU | w | 0 | 0 | z || qU | w | 0 | 0 | 0 | S | L | L | L |
| qU | w | 0 | 1 | z || qU | w | 0 | 1 | 1 | S | L | L | L |
| qU | w | 1 | 0 | z || qU | w | 1 | 0 | 1 | S | L | L | L |
| qU | w | 1 | 1 | z || qV | w | 1 | 1 | 0 | S | L | L | L |
| qU | w | B | B | B || hA | w | B | B | B | S | R | R | R |
// handle addition when carry //////////////////////////////////////////////
| qV | w | 0 | 0 | z || qU | w | 0 | 0 | 1 | S | L | L | L |
| qV | w | 0 | 1 | z || qV | w | 0 | 1 | 0 | S | L | L | L |
| qV | w | 1 | 0 | z || qV | w | 1 | 0 | 0 | S | L | L | L |
| qV | w | 1 | 1 | z || qV | w | 1 | 1 | 1 | S | L | L | L |
| qV | w | B | B | B || hA | w | B | B | B | S | R | R | R |
|----|----|----|----|----||----|-----|-----|-----|-----|----|----|----|----|
Legend:
Q: current state
T1: current tape symbol, input tape
T2: current tape symbol, scratch tape #1
T3: current tape symbol, scratch tape #2
T4: current tape symbol, output tape (not used)
Q': state to transition into
T1': symbol to write to input tape (not used)
T2': symbol to write to scratch tape #1
T3': symbol to write to scratch tape #2
T4': symbol to write to output tape
D1: direction to move input tape head
D2: direction to move scratch tape #1 head
D3: direction to move scratch tape #2 head
D4: direction to move output tape head
Conventions:
w, x, y and z are variables and represent either 0 or 1. A transition using all four of these can be thought of as a shorthand notation for writing sixteen (2^4) concrete transitions.
directions are L=left, S=same, R=right.
B is a blank symbol; it can be dispensed with if you add more states to assist U and V in the addition.

How the KMP changes MP tables?

when I study the algorithms for string matching, I met the lecture notes says that for example the pattern is abaab, the Morris-Pratt table is:
| a | b | a | a | b |
| 0 | 0 | 1 | 1 | 2 |
I understand how this is generated but the KMP table given is:
| a | b | a | a | b |
| 0 | -1| 1 | 0 | 2 |
Can anyone help me understand why the 2nd table is like that?
Thanks/

Creating a linkages map

I have an interesting programming problem that I need to solve for a iPhone app that I am currently building. The problem is actually a logic problem that does not need to be specific to any particular programming language.
The app needs to produce a linkages map (apologies if this isn't the right terminology but it makes sense to me). You have the following data:
A=C
B=A
C=O
D=F
E=F
F=G
G=D
H=J
I=L
J=N
K=A
L=O
M=C
N=H
O=E
The letters A through to O can be linked to any other letter. The app needs to follow the links to create a map, so starting with A, A link to C, C link to O, O links to E, E links to F etc
When complete this map would look like the attached photo.
http://i.stack.imgur.com/TEfAs.jpg
The problem I have is that I need to write code that will output any map using any combination of links. So for example another link list might look like
A=B
B=A
C=A
D=A
E=A
F=A
G=A
H=A
I=A
J=A
K=A
L=A
M=A
N=A
O=A
I can't get my head around the pseudocode / logic for drawing the app. There are always 15 letters A-O and a letter can never be linked to itself so A can never = A.
Can anyone help to come up with the logic for drawing the map?
What you want is to draw a graph. There is no canonical graphical representation of a graph. So if you have no constrains how the graph should be drawn, you can simply make a row of the Letters and than draw arches between the letters according to your map,
Little like this (ASCII-ART):
Example
+-----------------------------------------+
+--------------------------------------+ |
+-----------------------------------+ | |
+--------------------------------+ | | |
+-----------------------------+ | | | |
+--------------------------+ | | | | |
+-----------------------+ | | | | | |
+--------------------+ | | | | | | |
+-----------------+ | | | | | | | |
+--------------+ | | | | | | | | |
+-----------+ | | | | | | | | | |
+--------+ | | | | | | | | | | |
+-----+ | | | | | | | | | | | |
| | | | | | | | | | | | | |
A B C D E F G H I J K L M N O
| |
+--+
Example
+-----------------------------+
+-----------------------------+ |
+--+ +-----------------------------------+
| | | | +--------+ | |
A B C D E F G H I J K L M N O
| | | | | | | | | | | |
+-----+ | +--+ | +-----+ | +--------+
| +-----+ | | +-----------+
| | +--+ +-----------------+
| +--------+ |
+-----------------------------+
Look a bit confusing, but you cannot always avoid crossings. [In this example you could, but I did not try to avoid crossing, because they cannot be avoided in the general case.]

How to connect two points without crossing another path?

Let's pretend I have the following grid. I have to connect pairs of letters. Not only the same letters have to be connected, but I must also make sure that the connecting paths don't cross each other. What's the algorithm that could tell me if it is possible to connect all the pairs without crossing paths and the shortest path?
I realize that this is a graph problem and the shortest path part could be solved using BFS. What I am not sure about is the crossing paths.
+---+---+---+---+---+---+---+---+
| A | | | B | | | | |
+-------------------------------+
| | | | | | | | |
+-------------------------------+
| | | B | | | | D | |
+-------------------------------+
| | | | | | | | |
+-------------------------------+
| | C | | | C | | | |
+-------------------------------+
| | | | A | | | | |
+-------------------------------+
| | | | | | | D | |
+-------------------------------+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
This is an NP-complete problem called "Disjoint Connecting Paths". Other than some super-polynomial algorithm (really slow), there are some approximation algorithms (might make a mistake, or is non-optimal).
An Approximation Algorithm for the Disjoint Paths Problem in Even-Degree
Planar Graphs - Jonas Kleinberg (pdf)

Resources