我正在尝试解决 CodeFights 的技能测试,但在尝试解决他们的 word boggle 算法时遇到了问题。这是挑战的描述:
Boggle 是一种流行的文字游戏,玩家试图在矩形板上的相邻字母序列中找到单词。
给定一个表示 Boggle 板的字符单元的二维数组板和一个唯一字符串单词的数组,从板上可以形成的单词中找到所有可能的单词。
请注意,在 Boggle 中,当您查找一个单词时,您可以从一个单元格移动到其 8 个相邻单元格中的任何一个,但您不能在一个单词中两次使用同一个单元格。
我写了一个看起来可以工作的代码,但是它没有通过他们拥有的隐藏测试,现在我被困在了这一点上。
这是我的代码:
string[] wordBoggle(char[][] board, string[] words)
{
string[] foundWords = new string[words.Length];
for (int i = 0; i < words.Length; i++)
{
List<int[]> Prefixs = new List<int[]>();
for (int j = 0; j < board.Length; j++)
{
for (int k = 0; k < board[j].Length; k++)
{
if (board[j][k] == words[i][0])
{
int[] match = new int[] { j, k };
Prefixs.Add(match);
}
}
}
if (Prefixs.Count > 0)
{
for (int l = 0; l < Prefixs.Count; l++)
{
bool[][] bitMap = new bool[board.Length][];
ResetBitMap(bitMap, board.Length, board[0].Length);
bitMap[Prefixs[l][0]][Prefixs[l][1]] = true;
bool match = search(Prefixs[l][0], Prefixs[l][1], board, words[i], bitMap, 1);
if (match)
{
foundWords[i] = words[i];
break;
}
}
}
}
var res = foundWords.Where(q => q != null).OrderBy(e => e).Distinct().ToArray();
return res;
}
bool search(int col, int row, char[][] board, string word, bool[][] bitMap, int index)
{
var found = false;
if (index == word.Length)
{
return true;
}
Dictionary<int[], char> neighbors = getNeightbors(col, row, board);
var allNextMatchs = neighbors.Where(a => a.Value == word[index]).ToArray();
if (allNextMatchs.Length < 1 && index < word.Length - 1)
{
return false;
}
var count = 0;
for (int i = 0; i < bitMap.Length; i++)
{
for (int k = 0; k < bitMap[i].Length; k++)
{
if (bitMap[i][k] == true)
{
count++;
}
}
}
if (count == word.Length)
{
return true;
}
foreach (var item in allNextMatchs)
{
if (item.Value != 0 && bitMap[item.Key[0]][item.Key[1]] == false)
{
col = item.Key[0];
row = item.Key[1];
bitMap[col][row] = true;
if (index < word.Length - 1)
{
index += 1;
}
found = search(col, row, board, word, bitMap, index);
if (found)
{
return true;
}
else
{
ResetBitMap(bitMap, board.Length, board[0].Length);
}
}
else
{
continue;
}
}
return found;
}
Dictionary<int[], char> getNeightbors(int col, int row, char[][] board)
{
Dictionary<int[], char> neighbors = new Dictionary<int[], char>(8);
try
{
int[] cr = new int[] { col - 1, row - 1 };
neighbors.Add(cr, board[col - 1][row - 1]); ; // left up
}
catch (IndexOutOfRangeException e) {
Console.WriteLine(e.Message);
}
try
{
int[] cr = new int[] { col, row - 1 };
neighbors.Add(cr, board[col][row - 1]);
}
catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
try
{
int[] cr = new int[] { col + 1, row - 1 };
neighbors.Add(cr, board[col + 1][row - 1]);
}
catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
try
{
int[] cr = new int[] { col - 1, row };
neighbors.Add(cr, board[col - 1][row]);
}
catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
try
{
int[] cr = new int[] { col + 1, row };
neighbors.Add(cr, board[col + 1][row]);
}
catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
try
{
int[] cr = new int[] { col + 1, row + 1 };
neighbors.Add(cr, board[col + 1][row + 1]);
}
catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
try
{
int[] cr = new int[] { col, row + 1 };
neighbors.Add(cr, board[col][row + 1]);
}
catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
try
{
int[] cr = new int[] { col - 1, row + 1 };
neighbors.Add(cr, board[col - 1][row + 1]);
}
catch (IndexOutOfRangeException e) { Console.WriteLine(e.Message); }
return neighbors;
}
void ResetBitMap(bool[][] map, int col, int row)
{
for (int i = 0; i < map.Length; ++i)
{
map[i] = new bool[row];
}
for (int j = 0; j < map.Length; ++j)
{
for (int k = 0; k < map[0].Length; ++k)
{
map[j][k] = false;
}
}
}
有谁知道我在这里想念什么?