

Hi buddies,

Though I want to give you some new stuff regularly,
I fail to do so as I am not connected to the internet
for most of the time and even I am connected to the
net my work don't permit me to do much.

But still, I will try my best!


I Don't force you to include my name and address in
every distribution of this program... its upto you.
If you can, ie. if the usage permits please do it,
(It may add to my page rank in google!, and I am
desperate about that).

----------------------------------------------------
Here is a simple program for you.  This was asked in
Campus Interview/Apti test by some company at BITS,
Pilani, and was also asked by JUNO, for testing their 
employees. 

Though this problem can not be solved for some cases
(often found), this program attempts to solve this
problem using a simple recursion. (And quits if it 
can't; but only after trying its best!).
-----------------------------------------------------

Problem:

Write a program demonstrating the steps to arrange the 
numbered cells in a square board in ascending order 
from the left most top to the right most bottom, 
using one empty cell!

Example:

puzzle:
    7    5    3
    8    4    6
    1    0    2

solution:
    1    2    3
    4    5    6
    7    8    0

Assumptions:

1. 0 - zero, stands for a blank/empty cell and has the maximum
   value for the cell, you can assume this as infinity;
2. You can also assume that the numbers are the natural numbers
   starting from 1 to the no. of cells (n) - 1 ie [1,n-1].


The input to the program can be a file, or the program 
should randomly generate a board of given dimension.
--------------------------------------------------------

Sample out put for the board:

    7    5    3
    8    4    6
    1    0    2

Output:
    7    5    3
    8    4    6
    1    0    2

    7    5    3
    8    4    6
    1    2    0


    7    5    3
    8    4    0
    1    2    6


    7    5    0
    8    4    3
    1    2    6


    7    0    5
    8    4    3
    1    2    6


    0    7    5
    8    4    3
    1    2    6


    8    7    5
    0    4    3
    1    2    6


    8    7    5
    1    4    3
    0    2    6


    8    7    5
    1    4    3
    2    0    6


    8    7    5
    1    4    3
    2    6    0


    8    7    5
    1    4    0
    2    6    3


    8    7    0
    1    4    5
    2    6    3


    8    0    7
    1    4    5
    2    6    3


    0    8    7
    1    4    5
    2    6    3


    1    8    7
    0    4    5
    2    6    3


    1    8    7
    4    0    5
    2    6    3


    1    0    7
    4    8    5
    2    6    3


    1    7    0
    4    8    5
    2    6    3


    1    7    5
    4    8    0
    2    6    3


    1    7    5
    4    8    3
    2    6    0


    1    7    5
    4    8    3
    2    0    6


    1    7    5
    4    8    3
    0    2    6


    1    7    5
    0    8    3
    4    2    6


    1    7    5
    8    0    3
    4    2    6


    1    7    5
    8    2    3
    4    0    6


    1    7    5
    8    2    3
    4    6    0


    1    7    5
    8    2    0
    4    6    3


    1    7    0
    8    2    5
    4    6    3


    1    0    7
    8    2    5
    4    6    3


    1    2    7
    8    0    5
    4    6    3


    1    2    7
    8    5    0
    4    6    3


    1    2    7
    8    5    3
    4    6    0


    1    2    3
    8    6    7
    4    5    0


    1    2    3
    8    6    0
    4    5    7


    1    2    3
    8    0    6
    4    5    7


    1    2    3
    0    8    6
    4    5    7


    1    2    3
    4    8    6
    0    5    7


    1    2    3
    4    8    6
    5    0    7


    1    2    3
    4    8    6
    5    7    0


    1    2    3
    4    6    8
    7    5    0


    1    2    3
    4    6    0
    7    5    8


    1    2    3
    4    0    6
    7    5    8


    1    2    3
    4    5    6
    7    0    8


    1    2    3
    4    5    6
    7    8    0
---------------------------------------------------------
About Solution:

THIS IS NEITHER THE ONLY SOLUTION NOR THE BEST SOLUTION.
I just tried to solve the proble with some simple recursive
algorithm which striked my mind instantly!  So, I din't 
calculate the complexities of this algo or performance of
the program.  This is just one way...  not the only way.

Logic:

If a cell wants to move to left, it asks the left one to
move to left, and if that can't move to left it asks its
left one to move to left and so on.  If any of them can 
not move so, as they or on the border of the board or the
next one is FIXED, they will try to move in the other two
direction (but not on the opposite direction, ie, if asked
to move to left, the cell tries to move to up or down but
not to right).  If at any stage, if any of the cells could
move, the recursion unfolds and the requesting cell takes
the requested position.

In failure (according to this algo, this occurs only at the
corners and that too just before the cell takes it destined
position), the cell tries for a SWAP procedure with the 
destination cell.

If this is also failed, then there is no possible solution
for this board!! (Try, I am not very sure!).


To bring an order and to extend the problem to any n X n 
board, first the problem is reduced from n X n to 
(n-1) X (n-1) and so on to 2 X 2 where the problem is 
determined wether solvable or not.

This reduction is done by first arranging the border elements 
in their destination positions.

---------------------------------------------------------------
The the steps are not optimized.... I think the solution is!!
---------------------------------------------------------------

With regards and 
Best wishes for whatever you do,
ramu 

mail: rk_gaddipati@yahoo.com
url : http://www.geocities.com/rk_gaddipati

FSF - Free Software Foundation.
