Skip to content

Instantly share code, notes, and snippets.

@raphaelrk
Last active October 1, 2015 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphaelrk/8648f5e0ef188520ba67 to your computer and use it in GitHub Desktop.
Save raphaelrk/8648f5e0ef188520ba67 to your computer and use it in GitHub Desktop.
Board B:
[11 12 ... 1n]
[21 22 ... 2n]
[.. .. ... ..]
[n1 n2 ... nn]
Board contains unique elements 0,1,...,n-1
We will denote 0 as 'E'
Want board s.t. element at (r,c) = (rn + c + 1) % n*n
AKA board counts up from 1 as you go row by, column by column,
with the bottom-right corner equaling E (0)
Call this desired board D
The location of an element i in the board is denoted by the bijective function
B(i) -> (r,c) say r = B(i).r, c = B(i).c
B^-1(r, c) -> (i)
The location of an element i in the desired board is denoted by the bijective function
D(i) = ((i-i%n)/n , i%n) say r = D(i).r, say c = D(i).c
D^-1(r, c) = (nr + c + 1)%16
swap swaps the location of two elements/the elements at two locations
move (element a) to (pos b) is defined as such:
if a is E
using A* with placed blocks as obstacles, swap places
until E at b
else
get A* path of a to b with placed blocks as obstacles
for every movement in that path
move B to the place where the movement goes, with
placed blocks and a as obstacles
swap B and a
for the first n-2 rows:
for the first n-2 cols:
d = D(r, c)
i = B(d)
move i to d
/**
using A*,
calculate the path from (i.r, i.c) to (r, c)
with already placed elements [1...index) as obstacles
place that movement in an array
for every movement from (i.r, i.c) to (r, c)
e = B(E)
calculate path from (e.r, e.c) to next movement in array
already placed elements and i are obstacles
store movements in array
execute that movement of E
swap E and i
**/
// for the last two columns
di = D(row, n-1)
i = B(di)
dj = D(row, n)
j = D(dj)
move j to di
move i to (di.r + 1, di.c)
move E to (di.r, di.c + 1)
swap E and j
swap E and i
// for the last two rows
for the first n-2 columns
di = D(n-1, c)
i = B(d)
dj = D(n, c)
j = B(d)
move j to di
move i to (di.r, di.c + 1)
move E to (di.r+1, di.c)
swap E and j
swap E and i
// for the last two colums
move E to (n, n)
DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment