QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#479196 | #7751. Palindrome Path | ucup-team052# | WA | 2ms | 16256kb | C++14 | 2.7kb | 2024-07-15 15:51:45 | 2024-07-15 15:51:45 |
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];
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) {
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 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);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
allcnt += c[i][j] - '0';
}
}
dfs(tx, ty);
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);
for (auto i : ans) printf("%c", dc[i]);
for (int k = 1; k <= len; k++) printf("%c", dc[seq[k]]);
for (int k = len; k >= 1; k--) printf("%c", dc[seq[k]]);
reverse(ans.begin(), ans.end());
for (auto i : ans) printf("%c", dc[i]);
return 0;
}
}
}
printf("-1\n");
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 14052kb
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: 10144kb
input:
2 2 10 01 1 1 2 2
output:
-1
result:
ok No Solution.
Test #3:
score: 0
Accepted
time: 1ms
memory: 10088kb
input:
1 1 1 1 1 1 1
output:
result:
ok Valid Solution (Length = 0).
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 16256kb
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)