BrainDen.com - Brain Teasers
• 0

## Question

There are many slide 15 algorithms on the web. I need a slide 16 algorithm.

``` 1  2  3  4  The solved puzzle is to the left but it starts off with numbers

5  6  7  8  at random positions. These entire rows slide. So if you slide

9 10 11 12  the top row right you will get 4 1 2 3. Slide it left you will

13 14 15 16  get 2 3 4 1. The column starting with 2, slide it up you will

get 6 10 14 2, slide it down you get 14 2 6 10. I need the

13  8  6 12  algorithm (not the code) as I will add it to my computer program.

4 13  9 10  Lastly, is there another algorithm to state, that this random

11  7  1  5       sequence, is "unsolvable".   Thanks !

2 16 14  3  A starting sequence may look like the bottom puzzle.```

Edited by rookie1ja
formatting changed

## Recommended Posts

• 0

sorry, the post lost some of the formatting but hopefully it is

still understandable.

##### Share on other sites

• 0

for future referance, use [ c o d e ] [/ c o d e ] tags to keep spacing.

one way to solve would be to make a tree that stores the results after every move.

once you hit a winning move, you have your solution.

exe:

```
def slider(path,state):

if state == solution:

return path

queue = []

for i in range(0,len(state)):

queue += rotate(state,i)

queue += rotate(state,i+len(state)+len(state[0]))

for i in range(0,len(state[0])):

queue += rotate(state,i+len(state))

queue += rotate(state,i+2*len(state)+len(state[0]))

for i in range(0,len(queue)):

if queue[i] == solution:

return i

for i in range(0,len(queue)):

return slider(i,queue[i])

```

##### Share on other sites

• 0

^ For each state, you have 16 possible moves, if the solution required let's say only 10 moves then you'd have a tree with 1610leaves, not to mention how many branches, and also your program might get stuck forever trying the same moves over and over again...

##### Share on other sites

• 0

To be able to write a program to solve it you first need to know how to solve it at all, so far all I have is a method to solve all but the bottom row:

You start with the top row, left digit, the number that should go there (1) you put in the bottom row anywhere you want except for the column you want to put it in, then what you do is you take the left column down (as in slide it down three times) then move the bottom row until your digit is in it's column, then take the column up, repeat this fo the 2nd 3rd and 4th columns then move on to the second row and do the whole thing again, this way each row you complete won't affect the rows above it, it's alot like my method in the Rubix Cube, though U can't solve the bottom row in the Rubic Cube either...

##### Share on other sites

• 0

while I don't know how to write I flowchart for what you described (and I'll save that until we have the whole puzzle done), thanks, what you said indeed solves the first 12 cells. Two of the final four just fell in the correct position by accident for 14/16. Moreover, once I tried it a few times it is both simple and logical to understand. Now for a full solution and flowchart. Great !!

##### Share on other sites

• 0

by following your method, I san colve the first three rows. Thanks for the mthod and thanks also to one who helped format my message. After solving 3 rows, you can slide the 4th row so as to have a minimum of 14 out of 16 correct. By sliding the 13 to left 4th row, that gives you 13 right and there is a one in 3 chance, that 14 is in the right place, if it is, there is a one in 2 chance that 15 (and 16) is in the right place. This means (apporiximately) that solve the first 3 rows and you have a 1 in 6 chance of solving the puzzle, by luck alone. Two things missing, the 4th row solutiom and flowchart/algorithm to get there.

##### Share on other sites

• 0

