//创建字典实现 con -> cot -> bot -> bit -> pit -> pig Lexicon test = {"con", "cot", "bot", "bat", "bit", "pit", "pig", "big", "dig"}; //已访问集合 Set<string> visited;
Queue<Vector<string>> q;
Vector<string> ladder = {start}; visited.add(start); q.enqueue(ladder); int len = start.length();
do { //返回当前ladder长度的函数,假设 int nowend = ladder.size() - 1; Vector<string> check = q.peek(); if (check[nowend] == end){ cout << q.peek() << endl;//若数组末位为end,输出 break; } //ladder没有完成,弹出队首并更新 q.dequeue(); ladder = q.peek(); //找邻居函数,假设有邻居word string tempw = check[nowend]; string word = tempw; for (int i = 0; i < len; i++){ Vector<string> tempv = ladder;//进行ladder复位 for (char j = 'a'; j <= 'z'; j++){ word[i] = j;
//加一个word形成一个ladder,然后复位,再加另一个word再形成一个ladder,直到全部访问 if (test.contains(word) && !visited.contains(word)){ tempv.add(word); q.enqueue(tempv); break; } } word = tempw; }
实现了读取每个 ladder 的末位,检测是否为 end 值,遇到访问空队列 peek 的错误,debug 模式发现 line 22 dequeue 后又访问队首,初次将会报错。
注意暂时未完成返回当前 ladder 长度的函数,随手补上。 开始调试,哎,怎么还是死循环,看一下输出…哎呀,抱一丝,忘记把 word 设置为已访问了【挠头】,补上
再次测试,球进啦!!!!芜湖!!!!起飞!!!!!!
1 2 3 4 5 6 7 8 9 10 11
lease enter the source word [return to quit]: con Please enter the destination word [return to quit]: big Here's where you'll search for a word ladder connecting "con" to "big". {"con"} {"con", "cot"} {"con", "cot", "bot"} {"con", "cot", "bot", "bat"} {"con", "cot", "bot", "bat", "bit"} {"con", "cot", "bot", "bat", "bit", "pit"} {"con", "cot", "bot", "bat", "bit", "big"} {"con", "cot", "bot", "bat", "bit", "big"}
后续将转入大字典进行压力测试。
maze-Generator
耗时良久,至少两周,发现还是不要完全脱离 AI 辅助,否则效率太低。
读源码
maze-types.h
提供了 cell 与 wall 类型可供使用
cell 类接受 int row; int col; 并重载了<运算符: 返回 row1 < row2 或 row1 == row2 && col1 < col2 的真假。
inlinebool operator<(const wall& one, const wall& two) { if (one.one < two.one) returntrue; if (two.one < one.one) returnfalse; return one.two < two.two; } // yes, this is somewhat arbitrary(专横的), but it can be arbitrary as long as it's consistent
/** * Method: addAllWalls * ---------------- * Adds (but does not display) all the walls contained within the parameter. * To display all newly-added walls on the screen, call view.repaint(); * Optimized for speed as compared to several sequential calls to addOneWall. * * This function requires that the parameter passed in is, in fact, a collection * that can be iterated through and that contains wall objects. */ template <typename C> voidaddAllWalls(const C& collection) { addAllWalls(collection.begin(), collection.end()); } // defined inline as required by template type
添加(但不显示)参数中包含的所有墙壁。
要在屏幕上显示所有新添加的墙壁,请调用 view.repaint();
与依次调用 addOneWall 相比,针对速度进行了优化。
此函数要求传入的参数实际上是一个可以迭代且包含墙壁对象的集合。
typename C 意味着可以接受各种类型的 collection,比如说 Set,Vector 之类的
if (root1 == root2){ return; } oneCell[root2.row][root2.col] = root1;
}
intmain() { while (true) { int dimension = getMazeDimension("What should the dimension of your maze be [0 to exit]? "); if (dimension == 0) break; cout << "This is where I'd animate the construction of a maze of dimension " << dimension << "." << endl;
/** * i think i should do from here * 生成所有的墙壁 * 擦去起始点的墙壁,随机擦去迷宫内的一堵墙 * 如果一堵墙分隔了两个房间,把墙擦去 */
for (int i = 0, len = allwall.size(); i < len; i++){ if (!test(allwall[i].one, allwall[i].two, oneCell)) { display.removeWall(allwall[i]); display.repaint(); merge(allwall[i].one, allwall[i].two, oneCell); }
}
getLine("press [enter] to again."); // display.~MazeGeneratorView(); }