T1:TLD
模拟
代码实现
s = input()
a = s.split('.')
print(a[-1])
T2:Langton's Takahashi
模拟
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
int main() {
int h, w, n;
cin >> h >> w >> n;
vector<string> s(h, string(w, '.'));
int i = 0, j = 0, v = 0;
rep(ti, n) {
if (s[i][j] == '.') {
s[i][j] = '#';
v += 1;
}
else {
s[i][j] = '.';
v += 3;
}
v %= 4;
i += di[v]; j += dj[v];
i = (i+h)%h;
j = (j+w)%w;
}
rep(i, h) cout << s[i] << '\n';
return 0;
}
T3:Perfect Bus
先假设初始人数为 \(0\),然后求一遍到每一站为止的人数,找到其中最小的负数 \(x\),然后将初始人数改成 \(-x\) 就能满足题目要求
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
ll y = 0, l = 0;
rep(i, n) {
y += a[i];
l = min(l, y);
}
ll ans = y+(-l);
cout << ans << '\n';
return 0;
}
T4:Synchronized Players
维护两个玩家当前所在的点作为状态跑bfs即可
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;
using vp = vector<P>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
int main() {
int n;
cin >> n;
vector<string> s(n);
rep(i, n) cin >> s[i];
vector<P> ps;
rep(i, n)rep(j, n) {
if (s[i][j] == 'P') ps.emplace_back(i, j);
}
auto to_i = [&](vp ps) {
int res = 0;
res = res*n + ps[0].first;
res = res*n + ps[0].second;
res = res*n + ps[1].first;
res = res*n + ps[1].second;
return res;
};
auto to_ps = [&](int id) {
vp ps(2);
ps[1].second = id%n; id /= n;
ps[1].first = id%n; id /= n;
ps[0].second = id%n; id /= n;
ps[0].first = id%n; id /= n;
return ps;
};
int m = n*n*n*n;
const int INF = 1001001001;
vector<int> dist(m, INF);
queue<int> q;
int sid = to_i(ps);
dist[sid] = 0;
q.push(sid);
while (q.size()) {
int id = q.front(); q.pop();
int d = dist[id];
vp ps = to_ps(id);
rep(v, 4) {
vp nps;
for (auto [i, j] : ps) {
int ni = i+di[v], nj = j+dj[v];
if (ni < 0 or nj < 0 or ni >= n or nj >= n or s[ni][nj] == '#') {
ni = i; nj = j;
}
nps.emplace_back(ni, nj);
}
int nid = to_i(nps);
if (dist[nid] != INF) continue;
dist[nid] = d+1;
q.push(nid);
}
}
int ans = INF;
rep(id, m) {
vp ps = to_ps(id);
int d = dist[id];
if (ps[0] == ps[1]) ans = min(ans, d);
}
if (ans == INF) ans = -1;
cout << ans << '\n';
return 0;
}