QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#479220 | #7751. Palindrome Path | ucup-team052# | WA | 1ms | 14044kb | C++14 | 3.2kb | 2024-07-15 16:00:06 | 2024-07-15 16:00:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const char dc[4] = {'R', 'L', 'D', 'U'};
const int N = 905;
char c[35][35];
queue < pair <int, int> > q;
vector <int> ans;
int frx[N][N], fry[N][N], fr[N][N], vis[N][N], vis2[35][35];
int n, m, sx, sy, tx, ty;
inline int s(int x, int y) { return (x - 1) * m + y; }
int seq[N * N], len;
void solve(int x, int y) {
if (!frx[x][y]) return;
solve(frx[x][y], fry[x][y]);
seq[++len] = fr[x][y];
}
int allcnt;
void dfs(int x, int y) {
--allcnt;
vis[x][y] = 1;
for (int d = 0; d < 4; d++) {
int xx = x + dx[d], yy = y + dy[d];
if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && c[xx][yy] == '1' && !vis[xx][yy]) {
ans.push_back(d ^ 1);
dfs(xx, yy);
ans.push_back(d);
}
}
}
void go(int &x, int &y, int d) {
int tx = x + dx[d], ty = y + dy[d];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && c[tx][ty] == '1') x = tx, y = ty;
}
int tmp;
void upd(int x, int y) {
if (!vis2[x][y]) {
vis2[x][y] = 1;
++allcnt;
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
int _sx = sx, _sy = sy;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
allcnt += c[i][j] - '0';
}
}
tmp = allcnt;
dfs(tx, ty);
if (allcnt) {
printf("-1\n");
return 0;
}
for (auto i : ans) go(sx, sy, i);
memset(vis, 0, sizeof(vis));
q.push(make_pair(s(sx, sy), s(tx, ty))); vis[s(sx, sy)][s(tx, ty)] = 1;
while (!q.empty()) {
pair <int, int> t = q.front(); q.pop();
int ux1 = (t.first - 1) / m + 1, uy1 = (t.first - 1) % m + 1;
int ux2 = (t.second - 1) / m + 1, uy2 = (t.second - 1) % m + 1;
for (int d = 0; d < 4; d++) {
for (int i = 0; i <= 1; i++) {
int x1, y1, x2, y2;
x1 = ux1 + dx[d]; y1 = uy1 + dy[d];
if (x1 < 1 || x1 > n || y1 < 1 || y1 > m || c[x1][y1] == '0') x1 = ux1, y1 = uy1;
if (i) {
x2 = ux2 + dx[d ^ 1], y2 = uy2 + dy[d ^ 1];
if (x2 < 1 || x2 > n || y2 < 1 || y2 > m || c[x2][y2] == '0') continue;
} else {
x2 = ux2 + dx[d], y2 = uy2 + dy[d];
if (x2 >= 1 && x2 <= n && y2 >= 1 && y2 <= m && c[x2][y2] == '1') continue;
x2 = ux2; y2 = uy2;
}
int t1 = s(x1, y1), t2 = s(x2, y2);
if (vis[t1][t2]) continue;
vis[t1][t2] = 1; frx[t1][t2] = t.first; fry[t1][t2] = t.second; fr[t1][t2] = d; q.push(make_pair(t1, t2));
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int t = s(i, j);
if (vis[t][t]) {
solve(t, t);
upd(_sx, _sy);
assert((int)ans.size() * 2 + len * 2 <= 1000000);
for (auto i : ans) printf("%c", dc[i]), go(_sx, _sy, i), upd(_sx, _sy);
for (int k = 1; k <= len; k++) printf("%c", dc[seq[k]]), go(_sx, _sy, seq[k]), upd(_sx, _sy);
for (int k = len; k >= 1; k--) printf("%c", dc[seq[k]]), go(_sx, _sy, seq[k]), upd(_sx, _sy);
reverse(ans.begin(), ans.end());
for (auto i : ans) printf("%c", dc[i]), go(_sx, _sy, i), upd(_sx, _sy);
assert(_sx == tx && _sy == ty && tmp == allcnt);
return 0;
}
}
}
assert(0);
printf("-1\n");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14036kb
input:
2 2 11 11 1 1 2 2
output:
RDLRULRLDUUDLRLURLDR
result:
ok Valid Solution (Length = 20).
Test #2:
score: 0
Accepted
time: 1ms
memory: 6048kb
input:
2 2 10 01 1 1 2 2
output:
-1
result:
ok No Solution.
Test #3:
score: 0
Accepted
time: 1ms
memory: 8032kb
input:
1 1 1 1 1 1 1
output:
result:
ok Valid Solution (Length = 0).
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 14044kb
input:
5 4 1111 1111 1111 1111 1111 4 2 4 2
output:
LURRRRULLLLRRRRDDLLDLLRRRRLLURRULLLLDRRRRLLLLDUUUUUUDLLLLRRRRDLLLLURRULLRRRRLLDLLDDRRRRLLLLURRRRUL
result:
wrong answer End Point Is (2,3), Not (er = 4, ec = 2)