c++ - Boggle game Implementation 无法获取所有单词

标签 c++ algorithm boggle

我把棋盘读成一个char vector 的 vector B是4x4的棋盘 将“排序”字典读入字符串 vector 我扫描 4x4 板,为每个单独的单元格调用 fillGoodWoods 函数。

    int main(int argc, char *argv[])
    {
    if ( argc < 3 )
    {
        std::cout << "\n usage : ./boggle boardfile dictionaryfile ";
        return 1;
    }
    // B is the 4x4 board
    boggle::board B;
    // this function reads the board fine
    boggle::read_board(argv[1], B);
    // read the 'sorted' dictionary into a vector
    std::vector<std::string> dict;
    boggle::read_dictionary(argv[2], dict);
    int bsize = B.size();

    std::map<std::string, unsigned int> goodwords;

    for ( unsigned int i = 0; i < bsize; ++i )
    {
        for ( unsigned int j = 0 ; j < bsize ; ++j)
        {
            std::vector<std::vector<bool> > marked;
            for (unsigned int z = 0; z < bsize; z++)
            {
                marked.push_back(std::vector<bool>(bsize, false));
            }
            std::string pathStr ;
            pathStr += B[i][j];
            marked[i][j] = true; // mark visited
            fillGoodwords(i, j, goodwords, marked, pathStr, dict, B);
        }
    }
    std::map<std::string, unsigned int>::iterator itr;


    for ( itr = goodwords.begin(); itr != goodwords.end(); ++itr)
    {
        //std::cout << itr->first << "\n";
    }
    return 0;
}

fillGoodWords 是 DFS: 它必须经过路径中尚未访问过的每个相邻单元格。 对于包含 193 个单词的板,我只能提取 80 个单词

void fillGoodwords(int startrow , int startcol ,
                   std::map<std::string, unsigned int> &goodwords,
                   std::vector<std::vector<bool> > marked,
                   std::string currentStr,
                   const std::vector<std::string> &dict,
                   const boggle::board &B)
{
    if(currentStr.find("unt") == 0) {
        cout<<currentStr<<" -- ";
    }

    if (currentStr.length() >= 3)
    {
        if ( std::binary_search(dict.begin(), dict.end(), currentStr))
        {
            goodwords.insert(std::pair<std::string, unsigned int>(currentStr, 1));
        }
    }
    int boardsize = B.size();
    for ( int x = -1; x <= 1 ; ++x)
    {
        for ( int y = -1; y <= 1 ; ++y)
        {
            // checking if out of bounds
            if (  (startrow + x) < 0 || (startcol + y) < 0 || (startrow + x) >= boardsize || (startcol + y) >= boardsize )
            {
                continue;
            }
            else if (marked[startrow + x][startcol + y] == false)
            {
                marked[startrow + x][startcol + y] = true; // mark visited
                fillGoodwords(startrow + x , startcol + y, goodwords, marked, currentStr + B[startrow + x][startcol + y], dict, B);
            }
        }
    }
}

最佳答案

在收回之前将一个单元格标记为已访问,这样它就不会在同一个词中使用两次。但是您必须在递归返回后清除“同级”路径的单元格:

marked[startrow + x][startcol + y] = true; // mark as visited
fillGoodwords(startrow + x , startcol + y, goodwords, marked, currentStr + B[startrow + x][startcol + y], dict, B);
marked[startrow + x][startcol + y] = false; // clear again for other paths

举例说明:

S O C K
T P X Y
A H Z E
O W N T

如果您从 S 开始向东走到O你最终会找到SOCK .递归返回到 S然后你探索南方的牢房。你找不到 STOCKSTOP ,因为 O仍标记为可见。

关于c++ - Boggle game Implementation 无法获取所有单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21371018/

相关文章:

c++ - 将 gui 添加到 c++ 项目 (visual studio 2010)

algorithm - 检查一个数字是否可以写成一些斐波那契数的乘积

c# - 从长度为 M 的未排序数组中搜索前 N 个已排序整数?

algorithm - 最小生成树与另一个不同

algorithm - Boggle - 计算 N*N 网格上的所有可能路径。表现

c++ - vector <字符串>或 vector < vector <字符>>?

javascript - 创建 Boggle 游戏 - 将坐标链接到字母

c++ - cppcheck 为 "Redundant code: Found a statement that begins with numeric constant"语句报告 'using'

c++ - 使用 Win32 API 监控电池电量

c++ - 使用 WSARecv() 和 IOCP 时如何知道套接字何时收到 FIN 数据包?