【题目】来源:xmuoj
【知识点】 DFS+链表+二进制压缩
【核心代码】
- 1. bool dfs(int cnt){
- 2. if (cnt==0) // 所有空格都得到了合法答案,返回
- 3. return true;
- 4. list::iterator it=find(); // 启发搜索,取得当前选择最少的点
- 5. int x=it->x,y=it->y;
- 6. int choice=getchoice(x,y); // 获取该点可选数字的二进制集合
- 7. it=blank.erase(it); // 删去该点
- 8.
- 9. while (choice){
- 10. int key=getnum(lowbit(choice)); // 获取可选数字
- 11. add(x,y,key); // 记录可选数字
- 12. if (dfs(cnt-1)) // 向下递归
- 13. return true;; // 如果返回 true 说明已经找到答案,返回
- 14. remove(x,y,key); // 删去可选数字的记录
- 15. choice^=lowbit(choice); // 获取下一个可选数字
- 16. }
- 17. blank.insert(it,Coordinate{x,y}); // 复原,插入该点
- 18. return false;
- 19.}