QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#394526#7751. Palindrome PathluanmengleiWA 1ms3868kbC++173.0kb2024-04-20 15:40:152024-04-20 15:40:15

Judging History

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

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

answer

#include <bits/stdc++.h>
using namespace std;

namespace SOL {

using i64 = long long;
void debug(const char *msg, ...) {
#ifdef CLESIP
    va_list arg;
    static char pbString[512];
    va_start(arg,msg);
    vsprintf(pbString,msg,arg);
    cerr << "[DEBUG] " << pbString << "\n";
    va_end(arg);
#endif    
}
template<typename T, typename L>
bool chkmax(T &x, L y) { if (x < y) return x = y, true; return false; }
template<typename T, typename L>
bool chkmin(T &x, L y) { if (y < x) return x = y, true; return false; }

const int N = 35;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
const int neg[] = { 1, 0, 3, 2 };
const char wrd[] = { 'D', 'U', 'R', 'L' };
int n, m;
char pic[N][N];
array<int, 2> st, ed;
bool tvis[N][N], vis[N][N][N][N];
array<int, 5> lst[N][N][N][N];
vector<int> travel;
queue<array<int, 4>> q;

void dfs(int x, int y) {
	tvis[x][y] = true;
	for (int i = 0; i < 4; i ++) {
		int xx = x + dx[i], yy = y + dy[i];
		if (xx >= 1 && yy >= 1 && xx <= n && yy <= m && !tvis[xx][yy] && pic[xx][yy] == '1') {
			travel.push_back(i);
			dfs(xx, yy);
			travel.push_back(neg[i]);
		}
	}
}

array<int, 2> walk(int x, int y, int op) {
	int xx = x + dx[op], yy = y + dy[op];
	// debug("walk (%d, 5)")
	if (xx < 1 || yy < 1 || xx > n || yy > m || pic[xx][yy] == '0')
		return { x, y };
	return { xx, yy };
}

void bfs() {
	lst[st[0]][st[1]][ed[0]][ed[1]][0] = -1;
	vis[st[0]][st[1]][ed[0]][ed[1]] = true;
	q.push({ st[0], st[1], ed[0], ed[1] });
	while (!q.empty()) {
		auto [x, y, u, v] = q.front(); q.pop();
		if (x == u && y == v) {
			vector<int> ans;
			while (lst[x][y][u][v][0] != -1) {
				auto [op, xx, yy, uu, vv] = lst[x][y][u][v];
				ans.push_back(op);
				x = xx, y = yy, u =  uu, v = vv;
			}
			for (int i = int(travel.size()) - 1; i >= 0; i --)
				cout << wrd[travel[i]];
			cerr << "\n";
			for (int i = int(ans.size()) - 1; i >= 0; i --)
				cout << wrd[ans[i]];
			cerr << "\n";
			for (int i = 0; i < int(ans.size()); i ++)
				cout << wrd[ans[i]];
			cerr << "\n";
			for (int i = 0; i < int(travel.size()); i ++)
				cout << wrd[travel[i]];
			return;
		}
		for (int i = 0; i < 4; i ++) {
			auto [xx, yy] = walk(x, y, i);
			auto [uu, vv] = walk(u, v, i);
			if (vis[xx][yy][uu][vv])
				continue;
			vis[xx][yy][uu][vv] = true;
			lst[xx][yy][uu][vv] = { i, x, y, u, v };
			q.push({ xx, yy, uu, vv });
		}
	}
	assert(false);
}

void solve() {
	cin >> n >> m;	
	for (int i = 1; i <= n; i ++)
		cin >> (pic[i] + 1);
	cin >> st[0] >> st[1] >> ed[0] >> ed[1];
	dfs(ed[0], ed[1]);
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
			if (!tvis[i][j] && pic[i][j] == '1')
				return cout << "-1\n", void();
	for (int i = int(travel.size()) - 1; i >= 0; i --)
		st = walk(st[0], st[1], travel[i]);
	debug("st (%d, %d)", st[0], st[1]);
	bfs();
}

}
int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	SOL::solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3540kb

input:

2 2
11
11
1 1 2 2

output:

DRUDLUDRRDULDURD

result:

ok Valid Solution (Length = 16).

Test #2:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

2 2
10
01
1 1 2 2

output:

-1

result:

ok No Solution.

Test #3:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

1 1
1
1 1 1 1

output:


result:

ok Valid Solution (Length = 0).

Test #4:

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

input:

5 4
1111
1111
1111
1111
1111
4 2 4 2

output:

ULDDDDRUURDDUUUUDDLDDLLUUUUDDDDRUUUURDDDDRRRRDDDDRUUUURDDDDUUUULLDDLDDUUUUDDRUURDDDDLU

result:

ok Valid Solution (Length = 86).

Test #5:

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

input:

5 5
11111
10101
11111
10101
11111
1 4 5 5

output:

DDDDRRUULRUURRDDLRDDLRUUUULLLRDDDDLLUUUUDDDDRRRRRRRRDDDDUUUULLDDDDRLLLUUUURLDDRLDDRRUURLUURRDDDD

result:

ok Valid Solution (Length = 96).

Test #6:

score: 0
Accepted
time: 0ms
memory: 3524kb

input:

5 3
111
100
111
001
111
4 3 3 2

output:

RDDLLRRUULLUURRLLDDRRDDLLRRUULLUURRLLDDR

result:

ok Valid Solution (Length = 40).

Test #7:

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

input:

5 4
1001
1101
1111
0011
0010
2 2 1 1

output:

UULLULDDDUUURUDDRDURDDDDLLDDDDRUDRDDURUUUDDDLULLUU

result:

ok Valid Solution (Length = 50).

Test #8:

score: -100
Wrong Answer
time: 0ms
memory: 3868kb

input:

5 3
101
111
100
111
100
4 1 2 2

output:

RDUUULLRRUDDDLLDURLDDDDDDLRUDLLDDDURRLLUUUDR

result:

wrong answer (1,3) Not Visited