【问题标题】:Word Hunting Game algorithm in C++C ++中的单词狩猎游戏算法
【发布时间】:2012-03-05 00:28:28
【问题描述】:

我目前正在编写一个游戏,它以蛇形方式在矩阵中查找输入的单词。以下是游戏的简要说明。

用户将首先要求输入包含行的文件名以创建矩阵。创建后,用户将要求输入要在该矩阵中搜索的单词。搜索只针对南、东和东南。如果它找到了单词,则显示坐标并显示一些消息。下面是一个示例矩阵:

m e r e t z
e x i t a v
p p w a b i
y u u b l l
a l l l a l
z k v e l o

我已经设法在这个矩阵中搜索“exit”之类的词,但我的问题是“mere”或“table”之类的词。在我的算法中,我只能在两个方向上搜索没有相同字母的单词。我找不到合适的方法来做到这一点。

这是我的代码的搜索部分。

bool Search(tmatrix<char>& m, tmatrix<int>& c, const string& w, int i, int j, int index)  // m is the matrix to search in  // w is the word  // i and j are coordinates of matrix {
if(m[i][j] == w[index])
{
    c[index][0] = i; // c matrix is to keep coordinates of words
    c[index][1] = j;

    if(index != w.length()-1)
    {
            if((i < m.numrows()-1) && (m[i+1][j] == w[index+1]))
                return Search(m, c, w, i+1, j, index+1);
            else if((j < m.numcols()-1) && (i < m.numrows()-1) && (m[i+1][j+1] == w[index+1]))
                return Search(m, c, w, i+1, j+1, index+1);
            else if((j < m.numcols()-1) && (m[i][j+1] == w[index+1]))
                return Search(m, c, w, i, j+1, index+1);
            else 
                return false;
    }
    else
        return true;
}
return false;

}

int main()
{
   bool IsFound = false;                    //to check whether the word is found or not in the matrix
            tmatrix<int> coord(word.length(), 2);   //another matrix to keep coordinates of found word's coordinates.
                                                    //it works with the index of words and the row index of matrix.

            for(int i = 0; i < m.numrows(); i++)
            {
                for(int j = 0; j < m.numcols(); j++)
                {
                    int index = 0;      //another variable to keep index number
                    IsFound = Search(m, coord, word, i, j, index);      //searches matrix for word and if found, makes IsFound's return value true
                    if(IsFound)
                    {
                        cout << "The word "<< word << " is found!\n";
                        cout << "Indices in which this word is found in the matrix are:\n";
                        for(; index < word.length(); index++)
                        {
                            cout << word[index] << ":\t" << coord[index][0] << "," << coord[index][1] << endl;
                        }
                        break;      //whenever it finds a match in matrix, it finishes search in loops
                    }
                }
                if(IsFound)
                    break;
            }
}

它只指向首先出现在 if 语句列表中的方向。将else ifs 更改为if 对我不起作用。

【问题讨论】:

    标签: c++ search matrix word


    【解决方案1】:

    只需添加额外的布尔矩阵,它表明使用了这个词。因此,在调用搜索之前,将该布尔值设置为无效,然后在进行该方向之前,检查是否“未使用”

    【讨论】:

    • 我不太明白,您是否建议我在调用 main 中的 Search 函数或递归调用之前添加一个额外的布尔矩阵?
    • 您遇到的问题是您必须知道是否使用了 char。另一种方法是,您也可以将 char 替换为 null char 或类似内容,然后在从递归返回后将其放回。
    【解决方案2】:
    void Search(tmatrix<char>& m, tmatrix<int>& c, const string& w, int i, int j, int index) {
    if(m[i][j] == w[index])
    {
        c[index][0] = i;
        c[index][1] = j;
    
        if(index != w.length()-1)
        {
                if((i < m.numrows()-1) && (m[i+1][j] == w[index+1]))
                {
                    Search(m, c, w, i+1, j, index+1);
                }
                if((j < m.numcols()-1) && (i < m.numrows()-1) && (m[i+1][j+1] == w[index+1]))
                {
                    Search(m, c, w, i+1, j+1, index+1);
                }
                if((j < m.numcols()-1) && (m[i][j+1] == w[index+1]))
                {
                    Search(m, c, w, i, j+1, index+1);
                }
        }
        else
            IsFound = true;
    } }
    

    把函数改成这个解决了我所有的问题。感谢您的评论,他们启发了我。

    【讨论】:

    • 你能解释一下这里发生了什么吗?您只是复制了解决方案。乍一看没有有用的信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多