-2

I have a puzzle with initial state

R R R G R 
R G G R R
R G G R G
R G G R R
R R R B R 

Where R = Red, G = Green and B = Moveable Blank

and Goal State

R R R R R
R G G G R
R G B G R
R G G G R 
R R R R R

I know in order to move the blank i must apply searching algorithms like DFS, BFS, A* etc

and i know i must create classes:

Node

class Node {
    char board[5][5];
    Node *parent;
};

Tree

class Tree {
     Node *root;
 };

Frontier like Hash Table to detect visited state in O(1) complexity.

So i am just confused how will i start implementing a solution to this puzzle. Can anyone guide me? The operators that i can apply on the blank are up, down, left, right.

Dominique Fortin
  • 2,212
  • 15
  • 20
  • The exercise is not clear: What does it mean to apply 'up' on the blank? Moreover, please show your attempts for a solution. And, if this is a variant of the classical 15-puzzle (which may not be the case) the goal state can't be reached, because you have 9 Rs in the initial state, and only 8 in target state. – Dirk Herrmann Feb 26 '16 at 19:09
  • @DirkHerrmann Whats not clear?, its obvious that if you have ever tried a puzzle, it means to swap the positions in the specific direction. It is a variant, and that was my writing error where i accidentally wrote R as G. – Lovely Princess Feb 26 '16 at 19:25
  • [Here](http://coliru.stacked-crooked.com/a/bf164639becd94c1) is a solution I wrote. – wally Mar 05 '16 at 15:43

1 Answers1

3

The simplest idea would be to initialize the root node with the initial state. Then populate the next layer; write a procedure which generates the child nodes according to the blank space movement rules. You should be careful here; when the blank space is at the borders of the board, some movements would be invalid. In such a case, a sketch of A* algorithm can be drawn like that: Define your distance from the initial state as g(n). This may be the number of differently placed letters compared to the initial state, given the current state. Define a heuristic h(n), which gives your current distance from the goal state, which may be the number of differently placed letters compared to the goal state. Then in your current location in the tree, try to pick the next state, which minimizes f(n)=g(n)+h(n). I am not in a position to deeply analyze that right now, but I believe this approach may be much more efficient than brute force DFS or BFS approaches.

Ufuk Can Bicici
  • 3,589
  • 4
  • 28
  • 57
  • Thats what i am doing, root is initial state, operators produce tree branches, according to BFS i traverse the whole level, DFS whole depth, but i am not understanding how will i completely apply this on a puzzle. – Lovely Princess Feb 26 '16 at 19:26
  • and heuristic seems great idea (y) – Lovely Princess Feb 26 '16 at 19:26
  • According to the A* rules, just follow the path which minimizes f(n)=g(n)+h(n). Populate only the children as needed. You need to keep the track of all the nodes you have populated and their f(n)s, use any appropriate data structure for that. At any level in the tree, it may turn out that the path you are following is not the most efficient one. So, you need to go back and select a previous node in lower levels, whose f(n) is lower. Just be careful to keep track of the nodes you have already visited and you have discovered but not just visited, in separate buffers. – Ufuk Can Bicici Feb 26 '16 at 19:32
  • 1
    IIRC: The maximum manhattan distance heuristic works well on the sliding tile puzzle problem. – AndyG Feb 26 '16 at 19:34