首先获取一行的所有字母。然后将第一个字母复制到一个字符串中并与字典进行比较。接下来,你取前两个字母并做同样的事情。当您达到 7 个字符长度时,您将最后 6 个字母与字典进行比较。当您完成该行中的所有检查后,请转到下一行。完成所有行后,对列执行相同操作。
伪代码:
for ($currentCollumn=1; $gridWidth > $currentCollumn; $currentCollumn++) {
$collumn = get collumn as string
for ($i=1;$i!=7;$i++) {
get first $i characters of $collumn and compare to dictionary
}
for ($i=1;$i!=7;$i++) {
get last $i characters of $collumn and compare to dictionary
}
}
for ($currentRow=1; $gridHeight > $currentRow; $currentRow++) {
$row = get row as string
for ($i=1;$i!=7;$i++) {
get first $i characters of $row and compare to dictionary
}
for ($i=1;$i!=7;$i++) {
get last $i characters of $row and compare to dictionary
}
}
编辑:第 2 版,因为我没有意识到这些词不仅限于直线。
从每个可能的位置开始。
// define variables:
booleanArray path[x][y] = false;
p = pointerlocation;
stack[] = p;
currentString = "";
p.up/.down/.left/.right 分别检查 y+1, y-1, x+1, x-1 的 path[][] 。如果超出范围,它将返回 false。
stack[] 像 x86 堆栈一样工作。后进先出。
push() 将一个新对象添加到堆栈中
pop() 获取最后一个添加到堆栈的对象并将其从堆栈中移除
function getAllTheStrings(p.x, p.y) {
while (p) {
path (p.x, p.y) = true;
currentString += p.getCharacter();
// check neighbors
if (p.up) {
stack.push(p.up, path, currentString);
}
if (p.down) {
stack.push(p.down, path, currentString);
}
if (p.left) {
stack.push(p.left, path, currentString);
}
if (p.right) {
stack.push(p.right, path, currentString);
}
// add current string to list possible words
foundWords.push(currentString);
// pop next item from stack
overwrite variables with stored values of: p, path, currentString
if (stack is empty) {
p = false; // end loop
}
}
}
这将被调用:
function getWords() {
for ($y=1; $gridHeight > $y; $y++) {
for ($x=1; $gridWidth > $x; $x++) {
getAllTheStrings(x,y);
}
}
这个函数随着网格大小的缩放非常糟糕,因为它必须检查可能路径的每一个组合。 1x1 网格将进行一次测试。 2x2 需要 28 次测试。在 3x3 时,我在大约 270 次测试中数不清。
循环结束后,foundWords 将逐字逐句检查整个字典。在字典中找到 5 个单词和 100 个单词将进行 500 次比较。实际上,字典至少有一千个单词。