单词搜索难题:如何搜索一系列字母来查找单词



我正在尝试创建一个单词搜索拼图游戏,该游戏要求用户输入他们认为在数组中的单词,如果找到该单词,请给用户一个点(总共10分,然后打印出祝贺消息)。

在此代码中,我有一个常规的数组,但是根据一些研究,我认为2D阵列可能会更好。你怎么看?具体来说,我对我应该考虑哪种搜索算法感兴趣

#include <iostream>
using namespace std;
int main() {
// your code goes here
  cout<<"=================================================================================="<<endl; 
cout<<"||                                                                              ||"<<endl;
cout<<"||                        Welcome to the ECE 114 Game Design!                   ||"<<endl;
cout<<"||                                                                              ||"<<endl;
cout<<"=================================================================================="<<endl;   
cout<<"This is a sample output"<<endl; 

//We want to ask the user to enter a word that they think the cross-word puzzle contains
//if the puzzle contains that word, we tell the user they are correct and update their points
//score by 1. Total of 10 points to gain, when the points score reaches 10 the user gets congratulatory message
string myArray[] = { "A B C D E F G H
                      S P A D E T R Y
                      T I G O A L E A
                      T R A I N E A W
                      O A P B E A T N  "};
//The first array above will just be a visual for the player to see
//This second array is the one that I will use to search for words
 char wordBank[5][8] = [[ 'A', 'B ','C', 'D', 'E', 'F', 'G', 'H'],  
                    ['S','P', 'A', 'D', 'E', 'T', 'R', 'Y'], 
                    ['T', 'I', 'G', 'O', 'A', 'L', 'L', 'E', 'A'], 
                    ['T', 'R','A', 'I', 'N', 'E', 'A', 'W'], 
                    ['O', 'A', 'P', 'B', 'E', 'A', 'T', 'N']];
cout<<myArray<<endl;


return 0;
}

我建议您添加一些其他变量以使搜索更容易。例如,在垂直和水平方向上创建文本字符串。

static const std::string puzzle_text[] =
{
  "ABCDEFGH",
  "SPADETRY",
  "TIGOALEA",
  "TRAINEAW",
  "OAPBEATN"
};
int main(void)
{
  std::string vertical_texts[8];
  for (unsigned int column = 0; column < 8; ++column)
  {
    for (unsigned int row = 0; row < 5; ++row)
    {
      vertical_text[column] += puzzle_text[row].at(column);
    }
  }
  return 0;
}

通过创建垂直字母的字符串,可以使用std::string::find。每个难题只需要创建一次垂直字符串。

OP的任务:反向搜索(请参阅std::string方法)和对角线字符串创建。

编辑1:搜索算法
这是我在搜索这些难题中使用的算法:
1.提取关键词的第一个字母。
2.搜索文本排以获取字母。
3.如果找到了字母,请检查相邻的信件中的第二个关键字字母。
3.1。如果找到第二个字母,请继续方向并寻找剩余的关键字字母。

这对我有好处,尤其是很难找到单词。

最新更新