T1: RPG跑腿任务
本题难度中等, 求最短路,需要注意走到和 相邻的格子就算到达。
代码实现
#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
本题难度较大,优先上下左右移动,先把从起点出发可以移动到的格子都走过,之后再瞬移到一个 。然后以瞬移到的位置为起点,继续上下左右移动走到所有能走到的格子,再瞬移到下一格 。如此反复即可移动到所有可以走到的格子。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】