Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i want a solution for solving 8 puzzle whit RBFS.please help me!
Posted
Comments
Sergey Alexandrovich Kryukov 26-Oct-11 13:55pm    
Not a question. Help with what? What did you try?
--SA
Sergey Alexandrovich Kryukov 26-Oct-11 13:57pm    
What is "solving puzzle"? Is it really about programming? Because "solving a puzzle" normally means what a consumers of a puzzle do, not a software developer?
--SA
Maximilien 26-Oct-11 14:05pm    
what is RBFS ?
Fariba Rasouli 26-Oct-11 14:12pm    
Recursive Best-First Search:
RBFS is a linear-space algorithm that expands nodes in best-first order even with a non-monotonic cost function and generates fewer nodes than iterative deepening with a monotonic cost function.
-----------
i want Solution of Eight Puzzle Using RBFS.

You know what we need? Really, really nead?

World Peace? Would be nice, but not that likely really.

Fair shares for all? Again, good idea.

Decent music produced by Simon Cowel? Now you're just being silly.

An internet site that could look up thing you want to know about? That would be handy, but where would we find such a thing.


What! The last one exists! Why didn't they tell anyone about it? What's that Sooty? It's called Goggle or something? Well, I wish I'd known about that!

Goggle look for: puzzle whit RBFS[^]
 
Share this answer
 
Comments
Fariba Rasouli 26-Oct-11 14:18pm    
thanks alot for source code.
Manfred Rudolf Bihy 26-Oct-11 14:23pm    
What, no fries? ;)
5+
OriginalGriff 26-Oct-11 14:28pm    
Not right now - I've just eaten. Thanks for the offer though!
Manfred Rudolf Bihy 26-Oct-11 14:51pm    
Actually I'll be going after a couple of bacon cheese burgers with a side order of onion rings and chili cheese nuggets. :drool: :)

No fries today.
So long!
You should be doing your own homework, but I will give you some pointers.

What you first need to do is define a board state. Lets say the number 0 represents the empty space and numbers 1-8 represent the tiles.

So, we will have a 3x3 2D array of integers.

C++
typedef struct {
	int Board[3][3];
} BoardState;


Now we need to define valid moves. Lets call these Up, Down, Left and Right, which are the respective directions that we will move the hole.

C++
typedef enum {
	Up,
	Down,
	Left,
	Right,
} Moves;


You will then need to define functions to apply each move to the board and to check if a move is valid. You can do this.
C++
bool IsMoveValid(const BoardState &Board, Moves move);
void MoveUp(const BoardState &OrigBoard, BoardState *pNewBoard);
void MoveDown(const BoardState &OrigBoard, BoardState *pNewBoard);
void MoveLeft(const BoardState &OrigBoard, BoardState *pNewBoard);
void MoveRight(const BoardState &OrigBoard, BoardState *pNewBoard);


Now what you need is a starting board.

C++
BoardState board = {
	{ 0, 1, 2 },
	{ 3, 4, 5 },
	{ 6, 7, 8 },
};
MyTreeNode<boardstate> TreeRoot(board); //MyTree is a tree structure class that you have to write. This one is templated, and takes 1 parameter, which is the value stored at the root node.
</boardstate>


From this we now need to build a tree of all the possible moves we can make.
For the first case on my example board, these moves would be Right and Down. We will use a generic approach tho.

C++
void GenerateChildren(const MyTreeNode &node) {
	BoardState temp;
	if (IsValidMove(node.board, Left)) {
		MoveLeft(node.board, temp);
		node.AddChild(new MyTreeNode(temp));
	}
	//Do the same for other directions
}


Once we have a tree made, we can use a Recursive Breadth First Search (RBFS) to search it for the path to the solution. RBFS goes across the tree first, then into children.
To do this you will need to (you have to figure out the order):
a. Check the current node to see if it the solution
b. Check all child nodes to see if they are the solution


Hopefully I have given you enough so you can do it on your own now without giving you all the answers. Give it a try (Wikipedia helps). If you are still having trouble come back with code WHEN YOU HAVE TRIED SOMETHING.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 26-Oct-11 14:21pm    
Not really! It's the "Recursive Best First Search" here. Breadth first would not work as the decision tree grows exponentially. Otherwise great effort for getting OP started! 5+
Andrew Brock 26-Oct-11 14:23pm    
I imagine that there was mention of a heuristic in the assignment sheet, but I'm just giving pointers. Can't be doing their homework, I have my own thesis to work on.
Manfred Rudolf Bihy 26-Oct-11 15:06pm    
Yes, that is what Recursive Best First Search is about: Using a heuristic to determine which way to go as exhaustive search forbids itself because of the exponential complexity.
Good luck and success with your thesis. What is your thesis about if I may ask?
Andrew Brock 26-Oct-11 15:16pm    
Thanks. The official title is "Improving the Performance of Weather Simulations using Graphical Processing Units". Pretty much what it says, using graphics cards, mainly GTX580s to make weather forecasting faster. The deadline was 2 days ago so it is done, now I have all the other stuff to do, like 4 presentations and writing a paper for a journal.
Fariba Rasouli 26-Oct-11 14:42pm    
Thanks for your attention.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900