C++ 文字解算器-全方位
我为所有方向创建了一个单词解算器。它可以水平、垂直和反向查找单词。然而,我有问题,使它去所有的方向。因此,要理解“你好”,请: 谁能告诉我怎么做?下面是我搜索单词的算法(在C++中):C++ 文字解算器-全方位,c++,algorithm,matrix,wordsearch,C++,Algorithm,Matrix,Wordsearch,我为所有方向创建了一个单词解算器。它可以水平、垂直和反向查找单词。然而,我有问题,使它去所有的方向。因此,要理解“你好”,请: 谁能告诉我怎么做?下面是我搜索单词的算法(在C++中): /* *对于在所有8个可能方向上搜索每行、每列的循环。 */ void-Scramble::solve(){ cout单词[searchResult].length()) 打破 if(单词[搜索结果]==(顺序)) cout这里有一个有趣的思考方式:找到单词就像解决一个迷宫。“开始”和“结束”对应于你要寻找的单词
/*
*对于在所有8个可能方向上搜索每行、每列的循环。
*/
void-Scramble::solve(){
cout单词[searchResult].length())
打破
if(单词[搜索结果]==(顺序))
cout这里有一个有趣的思考方式:找到单词就像解决一个迷宫。“开始”和“结束”对应于你要寻找的单词的开始和结束,“死胡同”对应于路径和单词之间的不匹配,“成功”是指当你路径上的字符串匹配时
这里的好消息是,有很多关于迷宫求解算法的资源。我熟悉的一个特别的算法是
显然,为了解决您的问题,必须进行一些更改。例如,您不知道起点在哪里,但幸运的是,这并不重要。您可以检查每个可能的起点位置,但由于不匹配,许多起点位置在第一步都将被丢弃。只需将其视为一个图形,其中每个字母is连接到所有相邻的字母,并从每个字母开始执行深度/广度优先搜索,只接受字母等于您要查找的下一个字母的节点。1)当前,您的solve()
函数从每个点开始在直线上查找单词:这是您想要的吗?我之所以这样问,是因为“hello”在示例矩阵中不显示为直线:
H E i l
x L p q
c L O m
如果你真的只想要直线的话,那就好了(这就是我一直理解的工作方式),但如果事实上你想以蛇形搜索的方式找到单词,那么像Zilchonum和BlueRaja这样的递归搜索将是一个不错的选择。只是要小心,不要在已经使用的字母上循环
2) 在这两种情况下,您的verifyWord()
函数也有一些问题:在退出while(low
循环的情况下,它至少需要返回一些值
即使如此,它仍然不能完全满足您的需要:例如,说您的字典
包含{“ant”、“bat”、“hello”、“yak”、“zoo”}
,如果您使用str=“hel”
调用verifyWord()
,您希望返回值2,但此时它会执行以下操作:
step low mid high
0 0 0 5 // initialise
1 0 2 5 // set mid = (0+5)/2 = 2... words[2] == "hello"
2 0 2 1 // "hel" < "hello" so set high = mid - 1
3 0 0 1 // set mid = (0+1)/2 = 0... words[0] == "ant"
4 1 0 1 // "hel" > "ant" so set low = mid + 1
5 // now (low<high) is false, so we exit the loop with mid==0
步进低-中-高
05//初始化
1 0 2 5//set mid=(0+5)/2=2…单词[2]=“你好”
2 0 2 1//“Hell”<“hello”因此设置为高=中-1
3 0 0 1//set mid=(0+1)/2=0…单词[0]=“ant”
4 1 0 1/“hel”>“ant”因此设置为低=中+1
5//now(low这是我编写的一个简单的word sleuth程序-->
#包括
使用名称空间std;
int main()
{
int a,b,i,j,l,t,n,f,g,k;
coutb;//输入行数和列数
炭毡[100][100],硫[100];
你可以说这是一个路径查找问题。:)你认为我可以通过在上面的代码中再添加一个递归函数来实现这一点吗?听起来不错-但是它对直线非常有效。是的,它仍然有效,但我认为findWords()
将尝试匹配非字典中的单词…它只有在到达矩阵边缘时才会停止尝试。verifyWord()
将成功地将“hello”匹配到“hello”,因此当您最终找到该单词时,您会得到正确的结果。它只是做了大量额外的工作才能达到目的,因为您无法找到它“当第一个字符序列在字典中不构成有效单词时,请避免继续搜索单词。”
H E i l
x L p q
c L O m
step low mid high
0 0 0 5 // initialise
1 0 2 5 // set mid = (0+5)/2 = 2... words[2] == "hello"
2 0 2 1 // "hel" < "hello" so set high = mid - 1
3 0 0 1 // set mid = (0+1)/2 = 0... words[0] == "ant"
4 1 0 1 // "hel" > "ant" so set low = mid + 1
5 // now (low<high) is false, so we exit the loop with mid==0
#include<iostream>
using namespace std;
int main()
{
int a, b, i, j, l, t, n, f, g, k;
cout<<"Enter the number of rows and columns: "<<endl;
cin>>a>>b; //Inputs the number of rows and columns
char mat[100][100], s[100];
cout<<"Enter the matrix: "<<endl;
for (i = 0; i < a; i++) for (j = 0; j < b; j++) cin>>mat[i][j]; //Inputs the matrix
cout<<"Enter the number of words: "<<endl;
cin>>t; //Inputs the number of words to be found
while (t--)
{
cout<<"Enter the length of the word: "<<endl;
cin>>n; //Inputs the length of the word
cout<<"Enter the word: "<<endl;
for (i = 0; i < n; i++) cin>>s[i]; //Inputs the word to be found
for (i = 0; i < a; i++) //Loop to transverse along i'th row
{
for (j = 0; j < b; j++) //Loop to transverse along j'th column
{
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, g++); //Loop to find the word if it is horizontally right
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" right"<<endl;
goto A;
}
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, g--); //Loop to find the word if it is horizontally left
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" left"<<endl;
goto A;
}
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, f++); //Loop to find the word if it is vertically down
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" down"<<endl;
goto A;
}
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, f--); //Loop to find the word if it is vertically up
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" up"<<endl;
goto A;
}
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, f++, g++); //Loop to find the word if it is down right
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" down right"<<endl;
goto A;
}
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, f--, g--); //Loop to find the word if it is up left
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" up left"<<endl;
goto A;
}
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, f++, g--); //Loop to find the word if it is down left
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" down left"<<endl;
goto A;
}
f = i;
g = j;
for (k = 0; s[k] == mat[f][g] && k < n; k++, f--, g++); //Loop to find the word if it is up right
if (k == n)
{
cout<<"The coordinates and direction are ---> "<<j+1<<","<<i+1<<" up right"<<endl;
goto A;
}
}
}
A:; //If the word has been found the program should reach this point to start the search for the next word
}
return 0;
}