QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#686258 | #7751. Palindrome Path | test_algth | WA | 0ms | 3820kb | C++14 | 2.5kb | 2024-10-29 09:49:06 | 2024-10-29 09:49:07 |
Judging History
answer
#include <bits/stdc++.h>
const int MAXN = 35;
char A[MAXN][MAXN];
bool vis[MAXN][MAXN];
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char op[] = {'R', 'L', 'D', 'U'};
int N, M;
struct Node {
int x, y;
Node() {}
Node(int a, int b) : x(a), y(b) {}
friend Node operator + (const Node& a, int b) {
return Node(a.x + dx[b], a.y + dy[b]);
}
friend bool operator == (const Node& a, const Node& b) {
return a.x == b.x && a.y == b.y;
}
friend int operator - (const Node& a, const Node& b) {
for (int i = 0; i < 4; ++i) {
if ((a + i) == b) return i;
}
return -1;
}
inline bool check() {
if (x < 1 || x > N || y < 1 || y > M || A[x][y] == '0') return false;
return true;
}
inline void input() {
std::cin >> x >> y;
}
} s, t;
std::string ans;
Node fa[MAXN][MAXN];
int cnts, find;
inline bool check(Node x, int o) {
Node y = x + o;
return y.check();
}
inline void move(int o) {
Node nowS = s, nowT = t;
int step = 0;
while (check(nowS, o ^ 1) && check(nowT, o)) {
++step;
nowS = nowS + (o ^ 1);
nowT = nowT + o;
ans += op[o ^ 1];
}
if (check(nowT, o)) ans += op[o ^ 1];
for (int i = 1; i <= step + 1; ++i)
ans += op[o];
s = s + o;
}
void solve(Node now) {
++find;
for (int i = 0; i < 4; ++i) {
auto cur = now + i;
if (cur.check() == false || vis[cur.x][cur.y]) continue;
fa[cur.x][cur.y] = now;
vis[cur.x][cur.y] = true;
move(i);
solve(cur);
move(i ^ 1);
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int testCase = 1;
// std::cin >> testCase;
while (testCase--) {
std::cin >> N >> M;
ans = "";
cnts = find = 0;
for (int i = 1; i <= N; ++i) {
std::cin >> (A[i] + 1);
for (int j = 1; j <= M; ++j) {
vis[i][j] = false;
cnts += A[i][j] == '1';
}
}
s.input();
t.input();
vis[s.x][s.y] = true;
solve(s);
if (A[s.x][s.y] == '0' || A[t.x][t.y] == '0' || find != cnts) {
std::cout << "-1\n";
continue;
}
std::vector <int> Line;
while ((t == s) == false) {
Line.push_back(fa[t.x][t.y] - t);
t = fa[t.x][t.y];
}
std::reverse(Line.begin(), Line.end());
for (int o : Line)
move(o);
std::cout << ans;
std::reverse(ans.begin(), ans.end());
std::cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
2 2 11 11 1 1 2 2
output:
RDRLRDURLLRUDDURLLRUDRLRDR
result:
ok Valid Solution (Length = 26).
Test #2:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
2 2 10 01 1 1 2 2
output:
-1
result:
ok No Solution.
Test #3:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
1 1 1 1 1 1 1
output:
result:
ok Valid Solution (Length = 0).
Test #4:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
5 4 1111 1111 1111 1111 1111 4 2 4 2
output:
LLRRLLRRRUDDRLRLLRLLDUDDUULRLLRRLLRRRDDDUUURLRLLRLLDDDUUUULRLLRRLLRRRRLRLLRLLUDLRLLRRLLRRRUDDRLRLLRLLUDDUDDLRLLRRLLRRRDURLRLLLLRLRUDRRRLLRRLLRLDDUDDULLRLLRLRDDURRRLLRRLLRLDULLRLLRLRRRRLLRRLLRLUUUUDDDLLRLLRLRUUUDDDRRRLLRRLLRLUUDDUDLLRLLRLRDDURRRLLRRLL
result:
ok Valid Solution (Length = 250).
Test #5:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
5 5 11111 10101 11111 10101 11111 1 4 5 5
output:
RDDRLRRLLRRRLLLRRRRLLLLDDRRRRDUDRLRRLLDUDRRRLLLRRRRLLLLDUDDUUDDDUUUDDDDUUUURRDDDDDUUUURRRLLLRRRRLLLLDDRRRRDDDUUUDDDDUUUURLLRRDDRLRRLLRRRLLLRRRLLLLDDLRLRRLRRLRRRRLRRLRRLRLDDLLLLRRRLLLRRRLLRRLRDDRRLLRUUUUDDDDUUUDDDRRRRDDLLLLRRRRLLLRRRUUUUDDDDDRRUUUUDDDDUUUDDDUUDDUDLLLLRRRRLLLRRRDUDLLRRLRDUDRRRRDDLLLLR...
result:
ok Valid Solution (Length = 318).
Test #6:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
5 3 111 100 111 001 111 4 3 3 2
output:
DRLRLLLRLRRUURLRLLUULRLRRRLRLLDDLRLRRDDUULLUUDDRRLRLDDLLRLRRRLRLUULLRLRUURRLRLLLRLRD
result:
ok Valid Solution (Length = 84).
Test #7:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
5 4 1001 1101 1111 0011 0010 2 2 1 1
output:
LUUDDRRRUUDDDLUUDDURUUUUDUUDDLLLUUUDRRLUULRRDUUULLLDDUUDUUUURUDDUULDDDUURRRDDUUL
result:
ok Valid Solution (Length = 80).
Test #8:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
5 3 101 111 100 111 100 4 1 2 2
output:
LRLRRRLRLLDUUULRLRRUDRLRLLUDDDDDUUDDDUUULRRLUUUDDDUUDDDDDULLRLRDURRLRLUUUDLLRLRRRLRL
result:
ok Valid Solution (Length = 84).
Test #9:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
5 5 01110 10110 11110 11011 11100 2 4 5 1
output:
LDLLRRRDLRLDULLLDLRDLLRRLLLRDULDDUUDDDUUUDLRLLRRDUDDUULLRRLLLRDLRRLUUDDRLLRLLUUDDRUUDDRLLLLRDDUURDDUULLRLLRDDUULRRLDRLLLRRLLUUDDUDRRLLRLDUUUDDDUUDDLUDRLLLRRLLDRLDLLLUDLRLDRRRLLDL
result:
ok Valid Solution (Length = 178).
Test #10:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
5 3 011 111 110 111 011 3 1 2 1
output:
LRUUDDDLLRRUDLLRULLLRUULLRRULLRUDLLLRUUDDLLRDUULLUUDRLLDDUURLLLDURLLURRLLUURLLLURLLDURRLLDDDUURL
result:
ok Valid Solution (Length = 96).
Test #11:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
4 5 11111 11111 11111 11111 3 2 1 3
output:
LLRRLLRRRLLRRRUUUDDDRLRRLLRRLLLRRLLLUULRLLRRLLRRRLLRRRURLRRLLRRLLLRRLLLLRLLRRLLRRRLLRRRUDRLRRLLRRLLLRRLLLUUDDUUUDDDLRLLRRLLRRRLLRRRURLRRLLRRLLLLLRRLLLRRRLLLRRRRUDDRLRLLRLLRLLDUDDUULRLLRRLLLRRRLLLRRRRDDUUURLRLLLLRLRUUUDDRRRRLLLRRRLLLRRLLRLUUDDUDLLRLLRLLRLRDDURRRRLLLRRRLLLRRLLLLLRRLLRRLRURRRLLRRRLLRRL...
result:
ok Valid Solution (Length = 418).
Test #12:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
5 5 11111 10101 11111 10101 11111 2 5 1 1
output:
UUDDLLLLUUUDDDUUUUDDDDLRLLRRLLLRRRLLLLRRRRUUUUUDDDDLLUUUUUDDDDLLUUUULRLLRRLLLRRRLLLLRRRRLLUDULLUDUUDDLRLLRRLLLRRRLLLLRRRRUUUDDLLLLDUUDUUUUDUUDLLLLDDUUURRRRLLLLRRRLLLRRLLRLDDUUDULLUDULLRRRRLLLLRRRLLLRRLLRLUUUULLDDDDUUUUULLDDDDUUUUURRRRLLLLRRRLLLRRLLRLDDDDUUUUDDDUUULLLLDDUU
result:
ok Valid Solution (Length = 272).
Test #13:
score: 0
Accepted
time: 0ms
memory: 3748kb
input:
4 5 11111 10000 11111 00001 1 3 4 5
output:
RRLLLLDDRRRRDDULLLLDUDUURRRRLLLRRLLLDDLRLLRRLLRRRLLRRRDDRRRLLRRRLLRRLLRLDDLLLRRLLLRRRRUUDUDLLLLUDDRRRRDDLLLLRR
result:
ok Valid Solution (Length = 110).
Test #14:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
3 5 10100 00010 00111 1 3 1 1
output:
-1
result:
ok No Solution.
Test #15:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
4 5 10001 11111 11100 11111 4 5 3 1
output:
LLLLDULRLLRRDDUULLRRRLLRRRDUUDLLLLDDUUUUDLRLLRRUDLLUDDLRLLRRLLRRRLLRRRRLRRLLRRRLLLRRRRLLLLUULLLLRRRRLLLRRRLLRRLRRRRLLRRRLLRRLLRLDDULLDURRLLRLDUUUUDDLLLLDUUDRRRLLRRRLLUUDDRRLLRLUDLLLL
result:
ok Valid Solution (Length = 182).
Test #16:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
3 5 11111 10100 11111 1 2 3 5
output:
RRRRLRRLLDDRRRLRRLLRRRLLLRRRRLLLLUUDDRRUURRRLLLLLRRDDLLLRRRLLLRRRRRRRRLLLRRRLLLDDRRLLLLLRRRUURRDDUULLLLRRRRLLLRRRLLRRLRRRDDLLRRLRRRR
result:
ok Valid Solution (Length = 132).
Test #17:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
4 5 01110 10101 11011 10111 1 3 2 3
output:
-1
result:
ok No Solution.
Test #18:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
5 5 11111 11111 11111 11111 11111 1 3 5 2
output:
LLLRRRLLLRRRRDRLRLLRLLRLLDLRLLRRLLLRRRLLLRRRRDRLRLLRLLRLLDLRLLRRLLLRRRLLLRRRRRLRLLRLLRLLDULRLLRRLLLRRRLLLRRRRDDUURLRLLRLLRLLDDDUUUDDDDUUUULRRLLDLRLLRRLLLRRRLLLRRRRDDDDUUUURLRLLLLRRRLLRRRUDRLRRLLRRLLLRRLLLUUDDLRLLRRLLRRRLLRRRUUUDDDRLRRLLRRLLLRRLLLUUUUDDDDLRRLDDDDUUUULLLRRLLLRRLLRRLRDDDUUURRRLLRRRLLRR...
result:
ok Valid Solution (Length = 512).
Test #19:
score: -100
Wrong Answer
time: 0ms
memory: 3652kb
input:
5 5 11111 10101 11111 10101 11111 5 1 2 3
output:
RRRRDUDUULLLLUUUDDDDUUDUUDUURRRRUDDUULLUDDUULLUDUUDDRRUUUDDDDUURRUUUDDDUUUDDDDLLLLLRLLRRLLLRRRLLLLRRRRDUDDUULLLLDDDUUUDDDDUUUULRLLRRDDRRLLRLUUUUDDDDUUUDDDLLLLUUDDUDRRRRLLLLRRRLLLRRLLRLLLLLDDDDUUUDDDUUURRUUDDDDUUURRDDUUDULLUUDDULLUUDDURRRRUUDUUDUUDDDDUUULLLLUUDUDRRRR
result:
wrong answer End Point Is (2,1), Not (er = 2, ec = 3)