QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#394171 | #7751. Palindrome Path | 251Sec | WA | 1ms | 3840kb | C++14 | 1.9kb | 2024-04-20 09:35:27 | 2024-04-20 09:35:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, a[35][35];
int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };
char dc[] = "UDLR";
int sx, sy, ex, ey;
vector<int> ans;
void Mov(int x, int y, int d) {
int k = 0;
while (a[x - dx[d] * (k + 1)][y - dy[d] * (k + 1)] && a[ex + dx[d] * (k + 1)][ey + dy[d] * (k + 1)]) k++;
if (a[ex + dx[d] * (k + 1)][ey + dy[d] * (k + 1)]) {
for (int i = 1; i <= k + 1; i++) ans.push_back(d ^ 1);
for (int i = 1; i <= k + 1; i++) ans.push_back(d);
}
else {
for (int i = 1; i <= k; i++) ans.push_back(d ^ 1);
for (int i = 1; i <= k + 1; i++) ans.push_back(d);
}
}
bool vis[35][35];
pair<int, int> prt[35][35];
bool h[35][35];
void DFS1(int x, int y) {
if (x == ex && y == ey) h[x][y] = true;
vis[x][y] = true;
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (vis[nx][ny] || !a[nx][ny]) continue;
DFS1(nx, ny), prt[nx][ny] = { x, y };
h[x][y] |= h[nx][ny];
}
}
void DFS2(int x, int y) {
vis[x][y] = true;
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (vis[nx][ny] || !a[nx][ny]) continue;
if (!h[nx][ny] && prt[nx][ny].first == x && prt[nx][ny].second == y) {
Mov(x, y, d);
DFS2(nx, ny);
Mov(x, y, d ^ 1);
}
}
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (vis[nx][ny] || !a[nx][ny]) continue;
if (h[nx][ny] && prt[nx][ny].first == x && prt[nx][ny].second == y) {
Mov(x, y, d);
DFS2(nx, ny);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%1d", &a[i][j]);
}
}
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
DFS1(sx, sy), memset(vis, 0, sizeof(vis)), DFS2(sx, sy);
if (!vis[ex][ey]) return puts("-1"), 0;
for (int i : ans) putchar(dc[i]);
reverse(ans.begin(), ans.end());
for (int i : ans) putchar(dc[i]);
putchar('\n');
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3840kb
input:
2 2 11 11 1 1 2 2
output:
DRDUDDUDRD
result:
ok Valid Solution (Length = 10).
Test #2:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
2 2 10 01 1 1 2 2
output:
-1
result:
ok No Solution.
Test #3:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
1 1 1 1 1 1 1
output:
result:
ok Valid Solution (Length = 0).
Test #4:
score: 0
Accepted
time: 0ms
memory: 3836kb
input:
5 4 1111 1111 1111 1111 1111 4 2 4 2
output:
DDUUDDDUUUDDDUUUURLLUDUDDUDDUDDLRLLRRDUDDUUDDDUUUDDDUUUULLRRRUDUDDUDDUDDDDUUDDDUUUDDDUUUUDDDUUUURLLUDDUDDUDDUDDRLLRLLDDUUDDDUUUDDDUUUUDDDUUUULLRRUDDUDDUDDDDUDDUDDURRLLUUUUDDDUUUUDDDUUUDDDUUDDLLRLLRDDUDDUDDUDDULLRUUUUDDDUUUUDDDUUUDDDUUDDDDUDDUDDUDURRRLLUUUUDDDUUUDDDUUDDUDRRLLRLDDUDDUDDUDULLRUUUUDDDUU...
result:
ok Valid Solution (Length = 308).
Test #5:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
5 5 11111 10101 11111 10101 11111 1 4 5 5
output:
RRLLDDDDRRRLLLRRRRLLLLDUDDUUDDDUUUDDDDUUUURRRRRLLLLLDDRRRRRLLLLLDDRRRRDUDDUUDDDUUUDDDDUUUUDDRLRDDDDRLRDDUUUUDDDDUUUDDDUUDDUDRRRRDDLLLLLRRRRRDDLLLLLRRRRRUUUUDDDDUUUDDDUUDDUDLLLLRRRRLLLRRRDDDDLLRR
result:
ok Valid Solution (Length = 194).
Test #6:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
5 3 111 100 111 001 111 4 3 3 2
output:
DRLRLLLRRLRRUURLRLLUULRLRRRLLRLLDDLRRRRLDDLLRLLRRRLRLUULLRLRUURRLRRLLLRLRD
result:
ok Valid Solution (Length = 74).
Test #7:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
5 4 1001 1101 1111 0011 0010 2 2 1 1
output:
UDRUDUUDDURUUUUUDDUUDDDUUDDDLULLUUUULLULDDDUUDDDUUDDUUUUURUDDUUDURDU
result:
ok Valid Solution (Length = 68).
Test #8:
score: 0
Accepted
time: 1ms
memory: 3796kb
input:
5 3 101 111 100 111 100 4 1 2 2
output:
DULRLRRRLLRLLUUUDLRLRRUDRLLLLRDURRLRLDUUULLRLLRRRLRLUD
result:
ok Valid Solution (Length = 54).
Test #9:
score: -100
Wrong Answer
time: 0ms
memory: 3772kb
input:
5 5 01110 10110 11110 11011 11100 2 4 5 1
output:
DDDUUULLLLRRDDLLRRRDLRLDDUULLDDLLRRLLDUDDUUDDDUUUDDDDDDUUUDDDUUDDUDLLRRLLDDLLUUDDLRLDRRRLLDDRRLLLLUUUDDD
result:
wrong answer (4,2) Not Visited