问题描述
void scoreBoardHelper(Grid<char>& board,Lexicon& lex,Set<string>& visitedPrefixes,string current,GridLocation location,Set<string>& foundWords,Set<GridLocation> visitedTiles){
if(lex.contains(current) && current.size()>3){
foundWords.add(current);
}
if(current == ""){
current += board[location];
visitedTiles.add(location);
}
//check neighbors
GridLocation neighbor = location;
for(int i = 0; i<8; i++){
switch (i) {
case 0:
neighbor.coL++;
break;
case 1:
neighbor.col--;
break;
case 2:
neighbor.row++;
break;
case 3:
neighbor.row--;
break;
case 4:
neighbor.coL++;
neighbor.row++;
break;
case 5:
neighbor.col--;
neighbor.row--;
break;
case 6:
neighbor.col--;
neighbor.row++;
break;
case 7:
neighbor.coL++;
neighbor.row--;
break;
}
if(board.inBounds(neighbor) && board[neighbor]!=current[current.size()-2] && !visitedTiles.contains(neighbor) && lex.containsprefix(current + board[neighbor]) && !visitedPrefixes.contains(current + board[neighbor])){
visitedPrefixes.add(current + board[neighbor]);
visitedTiles.add(neighbor);
scoreBoardHelper(board,lex,visitedPrefixes,current + board[neighbor],neighbor,foundWords,visitedTiles);
}
neighbor = location;
}
}
int scoreBoard(Grid<char>& board,Lexicon& lex) {
Set<string> visitedPrefixes;
Set<GridLocation> visitedTiles;
string current = "";
GridLocation location;
Set<string> foundWords;
for(int r=0;r<board.numRows();r++){
location.row=r;
for(int c=0;c<board.numCols();c++){
location.col=c;
scoreBoardHelper(board,current,location,visitedTiles);
}
}
int prevIoUsPoints = 0;
for(string word : foundWords){
cout<<"found word: "<<word<<endl;
prevIoUsPoints += points(word);
}
return prevIoUsPoints;
}
我编写了这两个函数,但它们似乎无法解决boggle。当我删除!visitedTiles.contains(neighbor)
检查时,它可以按预期工作,但是会发现应丢弃的其他单词。进行此检查后,它会意外缩小解决方案的范围,并在应该找到更多单词的地方找到2个单词。注意:积分功能可以正常工作。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)