public static void Main()
char[,] matrix = new char[6, 6] {
{ 'x', 'c','a','s','o','y' },
{ 'x', 'm','a','r','r','y' },
{ 'c', 'u','o','r','t','x' },
{ 'x', 'c','u','l','o','x' },
{ 'x', 'p','u','l','a','x' },
{ 'x', 'p','a','l','o','x' },
WordMatrix w = new WordMatrix(6);
string result = w.searchWord(matrix, 6, "orto");
if (!string.IsNullOrEmpty(result))
Console.WriteLine(result);
solution = new int[N, N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
public string searchWord(char[,] matrix, int length, String word)
for (int i = 0; i < length; i++)
for (int j = 0; j < length; j++)
if (search(matrix, word, i, j, 0, length))
return "Trovato a x:" + i + " y:" + j;
private bool search(char[,] matrix, String word, int row, int col, int index, int N)
if (solution[row,col] != 0 || word[index] != matrix[row,col])
if (index == word.Length - 1)
solution[row,col] = path++;
solution[row,col] = path++;
if (row + 1 < N && search(matrix, word, row + 1, col, index + 1, N))
if (row - 1 >= 0 && search(matrix, word, row - 1, col, index + 1, N))
if (col + 1 < N && search(matrix, word, row, col + 1, index + 1, N))
if (col - 1 >= 0 && search(matrix, word, row, col - 1, index + 1, N))
public void print(int length)
for (int i = 0; i < length; i++)
for (int j = 0; j < length; j++)
Console.Write(" " + solution[i, j]);