- 题解:这题学习做了5个小时,最后拿了4分,这题综合考察了PAT大量的知识点,虽然核心部分是对DFS和图结构的综合应用。。。我这种题目还是做得太少了,经验不够丰富,其实我的时间主要都是花在理解DFS算法上面,因为我之前是有考虑是用最短路径去做的,我还学习了他们之前的一些不同。。。。
- 我后来还考虑了用非递归的方式来实现这个功能,遍历地铁图是成功了,但是增加完整的应用功能的时候后来还是失败了,我现在大体能够判断出错的地方,就是在于我传递给栈的参数太少,导致最后无法还原之前的工作状态,我也是经过这个手写递归发现了其实高级程序设计语言支持递归的原因,对于那个时代,可能这就是程序员们比较需要的一种加速开发的机制了,毕竟c++和c当年用于开发系统软件太多了,很经常需要使用高级的数据结构,网络通信,操作系统的实现都是树和图结构出现的高频场景
- 其实这道题的还有一个难点就是图结构的建立,它是没有办法使用邻接矩阵来做的,因为内存消耗太大了,它应该使用领接表来做,但是将原始的数据转化成领接表来表示还有一个过程
- 经过这道题,我对于PAT最后的大题的整体的难度有了更深的认识,我相信经过继续的训练我应该能在最后的考试考出理想的成绩
- 我的代码里面注释比较多,而且逻辑相比一些博主的满分解答清晰很多,虽然分数很低,但是还是值得你作为一种辅助参考材料
-
// A1131.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <bits/stdc++.h> using namespace std; #define max 10010 vector<vector<int>> row_in; vector<int> line(max,-1);//which line the station belong //inorder to check transfer //vector<vector<bool>> gn(max_station, vector<bool>(max_station, false));1*10^8有100MB来存储这个地图 //the PAT plantfrom is not allowed this size of memory vector<int> gn[max];//gn[max]如果用方括号的话前面就只要vector<int>,而不是vector<vector<int>> vector<bool> visited(max, false); int s1,s2; vector<int> path,final_path; int minCnt, minTransfer; int transferCnt(vector<int> path) { int cnt = -1, preLine = 0; for (int i = 1; i < path.size(); i++) { if (line[path[i]] != preLine) cnt++; preLine = line[path[i]]; } return cnt; } void dfs(int root,int cnt) { visited[root] = true; path.push_back(root); if (root == s2 && (cnt < minCnt || (cnt == minCnt && transferCnt(path) < minTransfer))) { minCnt = cnt;//又找到了一条到达终点的新路root == s2 minTransfer = transferCnt(path);//取代的条件,新路径经过的节点更少,或者中转更少 final_path = path;//新的去终点的路径去代旧路径 } if (root == s2) {//一条路径已经找到 return; //回退一格看看有没有别的路 } for (int i = 0; i < gn[root].size(); i++) { if (visited[gn[root][i]] == false) { dfs(gn[root][i],cnt+1); visited[gn[root][i]] = false;//把终点取消标记,为了下次能够找到 path.pop_back();//清除路径中的终点 } } } struct param { int root; int count; }; void DFS(int root,int count) { stack<param> s;//param list stack<param list> s.push({root,0}); /* example :struct pos{ int x,int y}; stack<pos>; */ visited[root] = true; bool is_push; while (!s.empty()) { is_push = false; param x= s.top(); if (x.root == s2 && (x.count < minCnt || (x.count == minCnt && transferCnt(path) < minTransfer))) { minCnt = x.count;//又找到了一条到达终点的新路root == s2 minTransfer = transferCnt(path);//取代的条件,新路径经过的节点更少,或者中转更少 final_path = path;//新的去终点的路径去代旧路径 } if (x.root != s2) {//一条路径已经找到 for (int i = 0; i < gn[x.root].size(); i++) { if (visited[gn[x.root][i]] == false) { visited[gn[x.root][i]] = true; path.push_back(gn[x.root][i]); s.push({ gn[x.root][i],x.count + 1 }); is_push = true; break; } } } if (!is_push) { //if (x.root == s2) { // x = s.top(); // visited[x.root] = false;//把终点取消标记,为了下次能够找到 // path.pop_back();//清除路径中的终点 //} if (path.size() != 0) { path.pop_back(); } visited[x.root] = false; s.pop(); } } } int main() { #ifndef ONLINE_JUDGE FILE* s; freopen_s(&s, "in.txt", "r", stdin); #endif // !ONLINE_JUDGE int n; cin >> n; row_in.resize(n + 1); for (int i = 1; i <= n; i++) { int m; cin >> m; int t; for (int j = 0; j < m; j++) { cin >> t; line[t] = i;//i : line number /t:station id row_in[i].push_back(t); } } for (int i = 1; i <= n; i++) { for (int j = 1; j < row_in[i].size(); j++) { //row_in[i][j] station id gn[row_in[i][j]].push_back(row_in[i][j-1]);//把后继加入 //因为是无向图 gn[row_in[i][j - 1]].push_back(row_in[i][j]); } }// //构建图结构 int k; cin >> k; for (int i = 0; i < k; i++) { cin >> s1 >> s2;//s1,s2表示start和end minCnt = 99999, minTransfer = 99999; path.clear(); dfs(s1,0); //DFS(s1,0); //for (auto s : final_path) { // cout << s << " "; //} printf("%d\n", minCnt); int preLine = 0, preTransfer = s1; for (int j = 1; j < final_path.size(); j++) { if (line[final_path[j]] != preLine) { if (preLine != 0) printf("Take Line#%d from %04d to %04d.\n", preLine, preTransfer, final_path[j - 1]); preLine = line[final_path[j]]; preTransfer = final_path[j - 1]; } } printf("Take Line#%d from %04d to %04d.\n", preLine, preTransfer, s2); } return 0; }
PAT2021年春季3月份备考_按照套卷刷真题(7-4)
最新推荐文章于 2021-03-17 12:07:29 发布