以下函数采用三个单词对象,并相互检查每个单词的字母坐标(在表格中)。这个想法是从没有相交字母坐标的列表中获取三个单词的组合。但是,当您有超过 600000 种可能的组合时,这将变得非常耗时。
bool lettersIntersect(word one, word two, word three)
{
for(int i = 0; i < one.getLength(); i++)
for(int j = 0; j < two.getLength(); j++)
if(one.getLetterPosition(i).x == two.getLetterPosition(j).x && one.getLetterPosition(i).y == two.getLetterPosition(j).y)
return true;
for(int i = 0; i < two.getLength(); i++)
for(int j = 0; j < three.getLength(); j++)
if(two.getLetterPosition(i).x == three.getLetterPosition(j).x && two.getLetterPosition(i).y == three.getLetterPosition(j).y)
return true;
for(int i = 0; i < three.getLength(); i++)
for(int j = 0; j < one.getLength(); j++)
if(three.getLetterPosition(i).x == one.getLetterPosition(j).x && three.getLetterPosition(i).y == one.getLetterPosition(j).y)
return true;
return false;
}
有没有更有效的方法来做到这一点?