zoukankan      html  css  js  c++  java
  • AcWing 第 13 场周赛  补题记录

    比赛链接:Here

    AcWing 3811. 排列

    签到题,

    先输出 (n) 然后输出 (1sim n -1) 即可

    AcWing 3812. 机器人走迷宫

    不会什么特别高级的方法 qaq,只能全排列 + bfs(懒得写太长了,注释写代码里 qaq)

    #include <bits/stdc++.h>
    #define rep(a,b,c) for (int i = a; i <= b; i += c)
    #define rept(i,a,b,c) for (int i = a; i <= b; i += c)
    
    using namespace std;
    
    const int N = 1e3 + 5, INF = INFINITY;
    int n, m, a[N][N];
    string st;
    
    int sx, sy, fx, fy;
    
    void init () {
        cin >> n >> m;
        rep (1, n, 1) rept (j, 1, m, 1) {
            char ch; cin >> ch;
            a[i][j] = (ch == '#');
            if (ch == 'S') sx = i, sy = j;
            if (ch == 'E') fx = i, fy = j; // 起点 终点
        }
        cin >> st;
    }
    
    int mx[] = {0, 0, -1, 1}, my[] = {-1, 1, 0, 0}, mtx[50]; // mtx 表示对应 0~3 的分配
    
    struct Node { int x, y, step; };
    
    bool bfs () { // 广搜模板不需要太多解释
        queue<Node> q;
        q.push ({sx, sy, 0});
        while (!q.empty()) {
            Node tp = q.front(); q.pop();
            int x = tp.x, y = tp.y, sp = tp.step;
            if (x == fx && y == fy && sp <= st.size()) return 1;
            int M = mtx[st[sp] & 15]; // 位运算,等价于 M = mtx[st[sp] - 48]
            x += mx[M], y += my[M], sp ++;
            if (!(x && y && x <= n && y <= m && !a[x][y])) return 0;// 如果撞墙/越界就送走(
            q.push ({x, y, sp});
        }
        return 0;
    }
    
    void solve() {
        mtx[0] = 0; mtx[1] = 1; mtx[2] = 2; mtx[3] = 3;
        int res = 0;
        do res += bfs(); /*可以到达终点 方案 + 1*/ while (next_permutation (mtx, mtx + 4)); // 全排列
        printf ("%d
    ", res);
        return ;
    }
    
    int main() {
        int T; cin >> T;
        while (T --) init(), solve();
        return 0;
    }
    

    AcWing 3813. 最大路径权值

    拆点 + topsort

    拆点: (dist[N][26]) 把每个点拆成 (26) 个点, 分别表示到当前点的路径上, 每个字母的最大值

    topsort:找环, 判掉有环的情况, 我们可以发现, 无论最长路径的字母是哪个字母, 我们都可以从该路径对应的拓扑序的第一个点开始遍历, 所以只要按照拓扑序线性递推就行了

    const int N = 3e5 + 10, M = 5e5;
    int h[N], e[M], ne[M], idx;
    int dist[N][30];
    int d[N];
    typedef pair<int, int> PII;
    int hh = 0, tt = -1;
    bool st[N];
    int q[N];
    int w[N];
    int n, m;
    
    void add(int a, int b) { // 添加一条边a->b
        e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    bool topsort() {
        // d[i] 存储点i的入度
        for (int i = 1; i <= n; i ++ )
            if (!d[i])
                q[ ++ tt] = i;
        while (hh <= tt) {
            int t = q[hh ++ ];
            for (int i = h[t]; i != -1; i = ne[i]) {
                int j = e[i];
                if (-- d[j] == 0)
                    q[ ++ tt] = j;
            }
        }
    
        // 如果所有点都入队了,说明存在拓扑序列;否则不存在拓扑序列。
        return tt == n - 1;
    }
    
    int main() {
        scanf("%d%d", &n, &m);
        string tmp;
        cin >> tmp;
        memset(h, -1, sizeof h);
        for (int i = 1; i <= n; i ++ ) {
            int x = tmp[i - 1] - 'a';
            w[i] = x;
        }
        while (m -- ) {
            int a, b;
            scanf("%d%d", &a, &b);
            add(a, b);
            d[b] ++;
        }
        for (int i = 1; i <= n; i ++ ) {
            if (!d[i]) {
                dist[i][w[i]] = 1;
            }
        }
        bool flag = topsort();
        if (!flag) {
            puts("-1");
            return 0;
        }
        for (int i = 0; i <= tt; i ++ ) {
            int j = q[i];
            for (int u = h[j]; ~u; u = ne[u]) {
                int k = e[u];
                for (int v = 0; v < 26; v ++ ) {
                    dist[k][v] = max(dist[k][v], dist[j][v] + (w[k] == v));
                }
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i ++ ) {
            for (int j = 0; j < 26; j ++ ) {
                ans = max(ans, dist[i][j]);
            }
        }
        printf("%d", ans);
        return 0;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    Maximum Flow Exhaustion of Paths Algorithm
    ubuntu下安装java环境
    visualbox使用(二)
    vxworks一个超级奇怪的错误(parse error before `char')
    February 4th, 2018 Week 6th Sunday
    February 3rd, 2018 Week 5th Saturday
    February 2nd, 2018 Week 5th Friday
    February 1st, 2018 Week 5th Thursday
    January 31st, 2018 Week 05th Wednesday
    January 30th, 2018 Week 05th Tuesday
  • 原文地址:https://www.cnblogs.com/RioTian/p/15177552.html
Copyright © 2011-2022 走看看