public bool Exist(char[][] board, string word)
{
for (int i = 0; i < board.Length; ++i)
{
for (int j = 0; j < board[0].Length; ++j)
{
if (board[i][j] == word[0])
{
if (this.Search(board, i, j, word, 0))
{
return true;
}
}
}
}
return false;
}
private bool Search(char[][] board, int i, int j, string word, int currIndex)
{
if (currIndex == word.Length)
{
return true;
}
if (!this.Check(board, i, j, word[currIndex]))
{
return false;
}
char temp = board[i][j];
board[i][j] = '#';
bool result = this.Search(board, i - 1, j, word, currIndex + 1) ||
this.Search(board, i + 1, j, word, currIndex + 1) ||
this.Search(board, i, j - 1, word, currIndex + 1) ||
this.Search(board, i, j + 1, word, currIndex + 1);
board[i][j] = temp;
return result;
}
private bool Check(char[][] board, int i, int j, char ch)
{
if (i >= 0 && i < board.Length && j >= 0 && j < board[0].Length && board[i][j] == ch)
return true;
return false;
}
sir, in this problem string can be either vertical only or horizontal only. this program is serching string as
ReplyDelete"|m| a a a a a a a a",
"|i| a a t a a a a a",
"|c| r a f a a a a a",
"|r| s s o f t a a a",
"|o||s| a a a a a a a",
" t |o||f|a a a a a a",
" o a |t|a a a a a a",
" f a a a a a a a a",
" t a a a a a a a a",
" a a a a a a a a a"
Again there is mistake in Problem Statement.
ReplyDeleteThanks for mentioning it.