T1: RPG跑腿任务

本题难度中等,\(\operatorname{BFS}\) 求最短路,需要注意走到和 \(M\) 相邻的格子就算到达。

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

int main() {
    int r, c;
    cin >> r >> c;
    
    vector<string> s(r);
    rep(i, r) cin >> s[i];
    
    set<P> st;
    rep(i, r)rep(j, c) {
        if (s[i][j] == 'M') {
            if (i-1 >= 0 and s[i-1][j] != 'T') st.emplace(i-1, j);
            if (i+1 < r and s[i+1][j] != 'T') st.emplace(i+1, j);
            if (j-1 >= 0 and s[i][j-1] != 'T') st.emplace(i, j-1);
            if (j+1 < c and s[i][j+1] != 'T') st.emplace(i, j+1);
        }
    }
    
    const int INF = 1001001001;
    vector<vector<int>> dist(r, vector<int>(c, INF));
    queue<P> q;
    rep(i, r)rep(j, c) {
        if (s[i][j] == 'S') {
            q.emplace(i, j);
            dist[i][j] = 0;
        }
    }
    while (q.size()) {
        auto [i, j] = q.front(); q.pop();
        if (st.count(P(i, j))) {
            cout << dist[i][j] << '\n';
            return 0;
        }
        rep(v, 4) {
            int ni = i+di[v], nj = j+dj[v];
            if (ni < 0 or ni >= r or nj < 0 or nj >= c) continue;
            if (s[ni][nj] == 'T') continue;
            if (dist[ni][nj] != INF) continue;
            dist[ni][nj] = dist[i][j]+1;
            q.emplace(ni, nj);
        }
    }
    
    puts("-1");
    
    return 0;
}

T2:背包漏洞0

本题难度较大,优先上下左右移动,先把从起点出发可以移动到的格子都走过,之后再瞬移到一个 \((x_i, y_i)\)。然后以瞬移到的位置为起点,继续上下左右移动走到所有能走到的格子,再瞬移到下一格 \((x_i, y_i)\)。如此反复即可移动到所有可以走到的格子。