المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Searching words in a matrix



C# Programming
03-28-2009, 01:00 AM
Hi! I am doing a project about a game - a 5x5 grid with letter. The player must search words from the grid.
//I have removed some parts of the method
void SeekWords(string word, int[,] visited, int x, int y)
{
if (!(x < 0 || x > 4 || y < 0 || y > 4) && visited[x, y] == 0)
{
visited[x, y] = 1;
SeekWords(word + Grid[x, y], visited, x - 1, y - 1);
SeekWords(word + Grid[x, y], visited, x - 1, y);
SeekWords(word + Grid[x, y], visited, x - 1, y + 1);
SeekWords(word + Grid[x, y], visited, x, y - 1);
SeekWords(word + Grid[x, y], visited, x + 1, y - 1);
SeekWords(word + Grid[x, y], visited, x + 1, y);
SeekWords(word + Grid[x, y], visited, x + 1, y + 1);
visited[x, y] = 0;
}
}

This method is good, but for words longer than 6 letters it's too slow. Are there other algorithms for searching the matrix which are faster?
Thanks http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif

Still learning...