using System; |
using System.Collections.Generic; |
using System.Text; |
using System.Collections; |
|
namespace ConsoleApplication1 |
{ |
class Algorithms |
{ |
private static Hashtable Tree = new Hashtable(); |
|
private static char[] charArray = new char[9]; |
|
//the entry point method of this class. Get's called everytime a next move using DFS is required. |
static public Hashtable solvePuzzle() |
{ |
if (DFS("316498527", "")) |
{ |
return Tree; |
} |
|
return new Hashtable(); |
} |
|
static private bool isValidMove(int direction, string board) |
{ |
int pos = board.IndexOf("9"); |
if ((pos < 0) || (pos > 8)) |
return false; |
else if ((direction == 0) && ((pos % 3) != 0)) //Left |
return true; |
else if ((direction == 2) && (((pos + 1) % 3) != 0)) //Right |
return true; |
else if ((direction == 3) && (!((pos + 3) > 8))) // Down |
return true; |
else if ((direction == 1) && (!((pos - 3) < 0))) // Up |
return true; |
return false; |
} |
|
static private bool isVisitedState(string board) |
{ |
if (Tree.ContainsKey(board)) |
{ |
return true; |
} |
return false; |
} |
|
static private Queue BFqueue = new Queue(); |
|
static private bool BFS(string board, string parent) |
{ |
string[] boardParent = new string[2]; |
boardParent[0] = board; |
boardParent[1] = parent; |
State S = new State(board, parent); |
Tree.Add(board, S); |
BFqueue.Enqueue(boardParent); |
return BFS(); |
} |
static private bool BFS() |
{ |
string[] boardParent = (string[])BFqueue.Dequeue(); |
if (isFinalState(boardParent[0])) |
return true; |
expandChildren(boardParent[0], ref BFqueue); |
if (BFS()) |
return true; |
return false; //should never be hit ;) |
} |
|
static private Stack DFstack = new Stack(); |
|
static private bool DFS(string board, string parent) |
{ |
string[] boardParent = new string[2]; |
boardParent[0] = board; |
boardParent[1] = parent; |
State S = new State(board, parent); |
Tree.Add(board, S); |
DFstack.Push(boardParent); |
return DFS(); |
} |
static private bool DFS() |
{ |
string[] boardParent = (string[])DFstack.Pop(); |
if (isFinalState(boardParent[0])) |
return true; |
expandChildren(boardParent[0], ref DFstack); |
if (DFS()) |
return true; |
return false; |
} |
|
static private void expandChildren(string board, ref Queue queue) |
{ |
string boardboardParent = board; |
if (isValidMove(2, board)) //test right |
{ |
moveRight(ref board); |
if (isVisitedState(board)) |
{ |
moveLeft(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Enqueue(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveLeft(ref board); |
} |
} |
if (isValidMove(0, board)) //test left |
{ |
moveLeft(ref board); |
if (isVisitedState(board)) |
{ |
moveRight(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Enqueue(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveRight(ref board); |
} |
} |
|
if (isValidMove(1, board)) //test up |
{ |
moveUp(ref board); |
if (isVisitedState(board)) |
{ |
moveDown(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Enqueue(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveDown(ref board); |
} |
} |
|
if (isValidMove(3, board)) //test down |
{ |
moveDown(ref board); |
if (isVisitedState(board)) |
{ |
moveUp(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Enqueue(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveUp(ref board); |
} |
} |
} |
static private void expandChildren(string board, ref Stack queue) |
{ |
string boardboardParent = board; |
if (isValidMove(2, board)) //test right |
{ |
moveRight(ref board); |
if (isVisitedState(board)) |
{ |
moveLeft(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Push(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveLeft(ref board); |
} |
} |
if (isValidMove(0, board)) //test left |
{ |
moveLeft(ref board); |
if (isVisitedState(board)) |
{ |
moveRight(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Push(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveRight(ref board); |
} |
} |
|
if (isValidMove(1, board)) //test up |
{ |
moveUp(ref board); |
if (isVisitedState(board)) |
{ |
moveDown(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Push(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveDown(ref board); |
} |
} |
|
if (isValidMove(3, board)) //test down |
{ |
moveDown(ref board); |
if (isVisitedState(board)) |
{ |
moveUp(ref board); |
} |
else |
{ |
string[] boardParentArray = new string[2]; |
boardParentArray[0] = board; |
boardParentArray[1] = boardParent; |
queue.Push(boardParentArray); |
State S = new State(board, boardParent); |
Tree.Add(board, S); |
moveUp(ref board); |
} |
} |
} |
|
static private void movePuzzle(int direction, ref string board) |
{ |
if (direction == 0) //Move Left |
{ |
moveLeft(ref board); |
} |
else if (direction == 1) //Move Up |
{ |
moveUp(ref board); |
} |
else if (direction == 2) //Move Right |
{ |
moveRight(ref board); |
} |
else if (direction == 3) //Move Down |
{ |
moveDown(ref board); |
} |
|
} |
public static void moveLeft(ref string board) |
{ |
int pos = board.IndexOf("9"); |
|
//char[] charArray = new char[9]; |
charArray = board.ToCharArray(); |
charArray[pos] = board[pos - 1]; |
charArray[pos - 1] = '9'; |
|
board = new string(charArray); |
} |
|
public static void moveRight(ref string board) |
{ |
int pos = board.IndexOf("9"); |
|
//char[] charArray = new char[9]; |
charArray = board.ToCharArray(); |
charArray[pos] = board[pos + 1]; |
charArray[pos + 1] = '9'; |
|
board = new string(charArray); |
} |
|
public static void moveUp(ref string board) |
{ |
int pos = board.IndexOf("9"); |
|
//char[] charArray = new char[9]; |
charArray = board.ToCharArray(); |
charArray[pos] = board[pos - 3]; |
charArray[pos - 3] = '9'; |
|
board = new string(charArray); |
} |
|
public static void moveDown(ref string board) |
{ |
int pos = board.IndexOf("9"); |
|
//char[] charArray = new char[9]; |
charArray = board.ToCharArray(); |
charArray[pos] = board[pos + 3]; |
charArray[pos + 3] = '9'; |
|
board = new string(charArray); |
} |
|
public static bool isFinalState(string board) |
{ |
if (board == "123456789") |
return true; |
return false; |
} |
} |
} |
|