QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#549207 | #2345. Karel the Robot | HKOI0 | AC ✓ | 6786ms | 95984kb | C++20 | 4.4kb | 2024-09-06 12:24:00 | 2024-09-06 12:24:01 |
Judging History
answer
#include <bits/stdc++.h>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto x : v) os << x << ' ';
os << '\n';
return os;
}
const vector<char> dirc{'n', 'w', 's', 'e'};
const vector<pii> dird{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
struct state {
int x, y;
char dir;
};
int to_state_num(state st) { return (st.x - 1) * 160 + (st.y - 1) * 4 + find(all(dirc), st.dir) - dirc.begin(); }
int dir_to_int(char dir) {
assert(dir != '\0');
return find(all(dirc), dir) - dirc.begin();
}
void solve() {
int n, m, d, e;
cin >> n >> m >> d >> e;
vector<string> g(n + 2);
for (int i = 1; i <= n; i++) {
cin >> g[i];
g[i] = '#' + g[i] + '#';
}
g[0] = g[n + 1] = string(m + 2, '#');
vector strs(200, string());
for (int i = 1; i <= d; i++) {
string s;
cin >> s;
char c = s[0];
strs[c] = s.substr(2);
}
map<pair<string, int>, state> val;
set<pair<string, int>> vis;
auto eval = [&](auto&& eval, string s, state st) -> state {
if (st.x == -1 || st.y == -1 || st.dir == '\0') return {-1, -1, '\0'};
int num = to_state_num(st);
if (val.find({s, num}) != val.end()) return val[{s, num}];
if (vis.find({s, num}) != vis.end()) {
return val[{s, num}] = {-1, -1, '\0'};
}
if (s.empty()) return st;
vis.emplace(s, num);
auto get_pair = [&](int l) {
int cnt = 0;
for (int j = l; j < sz(s); j++) {
if (s[j] == '(') cnt++;
else if (s[j] == ')') cnt--;
if (cnt == 0) return j;
}
assert(false);
};
for (int i = 0; i < sz(s); i++) {
if (s[i] == 'm') {
auto [dx, dy] = dird[dir_to_int(st.dir)];
if (g[st.x + dx][st.y + dy] == '.') {
st.x += dx;
st.y += dy;
}
}
else if (s[i] == 'l') {
st.dir = dirc[(dir_to_int(st.dir) + 1) % 4];
}
else if (s[i] == 'i' || s[i] == 'u') {
char c = s[i + 1];
auto check = [&] {
if (c == 'b') {
auto [dx, dy] = dird[dir_to_int(st.dir)];
if (g[st.x + dx][st.y + dy] == '#') return true;
return false;
}
else {
return st.dir == c;
}
};
if (s[i] == 'i') {
int l1 = i + 2;
int r1 = get_pair(l1);
int l2 = r1 + 1;
int r2 = get_pair(l2);
string prog1 = s.substr(l1 + 1, r1 - l1 - 1);
string prog2 = s.substr(l2 + 1, r2 - l2 - 1);
if (check()) st = eval(eval, prog1, st);
else st = eval(eval, prog2, st);
i = r2;
}
else {
int l = i + 2;
int r = get_pair(l);
string prog = s.substr(l + 1, r - l - 1);
if (!check()) {
st = eval(eval, prog, st);
st = eval(eval, s.substr(i, r - i + 1), st);
}
i = r;
}
}
else {
st = eval(eval, strs[s[i]], st);
}
if (st.x == -1 || st.y == -1 || st.dir == '\0') return val[{s, num}] = {-1, -1, '\0'};
}
return val[{s, num}] = st;
};
for (int i = 1; i <= e; i++) {
int x, y;
char dir;
cin >> x >> y >> dir;
string s;
cin >> s;
auto st = eval(eval, s, {x, y, dir});
if (st.x == -1) cout << "inf\n";
else cout << st.x << ' ' << st.y << ' ' << st.dir << '\n';
}
}
signed main() {
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << fixed << setprecision(10);
int T = 1;
// cin >> T;
while (T--) solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3600kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 3536kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 3648kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 3584kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 3624kb
Test #6:
score: 0
Accepted
time: 6ms
memory: 3936kb
Test #7:
score: 0
Accepted
time: 293ms
memory: 8984kb
Test #8:
score: 0
Accepted
time: 27ms
memory: 13720kb
Test #9:
score: 0
Accepted
time: 6786ms
memory: 68016kb
Test #10:
score: 0
Accepted
time: 0ms
memory: 3856kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 3652kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 3848kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 3660kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 3616kb
Test #15:
score: 0
Accepted
time: 7ms
memory: 4956kb
Test #16:
score: 0
Accepted
time: 15ms
memory: 6616kb
Test #17:
score: 0
Accepted
time: 6469ms
memory: 68072kb
Test #18:
score: 0
Accepted
time: 6620ms
memory: 67020kb
Test #19:
score: 0
Accepted
time: 69ms
memory: 13916kb
Test #20:
score: 0
Accepted
time: 1078ms
memory: 95984kb