For the heck of it (and cause drawing charts sounds real painful, I haven't held a pencil in months) here's a simulator:

The way it's coded will make it ery easy to put in an "automatic" solver, someone just tell me the algorithm and I'll apply it...

Edited by Anza Power
##### Share on other sites

• 0

I've played for a while with Anza Power's simulator and I've managed to swap "1" and "2" in original position.

This means that it is possible to swap any two horizontally adjacent cells, you just have to:

1. Make a couple of obvious moves to bring these two cells in place where "1" and "2" originally are.

2. Apply the procedure that swaps cells "1" and "2".

3. Undo moves made in step 1 (perform "opposite" moves in reversed order).

Exactly the same can be done for every vertically adjacent cells due to the symmetry of the puzzle,

you can easily transform procedure swapping "1" and "2" into procedure swapping "1" and "4".

Now when you know how to swap any two adjacent (horizontally or vertically) cells,

it is easy to solve every possible puzzle: you can just put numbers into their cells one by one.

First put 1 in place, then 2 without touching 1, then 3 without touching 1 and 2, etc.

Alternatively you can solve first three rows using method described by Anza Power and use above for the last row.

All you have to do is to find a sequence of moves that swaps "1" and "2" (possibly by trial and error).

And such a sequence exists, I've checked it.

Edited by witzar
##### Share on other sites

• 0

For the heck of it (and cause drawing charts sounds real painful, I haven't held a pencil in months) here's a simulator:

The way it's coded will make it ery easy to put in an "automatic" solver, someone just tell me the algorithm and I'll apply it...

in the version I have you can slide left to right and right to left,up and down.

nice looking app. Written in ???

##### Share on other sites

• 0

^ Meh, was too lazy to do it...

Here's two versions with 16 arrows:

http://anzapower.webs.com/Flash/16Slider.swf

##### Share on other sites

• 0

OK, here is the sequence of moves that swaps cells 1 and 2:

4D, 1R, 4U, 3D, 1R, 1R, 3U, 4D, 1L, 4U, 1L, 3D, 1L, 3U, 4D, 1R, 1R, 4U, 3D, 1R, 3U.

It should be read as follows:

4D = "slide 4th column Down",

1R = "slide 1st row Right",

etc (U=Up, L=Left).

Above sequence is not the shortest possible, but it completes the algorithm for solving any puzzle,

that I've described in my previous post.

##### Share on other sites

• 0

the answer does not work. Any 'move' with 4 such 4d 4u returns the puzzle to the exact same place. In other words they do nothing and should be omitted.

##### Share on other sites

• 0

the answer does not work. Any 'move' with 4 such 4d 4u returns the puzzle to the exact same place. In other words they do nothing and should be omitted.

You didn't get it. Move "4U" means: move 4th column Up (just one cell). (If you repeat this move 4 times, then you will return to the same position.)

##### Share on other sites

• 0

SOLVED! Using witzar's code as the final piece of the puzzle (though I flipped it 180°) and the algorithm I posted before, the final is ready and works, though it might not always give the shortest solution, but at least there's always a solution:

(Click the white triangle button on the bottom right then scramble it and solve, if you don't see it exit this page and clear your cache)

The scramble button scrambles the board in a method unrelated to the way the game works (as in it takes out all the numbers and puts them back randomly and not press random buttons)

If anyone's interested, this is the code for the main solver script:

It duplicates the board then tries to solve it while recording all the moves in the moves string, when it's done it moves on and another symbol comes and does the actions that are in the string...

```var a=new Array();

var moves="";

var m=0;

for(var i=0;i<4;i++){

a[i]=new Array();

for(var j=0;j<4;j++)

a[i][j]=_root.Board.num[i][j];

}

function colDown(n){

temp=a[3][n];

a[3][n]=a[2][n];

a[2][n]=a[1][n];

a[1][n]=a[0][n];

a[0][n]=temp;

moves+="D"+(n+1)+" ";

}

function colUp(n){

temp=a[0][n];

a[0][n]=a[1][n];

a[1][n]=a[2][n];

a[2][n]=a[3][n];

a[3][n]=temp;

moves+="U"+(n+1)+" ";

}

function rowRight(n){

temp=a[n][3];

a[n][3]=a[n][2];

a[n][2]=a[n][1];

a[n][1]=a[n][0];

a[n][0]=temp;

moves+="R"+(n+1)+" ";

}

function rowLeft(n){

temp=a[n][0];

a[n][0]=a[n][1];

a[n][1]=a[n][2];

a[n][2]=a[n][3];

a[n][3]=temp;

moves+="L"+(n+1)+" ";

}

function switchTwo(){

colUp(0);

rowLeft(3);

colDown(0);

colUp(1);

rowLeft(3);

rowLeft(3);

colDown(1);

colUp(0);

rowRight(3);

colDown(0);

rowRight(3);

colUp(1);

rowRight(3);

colDown(1);

colUp(0);

rowLeft(3);

rowLeft(3);

colDown(0);

colUp(1);

rowLeft(3);

colDown(1);

}

var r;

var c;

for(var i=0;i<4;i++)

trace(a[i]);

for(var n=1;n<13;n++){

for(var i=0;i<4;i++)

for(var j=0;j<4;j++)

if(a[i][j]==n){

r=i;

c=j;

}

if(c!=(n-1)%4||r!=Math.floor((n-1)/4)){

if(r==Math.floor((n-1)/4)){

colDown(c);

r++;

r%=4;

rowLeft(r);

colUp(c);

c+=3;

c%=4;

}

if(c==(n-1)%4){

rowRight(r);

c++;

c%=4;

}

for(var i=0;i<r-Math.floor((n-1)/4);i++)

colDown((n-1)%4);

for(var i=0;i<c-(n-1)%4;i++)

rowLeft(r);

for(var i=0;i<(n-1)%4-c;i++)

rowRight(r);

for(var i=0;i<r-Math.floor((n-1)/4);i++)

colUp((n-1)%4);

}

moves+="|| ";

}

//For the Last Row

while(a[3][0]!=13)

rowLeft(3);

if(a[3][3]==14)

switchTwo();

if(a[3][2]==14){

rowRight(3);

switchTwo();

rowLeft(3);

}

if(a[3][3]==15)

switchTwo();

trace("moves="+this.moves);

trace("moves.length="+this.moves.length);

for(var i=0;i<4;i++)

trace(a[i]);

```

##### Share on other sites

• 0

I am extremely impressed !

##### Share on other sites

• 0

I've played for a while with Anza Power's simulator and I've managed to swap "1" and "2" in original position.

This means that it is possible to swap any two horizontally adjacent cells, you just have to:

1. Make a couple of obvious moves to bring these two cells in place where "1" and "2" originally are.

2. Apply the procedure that swaps cells "1" and "2".

3. Undo moves made in step 1 (perform "opposite" moves in reversed order).

Exactly the same can be done for every vertically adjacent cells due to the symmetry of the puzzle,

you can easily transform procedure swapping "1" and "2" into procedure swapping "1" and "4".

Now when you know how to swap any two adjacent (horizontally or vertically) cells,

it is easy to solve every possible puzzle: you can just put numbers into their cells one by one.

First put 1 in place, then 2 without touching 1, then 3 without touching 1 and 2, etc.

Alternatively you can solve first three rows using method described by Anza Power and use above for the last row.

All you have to do is to find a sequence of moves that swaps "1" and "2" (possibly by trial and error).

And such a sequence exists, I've checked it.

got it and I verified itr workks perfectly. I need exactly that for pairs 13-14, 14-15,15-16.

That should do it.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.