QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#394172#7751. Palindrome Path251SecWA 1ms3868kbC++142.0kb2024-04-20 09:36:062024-04-20 09:36:06

Judging History

你现在查看的是最新测评结果

  • [2024-04-20 09:36:06]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3868kb
  • [2024-04-20 09:36:06]
  • 提交

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);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (a[i][j] && !vis[i][j]) 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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3656kb

input:

2 2
11
11
1 1 2 2

output:

DRDUDDUDRD

result:

ok Valid Solution (Length = 10).

Test #2:

score: 0
Accepted
time: 1ms
memory: 3636kb

input:

2 2
10
01
1 1 2 2

output:

-1

result:

ok No Solution.

Test #3:

score: 0
Accepted
time: 1ms
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: 3868kb

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: 3796kb

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: 1ms
memory: 3832kb

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: 3792kb

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: 0ms
memory: 3844kb

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: 3712kb

input:

5 5
01110
10110
11110
11011
11100
2 4 5 1

output:

DDDUUULLLLRRDDLLRRRDLRLDDUULLDDLLRRLLDUDDUUDDDUUUDDDDDDUUUDDDUUDDUDLLRRLLDDLLUUDDLRLDRRRLLDDRRLLLLUUUDDD

result:

wrong answer (4,2) Not Visited