QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#483511 | #2345. Karel the Robot | karuna | TL | 1032ms | 80464kb | C++20 | 4.2kb | 2024-07-18 18:20:24 | 2024-07-18 18:20:25 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n, m, f, q, a[40][40];
string F[36];
vector<int> M[36], T[36];
bool in(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1}; // s, e, n, w;
const char ch[4] = {'s', 'e', 'n', 'w'};
struct state {
int x;
bool operator<(const state &ot) const {
return x < ot.x;
}
};
int dir(char c) {
return c == 's' ? 0 : (c == 'e' ? 1 : (c == 'n' ? 2 : 3));
}
bool check_cond(state st, char c) {
if (c == 'b') {
int nx = st.x / (4 * m) + dx[st.x % 4];
int ny = st.x / 4 % m + dy[st.x % 4];
return !in(nx, ny) || a[nx][ny];
}
else {
return ch[st.x % 4] == c;
}
}
state do_move(state st, char c) {
if (c == 'm') {
if (check_cond(st, 'b')) return st;
else {
int nx = st.x / (4 * m) + dx[st.x % 4];
int ny = st.x / 4 % m + dy[st.x % 4];
return state{4 * m * nx + 4 * ny + st.x % 4};
}
}
else {
int d = st.x % 4;
st.x -= d;
st.x += (d + 1) % 4;
return st;
}
}
map<tuple<state, int, int>, state> mp;
state run(state st, int fn, int pos) {
// cout << "running " << "(" << st.x << ", " << st.y << "), facing " << ch[st.d] << '\n';
// cout << "function " << (fn < 26 ? (char)('A' + fn) : (char)('0' + (fn - 26))) << '\n';
// cout << F[fn] << '\n';
// for (int i = 0; i < pos; i++) cout << ' ';
// cout << "^";
// cout << endl;
if (mp.find({st, fn, pos}) != mp.end()) return mp[{st, fn, pos}];
mp[{st, fn, pos}] = state{-1};
if (pos >= F[fn].size()) return mp[{st, fn, pos}] = st;
char c = F[fn][pos];
if (c == '(' || c == 'i' || c == 'u') return mp[{st, fn, pos}] = run(st, fn, pos + 1);
else if (c == ')') {
if (T[fn][pos] == 1) { // until
return mp[{st, fn, pos}] = run(st, fn, M[fn][pos] - 1);
}
else {
if (pos == (int)F[fn].size() - 1 || F[fn][pos + 1] != '(') {
return mp[{st, fn, pos}] = run(st, fn, pos + 1);
}
else {
return mp[{st, fn, pos}] = run(st, fn, M[fn][pos + 1] + 1);
}
}
}
else if (c == 'b' || c == 'n' || c == 's' || c == 'e' || c == 'w') {
bool flag = check_cond(st, c);
if (flag) {
if (F[fn][pos - 1] == 'u') return mp[{st, fn, pos}] = run(st, fn, M[fn][pos + 1] + 1);
else return mp[{st, fn, pos}] = run(st, fn, pos + 1);
}
else {
if (F[fn][pos - 1] == 'u') return mp[{st, fn, pos}] = run(st, fn, pos + 1);
else return mp[{st, fn, pos}] = run(st, fn, M[fn][pos + 1] + 1);
}
}
else if (c == 'm' || c == 'l') {
return mp[{st, fn, pos}] = run(do_move(st, c), fn, pos + 1);
}
else if ('A' <= c && c <= 'Z') {
state ret = run(st, c - 'A', 0);
if (ret.x < 0) return mp[{st, fn, pos}];
else return mp[{st, fn, pos}] = run(ret, fn, pos + 1);
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> m >> f >> q;
for (int i = 0; i < n; i++) {
string S;
cin >> S;
for (int j = 0; j < m; j++) {
if (S[j] == '#') a[i][j] = true;
}
}
for (int i = 0; i < f + q; i++) {
state st;
if (i >= f) {
int x, y; string T;
cin >> x >> y >> T;
st.x = 4 * m * (x - 1) + 4 * (y - 1) + dir(T[0]);
}
string S; cin >> S;
int fn;
if (i < f) {
fn = S[0] - 'A';
F[fn] = S.substr(2);
}
else {
fn = 26 + i - f;
F[fn] = S;
}
int sz = F[fn].size();
M[fn].resize(sz);
T[fn].resize(sz);
vector<int> ST;
for (int j = 0; j < sz; j++) {
if (F[fn][j] == '(') ST.push_back(j);
if (F[fn][j] == ')') {
int pos = ST.back();
ST.pop_back();
M[fn][pos] = j;
M[fn][j] = pos;
}
}
for (int j = 0; j < sz; j++) {
if (F[fn][j] == 'u') T[fn][j + 2] = T[fn][M[fn][j + 2]] = 1;
if (F[fn][j] == 'i') {
int pos = j + 2;
T[fn][pos] = T[fn][M[fn][pos]] = 2;
pos = M[fn][pos] + 1;
T[fn][pos] = T[fn][M[fn][pos]] = 2;
}
}
if (i >= f) {
state res = run(st, 26 + i - f, 0);
if (res.x < 0) cout << "inf\n";
else {
int x = res.x / (4 * m);
int y = res.x / 4 % m;
int d = res.x % 4;
cout << x + 1 << ' ' << y + 1 << ' ' << ch[d] << '\n';
}
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3536kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 3644kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 3584kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 3676kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 3868kb
Test #6:
score: 0
Accepted
time: 5ms
memory: 4396kb
Test #7:
score: 0
Accepted
time: 1032ms
memory: 80464kb
Test #8:
score: 0
Accepted
time: 61ms
memory: 16800kb
Test #9:
score: -100
Time Limit Exceeded