PAT2021年春季3月份备考_按照套卷刷真题(7-4)

题干:1131 Subway Map (30 分)

  • 题解:这题学习做了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;
    }
    
    

     

REGISTER ADDRESS REGISTER DATA(1) HEX 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 0 LVDS_ RATE_2X 0 0 0 0 0 0 0 0 0 0 0 0 0 GLOBAL_ PDN 2 PAT_MODES_FCLK[2:0] LOW_ LATENCY_E N AVG_EN SEL_PRBS_ PAT_ FCLK PAT_MODES SEL_PRBS_ PAT_GBL OFFSET_CORR_DELAY_FROM_TX_TRIG[5:0] 3 SER_DATA_RATE DIG_GAIN_ EN 0 OFFSET_CORR_DELAY _FROM_TX_TRIG[7:6] DIG_ OFFSET_ EN 0 0 0 1 0 0 0 0 4 OFFSET_ REMOVA L_SELF OFFSET_ REMOVAL_ START_ SEL OFFEST_ REMOVAL_ START_ MANUAL AUTO_OF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DS8 PAT_LVDS5 PAT_LVDS6 0 HPF_CORNER_ADC5-8 DIG_HPF_ EN_ADC5-8 23 0 0 0 0 0 0 0 0 PAT_LVDS7 PAT_LVDS8 0 0
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exodus&Focus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值