字解决-所有方向
Word Solver - All Directions
我创建了一个面向所有方向的单词求解器。它可以水平、垂直和反向查找单词。然而,我有问题,使它去所有的方向。输入"hello"
H E i l
x L p q
c L O m
谁能告诉我怎么做?下面是我的算法来搜索单词(在c++中):
/*
* For loops that search each row, each column in all 8 possible directions.
*/
void Scramble::solve() {
cout << "Output:" << endl;
for (int row = 0; row < getRows(); row++) {
for (int col = 0; col < getCols(); col++)
for (int rowDir = -1; rowDir <= 1; rowDir++)
for (int colDir = -1; colDir <=1; colDir++)
if (rowDir != 0 || colDir != 0)
findWords(row, col, rowDir, colDir);
}
}
/*
* Finds the matches in a given direction. Also calls verifyWord() to verify that the
* current sequence of letters could possibly form a word. If not, search stops.
*/
void Scramble::findWords(int startingRow, int startingCol, int rowDir, int colDir) {
int searchResult;
string sequence = "";
sequence = sequence + wordsArr[startingRow][startingCol];
for (int i = startingRow + rowDir, j = startingCol + colDir; i >= 0 && j >= 0
&& i < getRows() && j < getCols(); i = i + rowDir, j = j + colDir) {
sequence = sequence + wordsArr[i][j];
if (sequence.length() >= 3) {
searchResult = verifyWord(words, sequence);
if ((unsigned int)searchResult == words.size())
break;
if (words[searchResult].rfind(sequence) > words[searchResult].length())
break;
if (words[searchResult] == (sequence))
cout << sequence << endl;
}
}
}
/*
* Performs the verifyWord search method.
* Searches the word to make sure that so far, there is possibly that the current sequence
* of letter could form a word. That is to avoid continuing to search for a word
* when the first sequence of characters do not construct a valid word in the dictionary.
*
* For example, if we have 'xzt', when this search is done it prevents the search
* to continue since no word in the dictionary starts with 'xzt'
*/
int Scramble::verifyWord(vector<string> words, string str) {
int low = 0;
int mid = 0;
int high = words.size();
while (low < high) {
mid = (low + high) / 2;
if (str > words[mid]) {
low = mid + 1;
}
else if (str < words[mid]) {
high = mid - 1;
}
else
return mid;
}
}
这是一种有趣的思考方式:寻找单词类似于解决迷宫。'start'和'end'对应的是你要查找的单词的开头和结尾,'dead end'对应的是路径和你要查找的单词不匹配,'success'则表示路径上的字符串匹配。
好消息是有很多关于解迷宫算法的资源。有一种算法我很熟悉,而且实现起来并不难,那就是带回溯的递归。
显然,为了解决您的问题,必须进行一些更改。例如,你不知道起点在哪里,但幸运的是,这并不重要。您可以检查每一个可能的起始位置,并且由于不匹配,它们中的许多将在第一步中被丢弃。
1)目前,您的solve()
函数在每个点开始的直线中查找单词:这是您想要的吗?我只是问,因为'hello'在您的示例矩阵中不显示为直线:
H E i l
x L p q
c L O m
如果你只想要直线单词,那么很好(这就是我一直理解这些谜题的工作方式),但如果实际上你想要以蛇形方式查找单词,那么像Zilchonum和BlueRaja建议的递归搜索将是一个不错的选择。只是要注意不要循环使用已经使用过的字母。
2)在任何一种情况下,你的verifyWord()
函数也有一些问题:至少它需要在你退出while (low < high)
循环的情况下返回一些值。
即便如此,它仍然不能完全做到你想要它做的事情:例如,你的字典包含{"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
与其将"hel"与"hello"进行比较,不如将字典中的单词截断为与str相同的长度:即比较str
与word[mid].substr(0,str.length())
?
将其视为每个字母连接到所有相邻字母的图,并从每个字母开始进行深度/宽度优先搜索,只接受那些字母等于您要查找的下一个字母的节点
这是我编写的一个简单的单词侦查程序——>
#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;
}
在我的程序中,它首先检查单词的第一个字母,然后检查后面的字母。如果找到该单词,则打印该单词的起始坐标和找到该单词的方向。
- 如何从C++中的依赖类型中获得它所依赖的类型
- 使用Unreal C++获取VR耳机的世界位置/方向
- cmake在我的项目中所需的所有静态库都不成功
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 在C++中向零方向近似的最佳方法
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 如何使用隔离>终止执行来停止所有线程
- OpenVR:向视图方向移动
- 具有所表示类的相同构造函数签名的代理类模板
- 将所选值(通过视图)从 boost::multi_array 复制到另一个数组 (C++)
- 函数如何使用引用返回所需的数字?
- 为什么 std::lerp 不适用于任何已实现所需操作的类型?
- Gtkmm 会不断检查 Gtk::日历上的所选日期是否发生变化
- 在QComboBox或QListWidget中设置所选元素的样式文本?
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 来自逆 vp 矩阵和相机位置的光线方向不正确
- 使用 OpenGL 4.5 更改所选顶点的颜色
- Linux承诺的内存比它所能提供的要多
- 如何在C++中沿对象所面对的方向在三维环境中移动对象