算法问题实战策略 SORTGAME
地址 https://algospot.com/judge/problem/read/SORTGAME
解答
常规BFS是会超时的 按照书上的提示 应该是打表(居然还有提倡打表的题目)
tle 代码

1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <queue> 5 #include <map> 6 7 using namespace std; 8 9 int n, m; 10 11 12 int bfs(const vector<int>& v) 13 { 14 vector<int> sorted = v; 15 sort(sorted.begin(), sorted.end()); 16 queue<vector<int>> q; 17 map<vector<int>, int> distance; 18 distance[v] = 0; 19 q.push(v); 20 while (!q.empty()) { 21 vector<int> here = q.front(); 22 q.pop(); 23 24 if (here == sorted) return distance[here]; 25 int cost = distance[here]; 26 //翻转可能的子区间 27 for (int i = 0; i < v.size(); i++) { 28 for (int j = i + 2; j <= v.size(); ++j) { 29 reverse(here.begin() + i, here.begin() + j); 30 if (distance.count(here) == 0) { 31 distance[here] = cost + 1; 32 q.push(here); 33 } 34 reverse(here.begin() + i, here.begin() + j); 35 } 36 } 37 } 38 39 return -1; 40 } 41 42 43 int main() 44 { 45 cin >> n; 46 47 while (n--) { 48 cin >> m; 49 vector<int> v; 50 for (int i = 0; i < m; i++) { 51 int t; 52 cin >> t; 53 v.push_back(t); 54 } 55 56 cout << bfs(v) << endl; 57 } 58 59 60 return 0; 61 }
ac代码

1 #include <iostream> 2 #include <algorithm> 3 #include <map> 4 #include <queue> 5 #include <vector> 6 7 using namespace std; 8 9 map<vector<int>, int> toSort; 10 11 void precalc(int n) { 12 vector<int> perm(n); 13 for (int i = 0; i < n; ++i) perm[i] = i; 14 queue<vector<int>> q; 15 q.push(perm); 16 toSort[perm] = 0; 17 while (!q.empty()) { 18 vector<int> here = q.front(); 19 q.pop(); 20 int cost = toSort[here]; 21 for (int i = 0; i < n; i++) { 22 for (int j = i + 2; j <= n; ++j) { 23 reverse(here.begin() + i, here.begin() + j); 24 if (toSort.count(here) == 0) { 25 toSort[here] = cost + 1; 26 q.push(here); 27 } 28 reverse(here.begin() + i, here.begin() + j); 29 } 30 } 31 } 32 } 33 34 int solve(const vector<int>& perm) 35 { 36 int n = perm.size(); 37 vector<int> fixed(n); 38 for (int i = 0; i < n; ++i) { 39 int smaller = 0; 40 for (int j = 0; j < n; ++j) 41 if (perm[j] < perm[i]) 42 ++smaller; 43 fixed[i] = smaller; 44 } 45 return toSort[fixed]; 46 } 47 48 49 int n, m; 50 int main() 51 { 52 cin >> n; 53 while (n--) { 54 cin >> m; 55 precalc(m); 56 vector<int> perm; 57 for (int i = 0; i < m; i++) { 58 int t; 59 cin >> t; 60 perm.push_back(t); 61 } 62 cout << solve(perm) << endl; 63 } 64 65 66 return 0; 67 }
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话