0

我将棋盘读入字符向量的向量 B 是 4x4 棋盘 将“排序”字典读入字符串向量我扫描 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);
            }
        }
    }
}
4

1 回答 1

3

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

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

如果你从 the 开始S向东走,O你最终会找到SOCK. 递归返回到S并且您探索南边的单元格。您找不到STOCKor STOP,因为O仍然标记为可见。

于 2014-01-27T08:34:29.130 回答