我的代码:

#include <bits/stdc++.h>
using namespace std;
#define in cin
#define out cout
#define int long long
int n, m, t, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1};
char maps[25][25];
struct node {
	int x, y, r, state;
};
queue <node> q;
int bfs() {
	while (!q.empty()) {
		int ux = q.front().x, uy = q.front().y, ur = q.front().r, ustate = q.front().state;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int tx = dx[i] + ux, ty = uy + dy[i];
			if (tx >= 0 && ty >= 0 && tx < n && ty < m && maps[tx][ty] != '#')
				if (islower(maps[tx][ty]))
					q.push({tx, ty, ur + 1, (ustate | (1 << (maps[tx][ty] - 'A')))});//, maps[tx][ty] = '#';
				else if (isupper(maps[tx][ty]))
					if (ustate & (1 << (maps[tx][ty] - 'A')))
						q.push({tx, ty, ur + 1, ustate});
					else;
				else if (maps[tx][ty] == '$')
					return ++ur;
				else
					q.push({tx, ty, ur + 1, ustate});//, maps[tx][ty] = '#';
		}
	}
	return -1;
}
signed main() {
	in >> n >> m >> t;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			in >> maps[i][j];
			if (maps[i][j] == '@')
				q.push({i, j, 0, 0}), maps[i][j] = '.';
		}
	out << bfs();
	return 0;
}

可以看到 bfs 的部分没有任何标记数组,但却 AC 了,之后试着输出 n×mn \times m,得出这 55 组数据 n×mn \times m 最大才 2525,很显然,这种代码本应 MLE,但地图太小,与数据范围相差过大,才使得不带标记数组也能 AC,建议加强数据。

0 comments

No comments so far...

Information

ID
1154
Time
1000ms
Memory
256MiB
Difficulty
9
Tags
# Submissions
28
Accepted
3
Uploaded By